Skip to content
Draft
Show file tree
Hide file tree
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
17 changes: 12 additions & 5 deletions ctl/authz/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ func NewStatusCmd() *cobra.Command {
}

// Prepare a slice of podStatuses. We can pre-allocate since we know how many pods we'll check.
type podStatus struct {
Pod string
Status string
type PodStatus struct {
Pod string `json:"pod"`
Status string `json:"status"`
}
statuses := make([]podStatus, 0, len(podNames))
statuses := make([]PodStatus, 0, len(podNames))

// Collect the status for each pod.
for _, podName := range podNames {
Expand All @@ -126,7 +126,14 @@ func NewStatusCmd() *cobra.Command {
log.Errorf("failed to get authz status for pod %s: %v", podName, err)
continue
}
statuses = append(statuses, podStatus{Pod: podName, Status: status})
statuses = append(statuses, PodStatus{Pod: podName, Status: status})
}

if handled, err := utils.PrintOutput(cmd, statuses); err != nil {
log.Errorf("failed to print output: %v", err)
os.Exit(1)
} else if handled {
return
}

// Output the results in a table format.
Expand Down
2 changes: 2 additions & 0 deletions ctl/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func GetRootCommand() *cobra.Command {
},
}

rootCmd.PersistentFlags().StringP("output", "o", "", "Output format. One of: json|yaml")

rootCmd.AddCommand(logcmd.NewCmd())
rootCmd.AddCommand(dump.NewCmd())
rootCmd.AddCommand(waypoint.NewCmd())
Expand Down
22 changes: 18 additions & 4 deletions ctl/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,40 @@ func GetJson(url string, val any) error {
return nil
}

func GetLoggerNames(url string) {
func GetLoggerNames(cmd *cobra.Command, url string) {
var loggerNames []string
if err := GetJson(url, &loggerNames); err != nil {
log.Errorf("failed to get logger names: %v", err)
return
}

if handled, err := utils.PrintOutput(cmd, loggerNames); err != nil {
log.Errorf("failed to print output: %v", err)
os.Exit(1)
} else if handled {
return
}

fmt.Printf("Existing Loggers:\n")
for _, logger := range loggerNames {
fmt.Printf("\t%s\n", logger)
}
}

func GetLoggerLevel(url string) {
func GetLoggerLevel(cmd *cobra.Command, url string) {
var loggerInfo LoggerInfo
if err := GetJson(url, &loggerInfo); err != nil {
log.Errorf("failed to get logger level: %v", err)
return
}

if handled, err := utils.PrintOutput(cmd, loggerInfo); err != nil {
log.Errorf("failed to print output: %v", err)
os.Exit(1)
} else if handled {
return
}

fmt.Printf("Logger Name: %s\n", loggerInfo.Name)
fmt.Printf("Logger Level: %s\n", loggerInfo.Level)
}
Expand Down Expand Up @@ -182,9 +196,9 @@ func RunGetOrSetLoggerLevel(cmd *cobra.Command, args []string) {
if setFlag == "" {
if len(args) >= 2 {
url += fmt.Sprintf("?name=%s", args[1])
GetLoggerLevel(url)
GetLoggerLevel(cmd, url)
} else {
GetLoggerNames(url)
GetLoggerNames(cmd, url)
}
} else {
SetLoggerLevel(url, setFlag)
Expand Down
11 changes: 9 additions & 2 deletions ctl/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ kmeshctl secret create --key=$(echo -n "{36-character user-defined key here}" |
kmeshctl secret get`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
GetSecret()
GetSecret(cmd)
},
}

Expand Down Expand Up @@ -198,7 +198,7 @@ func CreateOrUpdateSecret(cmd *cobra.Command, args []string) {
}
}

func GetSecret() {
func GetSecret(cmd *cobra.Command) {
secret, err := clientset.Kube().CoreV1().Secrets(utils.KmeshNamespace).Get(context.TODO(), SecretName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
Expand Down Expand Up @@ -234,6 +234,13 @@ func GetSecret() {
Length: ipSecKey.Length,
}

if handled, err := utils.PrintOutput(cmd, displayKey); err != nil {
log.Errorf("failed to print output: %v", err)
os.Exit(1)
} else if handled {
return
}

displayData, err := json.MarshalIndent(displayKey, "", " ")
if err != nil {
log.Errorf("failed to marshal display data: %v", err)
Expand Down
43 changes: 43 additions & 0 deletions ctl/utils/printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package utils

import (
"encoding/json"
"fmt"

"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)

// PrintOutput checks if the global --output (-o) flag is set on the command.
// If it is set to "json" or "yaml", it marshals the data appropriately, prints it to stdout,
// and returns true (indicating to the caller that default table/text formatting can be skipped).
// If no output format is specified, it returns false.
func PrintOutput(cmd *cobra.Command, data interface{}) (bool, error) {
outputFlag, err := cmd.Flags().GetString("output")
if err != nil {
// Flag might not be defined on some commands, default to normal output
return false, nil
}

switch outputFlag {
case "json":
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return false, fmt.Errorf("failed to marshal json: %w", err)
}
fmt.Println(string(b))
return true, nil
case "yaml", "yml":
b, err := yaml.Marshal(data)
if err != nil {
return false, fmt.Errorf("failed to marshal yaml: %w", err)
}
fmt.Print(string(b)) // yaml.Marshal already appends a newline
return true, nil
case "":
return false, nil
default:
// Unsupported output format, fallback to default or throw error
return false, fmt.Errorf("unsupported output format: %s", outputFlag)
}
}
Loading