-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (91 loc) · 2.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"runtime/debug"
"time"
"github.com/hashicorp/go-version"
"github.com/vitessio/vitess-releaser/go/cmd"
)
// Struct to hold the GitHub API response
type githubRelease struct {
TagName string `json:"tag_name"`
}
// getLatestVersionFromGitHub queries the GitHub API for the latest release version
func getLatestVersionFromGitHub() (string, error) {
const url = "https://api.github.com/repos/vitessio/vitess-releaser/releases/latest"
client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Get(url)
if err != nil {
return "", fmt.Errorf("error fetching latest release info: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to fetch latest version, status code: %d", resp.StatusCode)
}
var release githubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", fmt.Errorf("error decoding response: %w", err)
}
return release.TagName, nil
}
// compareVersions checks if the current version is the latest and prints a message
func compareVersions() {
latestVersionStr, err := getLatestVersionFromGitHub()
if err != nil {
fmt.Println("Could not check for updates:", err.Error())
os.Exit(1)
}
currentVersion, err := version.NewVersion(cmd.VERSION[1:])
if err != nil {
log.Fatalf("Error parsing current version: %v", err)
}
latestVersion, err := version.NewVersion(latestVersionStr[1:])
if err != nil {
log.Fatalf("Error parsing latest version: %v", err)
}
if currentVersion.LessThan(latestVersion) {
fmt.Printf("A new version of the tool is available: %s (you have %s)\n", latestVersionStr, cmd.VERSION)
fmt.Println("Please update to the latest version.")
fmt.Println("\n\tgo install github.com/vitessio/vitess-releaser@latest\n")
os.Exit(1)
}
}
// On some shells the terminal is left in a bad state possibly because the debug output is large or
// it contains control characters. This function restores the terminal to a sane state on a panic.
func restoreTerminal() {
cmdSane := exec.Command("stty", "sane")
cmdSane.Stdin = os.Stdin
if err := cmdSane.Run(); err != nil {
fmt.Println("Failed to restore terminal to sane state:", err)
}
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Vitess Releaser Panic:\n", r)
debug.PrintStack()
restoreTerminal()
}
}()
compareVersions()
cmd.Execute()
}