Skip to content

Commit d9e76e6

Browse files
committed
Fix unnecessary re-downloads during updates: #9
1 parent 4491b3d commit d9e76e6

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

.goreleaser.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ builds:
2626
- 6
2727
- 7
2828
ldflags:
29-
- "-s -w -X github.com/Chocapikk/wpprobe/cmd.version=v{{ .Version }}"
29+
- "-s -w -X github.com/Chocapikk/wpprobe/utils.Version=v{{ .Version }}"
3030

3131
- id: wpprobe-android
3232
main: ./main.go
@@ -43,7 +43,7 @@ builds:
4343
- 6
4444
- 7
4545
ldflags:
46-
- "-s -w -X github.com/Chocapikk/wpprobe/cmd.version=v{{ .Version }}"
46+
- "-s -w -X github.com/Chocapikk/wpprobe/utils.Version=v{{ .Version }}"
4747

4848
archives:
4949
- id: custom

cmd/root.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,16 @@ import (
2626
"github.com/spf13/cobra"
2727
)
2828

29-
var version = "dev"
30-
3129
var rootCmd = &cobra.Command{
3230
Use: "wpprobe",
3331
Short: "A fast WordPress plugin enumeration tool",
3432
Long: `WPProbe is a high-speed WordPress plugin scanner that detects installed plugins and checks for known vulnerabilities using the Wordfence database.`,
35-
Version: version,
33+
Version: utils.Version,
3634
}
3735

3836
func Execute() {
39-
_, isLatest := utils.CheckLatestVersion(version)
40-
utils.DefaultLogger.PrintBanner(version, isLatest)
37+
_, isLatest := utils.CheckLatestVersion(utils.Version)
38+
utils.DefaultLogger.PrintBanner(utils.Version, isLatest)
4139

4240
if err := rootCmd.Execute(); err != nil {
4341
utils.DefaultLogger.Error(err.Error())
@@ -50,7 +48,7 @@ func init() {
5048
rootCmd.AddCommand(updateCmd)
5149
rootCmd.AddCommand(updateDbCmd)
5250

53-
rootCmd.SetVersionTemplate("WPProbe version {{.Version}}\n")
51+
rootCmd.SetVersionTemplate("WPProbe version {{.utils.Version}}\n")
5452

5553
rootCmd.SilenceErrors = true
5654

cmd/update.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var updateCmd = &cobra.Command{
2828
Use: "update",
2929
Short: "Update WPProbe to the latest version",
3030
Run: func(cmd *cobra.Command, args []string) {
31-
if err := utils.AutoUpdate(); err != nil {
31+
current := utils.Version
32+
if err := utils.AutoUpdate(current); err != nil {
3233
utils.DefaultLogger.Error("Update failed: " + err.Error())
3334
} else {
3435
utils.DefaultLogger.Success("Update completed successfully!")

internal/utils/update.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030

3131
const githubRepo = "Chocapikk/wpprobe"
3232

33+
var Version = "dev"
34+
3335
var exitFunc = os.Exit
3436

3537
var GitHubLatestReleaseURL = func() string {
@@ -59,7 +61,7 @@ func getLatestVersion() (string, error) {
5961
DefaultLogger.Error("Failed to fetch latest release: " + err.Error())
6062
return "", err
6163
}
62-
defer func() { _ = resp.Body.Close() }()
64+
defer resp.Body.Close()
6365

6466
if resp.StatusCode != http.StatusOK {
6567
DefaultLogger.Error(fmt.Sprintf("GitHub API error: %d", resp.StatusCode))
@@ -82,25 +84,32 @@ func getLatestVersion() (string, error) {
8284
return version, nil
8385
}
8486

85-
func AutoUpdate() error {
87+
func AutoUpdate(currentVersion string) error {
8688
DefaultLogger.Info("Checking for WPProbe updates...")
8789

88-
version, err := getLatestVersion()
90+
latest, err := getLatestVersion()
8991
if err != nil {
9092
return err
9193
}
9294

95+
if currentVersion == latest {
96+
DefaultLogger.Info(
97+
fmt.Sprintf("WPProbe is already up-to-date (version %s)", currentVersion),
98+
)
99+
return nil
100+
}
101+
93102
osName := detectOS()
94103
arch := detectArch()
95-
updateURL := GitHubDownloadURL(version, osName, arch)
104+
updateURL := GitHubDownloadURL(latest, osName, arch)
96105

97106
DefaultLogger.Info("Downloading WPProbe update from: " + updateURL)
98107
resp, err := http.Get(updateURL)
99108
if err != nil {
100109
DefaultLogger.Error("Failed to download update: " + err.Error())
101110
return err
102111
}
103-
defer func() { _ = resp.Body.Close() }()
112+
defer resp.Body.Close()
104113

105114
if resp.StatusCode != http.StatusOK {
106115
DefaultLogger.Error(fmt.Sprintf("Update not found: %s", updateURL))
@@ -145,10 +154,6 @@ func AutoUpdate() error {
145154
return nil
146155
}
147156

148-
func detectOS() string {
149-
return runtime.GOOS
150-
}
157+
func detectOS() string { return runtime.GOOS }
151158

152-
func detectArch() string {
153-
return runtime.GOARCH
154-
}
159+
func detectArch() string { return runtime.GOARCH }

internal/utils/update_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestAutoUpdate(t *testing.T) {
126126
http.NotFound(w, r)
127127
}
128128
}))
129-
defer func() { mockServer.Close() }()
129+
defer mockServer.Close()
130130

131131
originalGitHubLatest := GitHubLatestReleaseURL
132132
originalGitHubDownload := GitHubDownloadURL
@@ -166,7 +166,7 @@ func TestAutoUpdate(t *testing.T) {
166166
os.Args[0] = tmpFile.Name()
167167
defer func() { os.Args[0] = originalArg0 }()
168168

169-
if err := AutoUpdate(); err != nil {
169+
if err := AutoUpdate(Version); err != nil {
170170
t.Errorf("AutoUpdate() error = %v, want nil", err)
171171
}
172172
if !exitCalled {
@@ -185,7 +185,7 @@ func TestAutoUpdate_NotFound(t *testing.T) {
185185

186186
GitHubLatestReleaseURL = func() string { return mockServer.URL + "/repos/Chocapikk/wpprobe/releases/latest" }
187187

188-
err := AutoUpdate()
188+
err := AutoUpdate(Version)
189189
if err == nil {
190190
t.Error("Expected error for 404, got nil")
191191
}

0 commit comments

Comments
 (0)