Skip to content

Commit 4c82173

Browse files
authored
Merge pull request #122 from vitessio/version-check
Check the version of the tool at start time
2 parents 0345f81 + 8d7f11d commit 4c82173

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/charmbracelet/bubbletea v0.24.1
88
github.com/charmbracelet/lipgloss v0.9.1
99
github.com/cli/go-gh v1.2.1
10-
github.com/hashicorp/go-version v1.6.0
10+
github.com/hashicorp/go-version v1.7.0
1111
github.com/spf13/cobra v1.8.0
1212
)
1313

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
2424
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2525
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
2626
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
27-
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
28-
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
27+
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
28+
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
2929
github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs=
3030
github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
3131
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=

go/cmd/cmd.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
"github.com/vitessio/vitess-releaser/go/releaser/utils"
3535
)
3636

37-
const VERSION = "v1.0"
37+
const VERSION = "v1.0.2"
3838

3939
var (
4040
releaseVersion string
@@ -73,7 +73,7 @@ func init() {
7373
rootCmd.PersistentFlags().IntVarP(&rcIncrement, flags.RCIncrement, "", 0, "Define the release as an RC release, value is used to determine the number of the RC.")
7474
rootCmd.PersistentFlags().StringVarP(&releaseVersion, flags.MajorRelease, "r", "", "Number of the major release on which we want to create a new release.")
7575
rootCmd.PersistentFlags().StringVarP(&vtopReleaseVersion, flags.VtOpRelease, "", "", "Number of the major and minor release on which we want to create a new release, i.e. '2.11', leave empty for no vtop release.")
76-
rootCmd.PersistentFlags().BoolVarP(&version, "version", "v", false, "Prints the version and git commit hash.")
76+
rootCmd.PersistentFlags().BoolVarP(&version, "version", "v", false, "Prints the version.")
7777

7878
err := cobra.MarkFlagRequired(rootCmd.PersistentFlags(), flags.MajorRelease)
7979
if err != nil {
@@ -223,9 +223,7 @@ func getGitRepos() (vitessRepo, vtopRepo string) {
223223
}
224224

225225
func printVersionAndExit() {
226-
commit, shortHash := getGitCommit()
227-
fmt.Printf("Version: %s.%s\n", VERSION, shortHash)
228-
fmt.Printf("Last Commit: %s\n", commit)
226+
fmt.Printf("Version: %s\n", VERSION)
229227
os.Exit(0)
230228
}
231229

main.go

+66-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,76 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
21-
"github.com/vitessio/vitess-releaser/go/cmd"
22+
"log"
23+
"net/http"
2224
"os"
2325
"os/exec"
2426
"runtime/debug"
27+
"time"
28+
29+
"github.com/hashicorp/go-version"
30+
"github.com/vitessio/vitess-releaser/go/cmd"
2531
)
2632

33+
// Struct to hold the GitHub API response
34+
type githubRelease struct {
35+
TagName string `json:"tag_name"`
36+
}
37+
38+
// getLatestVersionFromGitHub queries the GitHub API for the latest release version
39+
func getLatestVersionFromGitHub() (string, error) {
40+
const url = "https://api.github.com/repos/vitessio/vitess-releaser/releases/latest"
41+
42+
client := &http.Client{
43+
Timeout: 10 * time.Second,
44+
}
45+
46+
resp, err := client.Get(url)
47+
if err != nil {
48+
return "", fmt.Errorf("error fetching latest release info: %w", err)
49+
}
50+
defer resp.Body.Close()
51+
52+
if resp.StatusCode != http.StatusOK {
53+
return "", fmt.Errorf("failed to fetch latest version, status code: %d", resp.StatusCode)
54+
}
55+
56+
var release githubRelease
57+
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
58+
return "", fmt.Errorf("error decoding response: %w", err)
59+
}
60+
61+
return release.TagName, nil
62+
}
63+
64+
// compareVersions checks if the current version is the latest and prints a message
65+
func compareVersions() {
66+
latestVersionStr, err := getLatestVersionFromGitHub()
67+
if err != nil {
68+
fmt.Println("Could not check for updates:", err.Error())
69+
os.Exit(1)
70+
}
71+
72+
currentVersion, err := version.NewVersion(cmd.VERSION[1:])
73+
if err != nil {
74+
log.Fatalf("Error parsing current version: %v", err)
75+
}
76+
77+
latestVersion, err := version.NewVersion(latestVersionStr[1:])
78+
if err != nil {
79+
log.Fatalf("Error parsing latest version: %v", err)
80+
}
81+
82+
if currentVersion.LessThan(latestVersion) {
83+
fmt.Printf("A new version of the tool is available: %s (you have %s)\n", latestVersionStr, cmd.VERSION)
84+
fmt.Println("Please update to the latest version.")
85+
fmt.Println("\n\tgo install github.com/vitessio/vitess-releaser@latest\n")
86+
os.Exit(1)
87+
}
88+
}
89+
2790
// On some shells the terminal is left in a bad state possibly because the debug output is large or
2891
// it contains control characters. This function restores the terminal to a sane state on a panic.
2992
func restoreTerminal() {
@@ -43,5 +106,7 @@ func main() {
43106
}
44107
}()
45108

109+
compareVersions()
110+
46111
cmd.Execute()
47112
}

0 commit comments

Comments
 (0)