diff --git a/cmd/instance_get.go b/cmd/instance_get.go index ed13dac..314d644 100644 --- a/cmd/instance_get.go +++ b/cmd/instance_get.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "net/url" "strconv" "strings" @@ -9,6 +10,24 @@ import ( "github.com/spf13/cobra" ) +func maskPassword(urlStr string) string { + parsedURL, err := url.Parse(urlStr) + if err != nil { + return urlStr + } + + if parsedURL.User == nil { + return urlStr + } + + password, hasPassword := parsedURL.User.Password() + if !hasPassword || password == "" { + return urlStr + } + + return strings.Replace(urlStr, password, "****", 1) +} + var instanceGetCmd = &cobra.Command{ Use: "get --id ", Short: "Get details of a specific CloudAMQP instance", @@ -44,6 +63,14 @@ var instanceGetCmd = &cobra.Command{ fmt.Printf("Plan = %s\n", instance.Plan) fmt.Printf("Region = %s\n", instance.Region) fmt.Printf("Tags = %s\n", strings.Join(instance.Tags, ",")) + + showURL, _ := cmd.Flags().GetBool("show-url") + if showURL { + fmt.Printf("URL = %s\n", instance.URL) + } else { + fmt.Printf("URL = %s\n", maskPassword(instance.URL)) + } + fmt.Printf("Hostname = %s\n", instance.HostnameExternal) ready := "No" if instance.Ready { @@ -58,5 +85,6 @@ var instanceGetCmd = &cobra.Command{ func init() { instanceGetCmd.Flags().StringP("id", "", "", "Instance ID (required)") instanceGetCmd.MarkFlagRequired("id") + instanceGetCmd.Flags().BoolP("show-url", "", false, "Show full connection URL with credentials") instanceGetCmd.RegisterFlagCompletionFunc("id", completeInstanceIDFlag) }