-
Notifications
You must be signed in to change notification settings - Fork 32
refactor: remove 'version.Get()' and only use 'version.Raw()' #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,6 @@ package version | |
|
|
||
| import ( | ||
| "os" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
|
|
@@ -33,23 +32,24 @@ func init() { | |
| if envVersion := getVersionFromEnv(); envVersion != "" { | ||
| Version = envVersion | ||
| } | ||
| Version = ensurePrefix(Version) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: We now enforce the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🦠 thought: I like this approach! It keeps the |
||
| } | ||
|
|
||
| // getVersionFromEnv will return the formatted version from EnvTestVersion otherwise "". | ||
| func getVersionFromEnv() string { | ||
| return strings.Trim(os.Getenv(EnvTestVersion), " ") | ||
| } | ||
|
|
||
| // Get the version and format it (e.g. `v1.0.0`) | ||
| func Get() string { | ||
| version := Version | ||
| if match, _ := regexp.MatchString(`^[^v]`, version); match { | ||
| version = "v" + version | ||
| // ensurePrefix ensures that the version string has a "v" prefix. | ||
| // Empty strings are returned as-is to avoid producing a bare "v". | ||
| func ensurePrefix(v string) string { | ||
| if v != "" && !strings.HasPrefix(v, "v") { | ||
| return "v" + v | ||
| } | ||
| return version | ||
| return v | ||
| } | ||
|
|
||
| // Raw returns the raw, unformatted version | ||
| // Raw returns the version | ||
| func Raw() string { | ||
| return Version | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,8 +27,7 @@ func TestVersion(t *testing.T) { | |
| assert.True(t, len(v) > 0, "some default value exists") | ||
| } | ||
|
|
||
| // Test overriding the Version with an environment variable | ||
| func Test_Get(t *testing.T) { | ||
| func Test_ensurePrefix(t *testing.T) { | ||
| tests := map[string]struct { | ||
| version string | ||
| expected string | ||
|
|
@@ -52,10 +51,7 @@ func Test_Get(t *testing.T) { | |
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| original := Version | ||
| defer func() { Version = original }() | ||
| Version = tc.version | ||
| assert.Equal(t, tc.expected, Get()) | ||
|
Comment on lines
-55
to
-58
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🪓 praise: Huge improvement IMHO! |
||
| assert.Equal(t, tc.expected, ensurePrefix(tc.version)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎁 suggestion(non-blocking): In later changes to
upgradedo we want to preferslackcontextpackages instead of importingversionhere?slack-cli/internal/slackcontext/slackcontext.go
Lines 174 to 184 in e114959
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion. During this PR, I was surprised to see so many
version.Raw()calls still remain. In the follow-up PR, I'll aim to replace these - it's less dangerous now thatslackcontext.Version(ctx)fallsback on the raw version. 👍🏻