github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/common/cloudcontext.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "regexp" 6 "strings" 7 8 "github.com/spf13/cobra" 9 10 "github.com/kubeshop/testkube/cmd/kubectl-testkube/config" 11 "github.com/kubeshop/testkube/pkg/ui" 12 "github.com/kubeshop/testkube/pkg/utils/text" 13 ) 14 15 func UiPrintContext(cfg config.Data) { 16 ui.Warn("Your current context is set to", string(cfg.ContextType)) 17 ui.NL() 18 19 if cfg.ContextType == config.ContextTypeCloud { 20 contextData := map[string]string{ 21 "Organization ": cfg.CloudContext.OrganizationName + ui.DarkGray(" ("+cfg.CloudContext.OrganizationId+")"), 22 "Environment ": cfg.CloudContext.EnvironmentName + ui.DarkGray(" ("+cfg.CloudContext.EnvironmentId+")"), 23 "API Key ": text.Obfuscate(cfg.CloudContext.ApiKey), 24 "API URI ": cfg.CloudContext.ApiUri, 25 "Namespace ": cfg.Namespace, 26 } 27 28 // add agent information only when need to change agent data, it's usually not needed in usual workflow 29 if ui.Verbose { 30 contextData["Agent Key"] = text.Obfuscate(cfg.CloudContext.AgentKey) 31 contextData["Agent URI"] = cfg.CloudContext.AgentUri 32 } 33 34 ui.InfoGrid(contextData) 35 } else { 36 ui.InfoGrid(map[string]string{ 37 "Namespace ": cfg.Namespace, 38 "Telemetry Enabled": fmt.Sprintf("%t", cfg.TelemetryEnabled), 39 }) 40 } 41 } 42 43 func UiCloudContextValidationError(err error) { 44 ui.NL() 45 ui.Errf("Validating cloud context failed: %s", err.Error()) 46 ui.NL() 47 ui.Info("Please set valid cloud context using `testkube set context` with valid values") 48 ui.NL() 49 ui.ShellCommand(" testkube set context -c cloud -e tkcenv_XXX -o tkcorg_XXX -k tkcapi_XXX") 50 ui.NL() 51 } 52 53 func UiContextHeader(cmd *cobra.Command, cfg config.Data) { 54 // only show header when output is pretty 55 if cmd.Flag("output") != nil && cmd.Flag("output").Value.String() != "pretty" { 56 return 57 } 58 59 header := "\n" 60 separator := " " 61 62 orgName := cfg.CloudContext.OrganizationName 63 if orgName == "" { 64 orgName = cfg.CloudContext.OrganizationId 65 } 66 envName := cfg.CloudContext.EnvironmentName 67 if envName == "" { 68 envName = cfg.CloudContext.EnvironmentId 69 } 70 71 if cfg.ContextType == config.ContextTypeCloud { 72 header += ui.DarkGray("Context: ") + ui.White(cfg.ContextType) + ui.DarkGray(" ("+Version+")") + separator 73 header += ui.DarkGray("Namespace: ") + ui.White(cfg.Namespace) + separator 74 header += ui.DarkGray("Org: ") + ui.White(orgName) + separator 75 header += ui.DarkGray("Env: ") + ui.White(envName) 76 } else { 77 header += ui.DarkGray("Context: ") + ui.White(cfg.ContextType) + ui.DarkGray(" ("+Version+")") + separator 78 header += ui.DarkGray("Namespace: ") + ui.White(cmd.Flag("namespace").Value.String()) 79 } 80 81 fmt.Println(header) 82 fmt.Println(strings.Repeat("-", calculateStringSize(header))) 83 } 84 85 // calculateStringSize calculates the length of a string, excluding shell color codes. 86 func calculateStringSize(s string) int { 87 // Regular expression to match ANSI escape codes. 88 re := regexp.MustCompile(`\x1b[^m]*m`) 89 // Remove the escape codes from the string. 90 s = re.ReplaceAllString(s, "") 91 // Return the length of the string. 92 return len(s) - 1 93 }