From c2c2ab4299bbb243c55644984392f1c39af499cf Mon Sep 17 00:00:00 2001 From: Aditya Manthramurthy Date: Wed, 10 Aug 2022 17:30:48 -0700 Subject: [PATCH] Make environment variables output highlighted (#4205) For `mc admin config get|export` --- cmd/admin-config-export.go | 31 ++++++++++++++++++++++++++++++- cmd/admin-config-get.go | 29 ++++++++++++++++++++++++++++- cmd/print.go | 1 + 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/cmd/admin-config-export.go b/cmd/admin-config-export.go index 3c95c413..572f662d 100644 --- a/cmd/admin-config-export.go +++ b/cmd/admin-config-export.go @@ -18,9 +18,17 @@ package cmd import ( + "bufio" + "bytes" + "fmt" + "io" + "strings" + + "github.com/fatih/color" "github.com/minio/cli" json "github.com/minio/colorjson" "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" ) var adminConfigExportCmd = cli.Command{ @@ -40,6 +48,8 @@ FLAGS: {{range .VisibleFlags}}{{.}} {{end}} EXAMPLES: + The output includes environment variables set on the server. These cannot be overridden from the client. + 1. Export the current config from MinIO server {{.Prompt}} {{.HelpName}} play/ > config.txt `, @@ -53,7 +63,26 @@ type configExportMessage struct { // String colorized service status message. func (u configExportMessage) String() string { - return string(u.Value) + console.SetColor("EnvVar", color.New(color.FgYellow)) + bio := bufio.NewReader(bytes.NewReader(u.Value)) + var lines []string + for { + s, err := bio.ReadString('\n') + // Make lines displaying environment variables bold. + if strings.HasPrefix(s, "# MINIO_") { + s = strings.TrimPrefix(s, "# ") + parts := strings.SplitN(s, "=", 2) + s = fmt.Sprintf("# %s=%s", console.Colorize("EnvVar", parts[0]), parts[1]) + lines = append(lines, s) + } else { + lines = append(lines, s) + } + if err == io.EOF { + break + } + fatalIf(probe.NewError(err), "Unable to marshal to string.") + } + return strings.Join(lines, "") } // JSON jsonified service status Message message. diff --git a/cmd/admin-config-get.go b/cmd/admin-config-get.go index 8931c6ef..b700e039 100644 --- a/cmd/admin-config-get.go +++ b/cmd/admin-config-get.go @@ -18,12 +18,18 @@ package cmd import ( + "bufio" + "bytes" + "fmt" + "io" "strings" + "github.com/fatih/color" "github.com/minio/cli" json "github.com/minio/colorjson" "github.com/minio/madmin-go" "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" ) var adminConfigGetCmd = cli.Command{ @@ -43,6 +49,8 @@ FLAGS: {{range .VisibleFlags}}{{.}} {{end}} EXAMPLES: + The output includes environment variables set on the server. These cannot be overridden from the client. + 1. Get the current region setting on MinIO server. {{.Prompt}} {{.HelpName}} play/ region region name=us-east-1 @@ -66,7 +74,26 @@ type configGetMessage struct { // String colorized service status message. func (u configGetMessage) String() string { - return string(u.value) + console.SetColor("EnvVar", color.New(color.FgYellow)) + bio := bufio.NewReader(bytes.NewReader(u.value)) + var lines []string + for { + s, err := bio.ReadString('\n') + // Make lines displaying environment variables bold. + if strings.HasPrefix(s, "# MINIO_") { + s = strings.TrimPrefix(s, "# ") + parts := strings.SplitN(s, "=", 2) + s = fmt.Sprintf("# %s=%s", console.Colorize("EnvVar", parts[0]), parts[1]) + lines = append(lines, s) + } else { + lines = append(lines, s) + } + if err == io.EOF { + break + } + fatalIf(probe.NewError(err), "Unable to marshal to string.") + } + return strings.Join(lines, "") } // JSON jsonified service status Message message. diff --git a/cmd/print.go b/cmd/print.go index fe0ebf5d..f3ee2cee 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -46,5 +46,6 @@ func printMsg(msg message) { } } } + msgStr = strings.TrimSuffix(msgStr, "\n") console.Println(msgStr) }