-
Notifications
You must be signed in to change notification settings - Fork 70
/
version.go
176 lines (138 loc) · 4.96 KB
/
version.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cli
import (
"context"
"fmt"
"os"
"runtime"
"strings"
"github.com/Masterminds/semver/v3"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/utils"
)
var (
// Represents the currently installed/compiled version of the CLI
version string
// Represents the latest release version per the GitHub repository's latest release
latestVersion string
)
const installCLISnippetLinux = `curl -Ls https://download.newrelic.com/install/newrelic-cli/scripts/install.sh | bash`
const installCLISnippetWindows = `[Net.ServicePointManager]::SecurityProtocol = 'tls12, tls'; (New-Object System.Net.WebClient).DownloadFile(\"https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1\", \"$env:TEMP\\install.ps1\"); &$env:TEMP\\install.ps1;`
// NewRelicCLILatestReleaseURL is the URL used to fetch the latest release data utilizing GitHub's API.
const NewRelicCLILatestReleaseURL string = "https://download.newrelic.com/install/newrelic-cli/currentVersion.txt"
// UpdateVersionMsgFormat is the message displayed to a user when an older
// version of the CLI is installed.
const UpdateVersionMsgFormat string = `
We need to update your New Relic CLI version to continue.
Installed version: %s
Latest version: %s
To update your CLI and continue this installation, run this command:
%s
`
// Version returns the version of the CLI that's currently installed.
func Version() string {
if version == "" {
version = os.Getenv("NEW_RELIC_CLI_VERSION")
}
return version
}
// IsLatestVersion returns true if the provided version string matches
// the current installed version.
func IsLatestVersion(ctx context.Context, latestVersion string) (bool, error) {
installedVersion, err := semver.NewVersion(Version())
if err != nil {
return false, fmt.Errorf("error parsing installed CLI version %s: %s", version, err.Error())
}
lv, err := semver.NewVersion(latestVersion)
if err != nil {
return false, fmt.Errorf("error parsing version to check %s: %s", latestVersion, err.Error())
}
if installedVersion.LessThan(lv) {
return false, nil
}
return installedVersion.Equal(lv), nil
}
// GetLatestReleaseVersion returns the latest released tag.
// The latest tag is pulled from the newrelic-cli GitHub repository.
func GetLatestReleaseVersion(ctx context.Context) (string, error) {
if latestVersion != "" {
return latestVersion, nil
}
latestRelease, err := fetchLatestRelease(ctx)
if err != nil {
return "", fmt.Errorf("error fetching latest release: %w", err)
}
lv, err := semver.NewVersion(latestRelease)
if err != nil {
return "", fmt.Errorf("error parsing latest release version string '%s': %w", latestRelease, err)
}
// Cache the latest version string
versionString := lv.String()
setLatestVersion(versionString)
return versionString, nil
}
// IsDevEnvironment is a naive implementation to determine if the CLI
// is being run in a dev environment. IsDevEnvironment returns true when
// the installed CLI version is either in a prerelease state or in a dirty state.
// The version string is generated at compile time using git. The prerelease string
// is appended to the primary semver version string.
//
// If you're doing local development on the CLI, your version may look similar to
// the examples below.
//
// Examples of versions that have a prerelease tag (i.e. the suffix):
//
// v0.32.1-10-gbe63a24
// v0.32.1-10-gbe63a24-dirty
//
// In this example version string, "10" represents the number of commits
// since the 0.32.1 tag was created. The "gbe63a24" is the previous commit's
// abbreviated sha. The "dirty" part means that git was in a dirty state at compile
// time, meaning an updated file was saved, but not yet committed.
func IsDevEnvironment() bool {
v, err := semver.NewVersion(version)
if err != nil {
return true
}
prereleaseString := v.Prerelease()
if prereleaseString == "" {
return false
}
if strings.Contains(prereleaseString, "dirty") {
return true
}
hasDevVersionSuffix := len(strings.Split(prereleaseString, "-")) > 1
return hasDevVersionSuffix
}
func PrintUpdateCLIMessage(latestReleaseVersion string) {
output.Printf("%s", FormatUpdateVersionMessage(latestReleaseVersion))
}
func FormatUpdateVersionMessage(latestReleaseVersion string) string {
snippet := installCLISnippetLinux
if isWindowsOS() {
snippet = installCLISnippetWindows
}
return fmt.Sprintf(UpdateVersionMsgFormat, version, latestReleaseVersion, snippet)
}
func setLatestVersion(v string) {
latestVersion = v
}
func isWindowsOS() bool {
return runtime.GOOS == "windows"
}
func fetchLatestRelease(ctx context.Context) (string, error) {
client := utils.NewHTTPClient("")
respBytes, err := client.Get(ctx, NewRelicCLILatestReleaseURL)
if err != nil {
return "", err
}
gitTag := strings.TrimSpace(string(respBytes))
_, err = semver.NewVersion(gitTag)
if err != nil {
return "", err
}
log.WithFields(log.Fields{
"tag": gitTag,
}).Debug("fetch tag success")
return gitTag, nil
}