Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 52 additions & 44 deletions tool/tsh/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -1589,11 +1589,8 @@ func onLogin(cf *CLIConf) error {
if err := updateKubeConfigOnLogin(cf, tc, ""); 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) &&
Expand Down Expand Up @@ -1797,8 +1794,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)
}

Expand Down Expand Up @@ -3836,6 +3838,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 {
Expand All @@ -3851,19 +3896,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 {
Expand Down Expand Up @@ -4046,32 +4080,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
Expand Down