Skip to content

Commit

Permalink
minor improvements to logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pgier committed May 1, 2024
1 parent 5bd637e commit fdcff4e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/kcloud/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func AzureListClusters(stdout, stderr io.Writer, subscription string) error {
return err
}
if cli.Verbose {
fmt.Println("debug: raw command output")
fmt.Println("[debug] raw command output")
fmt.Print(string(output))
fmt.Println("debug: end raw command output")
fmt.Println("[debug] end raw command output")
}
clusterList := []azureCluster{}
if err := json.Unmarshal(output, &clusterList); err != nil {
Expand Down
13 changes: 7 additions & 6 deletions cmd/kcloud/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)

// RunCommandAndPrint runs the given command and prints the output
func RunCommandAndPrint(command string, args ...string) error {
if cli.Verbose {
fmt.Println("debug: ", QuoteCommand(command, args...))
fmt.Println("[debug] ", QuoteCommand(command, args...))
}
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
Expand All @@ -26,18 +27,18 @@ func RunCommandAndPrint(command string, args ...string) error {
// combined stdout/stderr output
func RunCommand(command string, args ...string) ([]byte, error) {
if cli.Verbose {
fmt.Println("debug: ", QuoteCommand(command, args...))
fmt.Println("[debug] ", QuoteCommand(command, args...))
}
cmd := exec.Command(command, args...)
cmd.Stderr = os.Stderr
return cmd.Output()
}

// QuoteCommand writes the given command with quotes around the args for convenience
// QuoteCommand concatenates the command and args with quotes around the args to avoid whitespace issues.
func QuoteCommand(command string, args ...string) string {
var sb strings.Builder
var quotedArgs strings.Builder
for _, arg := range args {
sb.WriteString(fmt.Sprintf(" \"%s\"", arg))
quotedArgs.WriteString((" " + strconv.Quote(arg)))
}
return sb.String()
return command + quotedArgs.String()
}

0 comments on commit fdcff4e

Please sign in to comment.