Skip to content

Commit 922813e

Browse files
authored
refactor: remove 'version.Get()' and only use 'version.Raw()' (#445)
* refactor: remove version.Get() and use only version.Raw() Move the v-prefix enforcement from Get() into init(), ensuring Version is normalized at startup. This eliminates the confusing Get() vs Raw() distinction — Raw() is now the single way to access the version. * refactor: use strings.HasPrefix instead of regexp in ensurePrefix
1 parent 7514e97 commit 922813e

7 files changed

Lines changed: 19 additions & 23 deletions

File tree

cmd/doctor/doctor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
)
3939

4040
func TestDoctorCommand(t *testing.T) {
41-
expectedCLIVersion := version.Get()
41+
expectedCLIVersion := version.Raw()
4242
expectedCredentials := types.SlackAuth{
4343
TeamDomain: "team123",
4444
TeamID: "T123",

cmd/root.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ func NewRootCommand(clients *shared.ClientFactory, updateNotification *update.Up
125125
})
126126

127127
// Check for an CLI update in the background while the command runs
128-
updateNotification = update.New(clients, version.Get(), "SLACK_SKIP_UPDATE")
128+
updateNotification = update.New(clients, version.Raw(), "SLACK_SKIP_UPDATE")
129129
updateNotification.CheckForUpdateInBackground(ctx, false)
130130
return nil
131131
},
132132
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
133133
// TODO: since commands are moving to `*E` cobra lifecycle methods, this method may not be invoked if those earlier lifecycle methods return an error. Maybe move this to the cleanup() method below? but maybe this is OK, no need to prompt users to update if they encounter an error?
134134
// when the command is `slack update`
135-
return updateNotification.PrintAndPromptUpdates(cmd, version.Get())
135+
return updateNotification.PrintAndPromptUpdates(cmd, version.Raw())
136136
},
137137
RunE: func(cmd *cobra.Command, args []string) error {
138138
return cmd.Help()
@@ -153,7 +153,7 @@ func Init(ctx context.Context) (*cobra.Command, *shared.ClientFactory) {
153153

154154
// Support `--version` by setting root command's `Version` and custom template.
155155
// Add a newline to `SetVersionTemplate` to display correctly on terminals.
156-
rootCmd.Version = version.Get()
156+
rootCmd.Version = version.Raw()
157157
rootCmd.SetVersionTemplate(versioncmd.Template() + "\n")
158158

159159
// Add subcommands (each subcommand may add their own child subcommands)

cmd/upgrade/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
5555
// When there are updates, the function will *not* print a message because the root command handles printing update notifications.
5656
func checkForUpdates(clients *shared.ClientFactory, cmd *cobra.Command) error {
5757
ctx := cmd.Context()
58-
updateNotification := update.New(clients, version.Get(), "SLACK_SKIP_UPDATE")
58+
updateNotification := update.New(clients, version.Raw(), "SLACK_SKIP_UPDATE")
5959

6060
// TODO(@mbrooks) This update check is happening at the same time as the root command's `CheckForUpdateInBackground`.
6161
// The difference between the two is that this update check is forced while the root command runs every 24 hours.

cmd/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
8181

8282
func Template() string {
8383
processName := cmdutil.GetProcessName()
84-
version := version.Get()
84+
version := version.Raw()
8585

8686
return fmt.Sprintf(style.Secondary("Using %s %s"), processName, version)
8787
}

internal/pkg/auth/login.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func createNewAuth(ctx context.Context, apiClient api.APIInterface, authClient a
147147
return types.SlackAuth{}, "", err
148148
}
149149

150-
authExchangeRes, err := apiClient.ExchangeAuthTicket(ctx, authTicket, challengeCode, version.Get())
150+
authExchangeRes, err := apiClient.ExchangeAuthTicket(ctx, authTicket, challengeCode, version.Raw())
151151
if err != nil {
152152
return types.SlackAuth{}, "", err
153153
}
@@ -162,7 +162,7 @@ func requestAuthTicket(ctx context.Context, apiClient api.APIInterface, io iostr
162162
span, ctx = opentracing.StartSpanFromContext(ctx, "requestAuthTicket")
163163
defer span.Finish()
164164

165-
cliVersion := semver.MajorMinor(version.Get())
165+
cliVersion := semver.MajorMinor(version.Raw())
166166

167167
// Get a ticket from Slack
168168
if authTicketResult, err := apiClient.GenerateAuthTicket(ctx, cliVersion, noRotation); err != nil {
@@ -246,7 +246,7 @@ func LoginNoPrompt(ctx context.Context, clients *shared.ClientFactory, ticketArg
246246

247247
// existing ticket request, try to exchange
248248
if ticketArg != "" && challengeCodeArg != "" {
249-
authExchangeRes, err := clients.API().ExchangeAuthTicket(ctx, ticketArg, challengeCodeArg, version.Get())
249+
authExchangeRes, err := clients.API().ExchangeAuthTicket(ctx, ticketArg, challengeCodeArg, version.Raw())
250250
if err != nil || !authExchangeRes.IsReady {
251251
return types.SlackAuth{}, "", err
252252
}

internal/version/version.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package version
1616

1717
import (
1818
"os"
19-
"regexp"
2019
"strings"
2120
)
2221

@@ -33,23 +32,24 @@ func init() {
3332
if envVersion := getVersionFromEnv(); envVersion != "" {
3433
Version = envVersion
3534
}
35+
Version = ensurePrefix(Version)
3636
}
3737

3838
// getVersionFromEnv will return the formatted version from EnvTestVersion otherwise "".
3939
func getVersionFromEnv() string {
4040
return strings.Trim(os.Getenv(EnvTestVersion), " ")
4141
}
4242

43-
// Get the version and format it (e.g. `v1.0.0`)
44-
func Get() string {
45-
version := Version
46-
if match, _ := regexp.MatchString(`^[^v]`, version); match {
47-
version = "v" + version
43+
// ensurePrefix ensures that the version string has a "v" prefix.
44+
// Empty strings are returned as-is to avoid producing a bare "v".
45+
func ensurePrefix(v string) string {
46+
if v != "" && !strings.HasPrefix(v, "v") {
47+
return "v" + v
4848
}
49-
return version
49+
return v
5050
}
5151

52-
// Raw returns the raw, unformatted version
52+
// Raw returns the version
5353
func Raw() string {
5454
return Version
5555
}

internal/version/version_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ func TestVersion(t *testing.T) {
2727
assert.True(t, len(v) > 0, "some default value exists")
2828
}
2929

30-
// Test overriding the Version with an environment variable
31-
func Test_Get(t *testing.T) {
30+
func Test_ensurePrefix(t *testing.T) {
3231
tests := map[string]struct {
3332
version string
3433
expected string
@@ -52,10 +51,7 @@ func Test_Get(t *testing.T) {
5251
}
5352
for name, tc := range tests {
5453
t.Run(name, func(t *testing.T) {
55-
original := Version
56-
defer func() { Version = original }()
57-
Version = tc.version
58-
assert.Equal(t, tc.expected, Get())
54+
assert.Equal(t, tc.expected, ensurePrefix(tc.version))
5955
})
6056
}
6157
}

0 commit comments

Comments
 (0)