diff --git a/tool/tsh/tsh.go b/tool/tsh/tsh.go index 2675393aac50a..5eb85f35397c6 100644 --- a/tool/tsh/tsh.go +++ b/tool/tsh/tsh.go @@ -1672,11 +1672,8 @@ func onLogin(cf *CLIConf) error { if err := updateKubeConfigOnLogin(cf, tc, updateKubeConfigOption); err != nil { return trace.Wrap(err) } - env := getTshEnv() - active, others := makeAllProfileInfo(profile, profiles, env) - printProfiles(cf.Debug, active, others, env, cf.Verbose) - return nil + return trace.Wrap(printProfiles(cf, profile, profiles)) // if the proxy names match but nothing else is specified; show motd and update active profile and kube configs case host(cf.Proxy) == host(profile.ProxyURL.Host) && @@ -1880,8 +1877,13 @@ func onLogin(cf *CLIConf) error { webProxyHost, _ := tc.WebProxyHostPort() cf.Proxy = webProxyHost + profile, profiles, err = cf.FullProfileStatus() + if err != nil { + return trace.Wrap(err) + } + // Print status to show information of the logged in user. - if err := onStatus(cf); err != nil { + if err := printProfiles(cf, profile, profiles); err != nil { return trace.Wrap(err) } @@ -3923,6 +3925,49 @@ func printStatus(debug bool, p *profileInfo, env map[string]string, isActive boo fmt.Printf("\n") } +// printProfiles displays the provided profile information +// to the user. +func printProfiles(cf *CLIConf, profile *client.ProfileStatus, profiles []*client.ProfileStatus) error { + env := getTshEnv() + active, others := makeAllProfileInfo(profile, profiles, env) + + format := strings.ToLower(cf.Format) + switch format { + case teleport.JSON, teleport.YAML: + out, err := serializeProfiles(active, others, env, format) + if err != nil { + return trace.Wrap(err) + } + fmt.Println(out) + default: + if profile == nil && len(profiles) == 0 { + return nil + } + + // Print the active profile. + if profile != nil { + printStatus(cf.Debug, active, env, true) + } + + // Print all other profiles. + for _, p := range others { + printStatus(cf.Debug, p, env, false) + } + + // Print relevant active env vars, if they are set. + if cf.Verbose { + if len(env) > 0 { + fmt.Println("Active Environment:") + } + for k, v := range env { + fmt.Printf("\t%s=%s\n", k, v) + } + } + } + + return nil +} + // onStatus command shows which proxy the user is logged into and metadata // about the certificate. func onStatus(cf *CLIConf) error { @@ -3938,19 +3983,8 @@ func onStatus(cf *CLIConf) error { return trace.Wrap(err) } - env := getTshEnv() - active, others := makeAllProfileInfo(profile, profiles, env) - - format := strings.ToLower(cf.Format) - switch format { - case teleport.JSON, teleport.YAML: - out, err := serializeProfiles(active, others, env, format) - if err != nil { - return trace.Wrap(err) - } - fmt.Println(out) - default: - printProfiles(cf.Debug, active, others, env, cf.Verbose) + if err := printProfiles(cf, profile, profiles); err != nil { + return trace.Wrap(err) } if profile == nil { @@ -4133,32 +4167,6 @@ func getTshEnv() map[string]string { return env } -func printProfiles(debug bool, profile *profileInfo, profiles []*profileInfo, env map[string]string, verbose bool) { - if profile == nil && len(profiles) == 0 { - return - } - - // Print the active profile. - if profile != nil { - printStatus(debug, profile, env, true) - } - - // Print all other profiles. - for _, p := range profiles { - printStatus(debug, p, env, false) - } - - // Print relevant active env vars, if they are set. - if verbose { - if len(env) > 0 { - fmt.Println("Active Environment:") - } - for k, v := range env { - fmt.Printf("\t%s=%s\n", k, v) - } - } -} - // host is a utility function that extracts // host from the host:port pair, in case of any error // returns the original value