Skip to content

Commit 3a1703f

Browse files
authored
Print a warning if you login but have env set (#358)
1 parent 6dec52a commit 3a1703f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

internal/cmd/profile/login_command.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func loginCommand() *cli.Command {
3838
}
3939

4040
func loginAction(ctx context.Context, cliCmd *cli.Command) error {
41+
defer printEnvWarning()
42+
4143
var storedCredentials session.StoredCredentials
4244

4345
// Let's try to re-authenticate user.
@@ -211,3 +213,39 @@ func persistAccessCredentials(creds *session.StoredCredentials) error {
211213
Credentials: creds,
212214
})
213215
}
216+
217+
func printEnvWarning() {
218+
envVars := checkEnvironmentVariables()
219+
if len(envVars) == 0 {
220+
return
221+
}
222+
223+
fmt.Println("WARNING: The following Spacelift environment variables are set:")
224+
for _, envVar := range envVars {
225+
fmt.Printf(" - %s\n", envVar)
226+
}
227+
fmt.Println("\nEnvironment variables take precedence over profile credentials.")
228+
fmt.Println("If these credentials are expired or invalid, commands will fail even after successful login.")
229+
}
230+
231+
// checkEnvironmentVariables checks for Spacelift environment variables and returns a list of those that are set.
232+
func checkEnvironmentVariables() []string {
233+
var envVars []string
234+
235+
checkVars := []string{
236+
session.EnvSpaceliftAPIToken,
237+
session.EnvSpaceliftAPIKeyEndpoint,
238+
session.EnvSpaceliftAPIEndpoint,
239+
session.EnvSpaceliftAPIGitHubToken,
240+
session.EnvSpaceliftAPIKeyID,
241+
session.EnvSpaceliftAPIKeySecret,
242+
}
243+
244+
for _, envVar := range checkVars {
245+
if value := os.Getenv(envVar); value != "" {
246+
envVars = append(envVars, envVar)
247+
}
248+
}
249+
250+
return envVars
251+
}

0 commit comments

Comments
 (0)