-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cli): add config view command with JSON output support #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f4f8fb9
b8d3b91
0e89dd0
bdd34fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package commands | ||
|
|
||
| import "github.com/urfave/cli/v2" | ||
|
|
||
| var ConfigCommand *cli.Command = &cli.Command{ | ||
| Name: "config", | ||
| Usage: "manage shelltime configuration", | ||
| Subcommands: []*cli.Command{ | ||
| ConfigViewCommand, | ||
| }, | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,167 @@ | ||||||
| package commands | ||||||
|
|
||||||
| import ( | ||||||
| "encoding/json" | ||||||
| "fmt" | ||||||
| "os" | ||||||
| "reflect" | ||||||
| "strings" | ||||||
|
|
||||||
| "github.com/gookit/color" | ||||||
| "github.com/malamtime/cli/model" | ||||||
| "github.com/olekukonko/tablewriter" | ||||||
| "github.com/urfave/cli/v2" | ||||||
| "go.opentelemetry.io/otel/trace" | ||||||
| ) | ||||||
|
|
||||||
| var ConfigViewCommand *cli.Command = &cli.Command{ | ||||||
| Name: "view", | ||||||
| Usage: "view current configuration", | ||||||
| Flags: []cli.Flag{ | ||||||
| &cli.StringFlag{ | ||||||
| Name: "format", | ||||||
| Aliases: []string{"f"}, | ||||||
| Value: "table", | ||||||
| Usage: "output format (table/json)", | ||||||
| }, | ||||||
| }, | ||||||
| Action: configView, | ||||||
| OnUsageError: func(cCtx *cli.Context, err error, isSubcommand bool) error { | ||||||
| color.Red.Println(err.Error()) | ||||||
| return nil | ||||||
| }, | ||||||
| } | ||||||
|
|
||||||
| func configView(c *cli.Context) error { | ||||||
| ctx, span := commandTracer.Start(c.Context, "config.view", trace.WithSpanKind(trace.SpanKindClient)) | ||||||
| defer span.End() | ||||||
|
|
||||||
| SetupLogger(os.ExpandEnv("$HOME/" + model.COMMAND_BASE_STORAGE_FOLDER)) | ||||||
|
|
||||||
| format := c.String("format") | ||||||
| if format != "table" && format != "json" { | ||||||
| return fmt.Errorf("unsupported format: %s. Use 'table' or 'json'", format) | ||||||
| } | ||||||
|
|
||||||
| cfg, err := configService.ReadConfigFile(ctx) | ||||||
| if err != nil { | ||||||
| return fmt.Errorf("failed to read config: %w", err) | ||||||
| } | ||||||
|
|
||||||
| if format == "json" { | ||||||
| return outputConfigJSON(cfg) | ||||||
| } | ||||||
| return outputConfigTable(cfg) | ||||||
| } | ||||||
|
|
||||||
| func outputConfigJSON(cfg model.ShellTimeConfig) error { | ||||||
| jsonData, err := json.MarshalIndent(cfg, "", " ") | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
| fmt.Println(string(jsonData)) | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func outputConfigTable(cfg model.ShellTimeConfig) error { | ||||||
| w := tablewriter.NewWriter(os.Stdout) | ||||||
| w.Header([]string{"KEY", "VALUE"}) | ||||||
|
|
||||||
| pairs := flattenConfig(cfg, "") | ||||||
| for _, pair := range pairs { | ||||||
| w.Append([]string{pair.key, pair.value}) | ||||||
| } | ||||||
|
|
||||||
| w.Render() | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| type keyValuePair struct { | ||||||
| key string | ||||||
| value string | ||||||
| } | ||||||
|
|
||||||
| func flattenConfig(v interface{}, prefix string) []keyValuePair { | ||||||
| var pairs []keyValuePair | ||||||
|
|
||||||
| val := reflect.ValueOf(v) | ||||||
| if val.Kind() == reflect.Ptr { | ||||||
| if val.IsNil() { | ||||||
| return pairs | ||||||
| } | ||||||
| val = val.Elem() | ||||||
| } | ||||||
|
|
||||||
| if val.Kind() != reflect.Struct { | ||||||
| return pairs | ||||||
| } | ||||||
|
|
||||||
| typ := val.Type() | ||||||
| for i := 0; i < val.NumField(); i++ { | ||||||
| field := val.Field(i) | ||||||
| fieldType := typ.Field(i) | ||||||
|
|
||||||
| // Get JSON tag for the key name | ||||||
| jsonTag := fieldType.Tag.Get("json") | ||||||
| if jsonTag == "" || jsonTag == "-" { | ||||||
| continue | ||||||
| } | ||||||
| // Parse the tag to get just the name part | ||||||
| tagParts := strings.Split(jsonTag, ",") | ||||||
| keyName := tagParts[0] | ||||||
|
|
||||||
| fullKey := keyName | ||||||
| if prefix != "" { | ||||||
| fullKey = prefix + "." + keyName | ||||||
| } | ||||||
|
|
||||||
| // Handle pointer types | ||||||
| if field.Kind() == reflect.Ptr { | ||||||
| if field.IsNil() { | ||||||
| pairs = append(pairs, keyValuePair{key: fullKey, value: "<not set>"}) | ||||||
| continue | ||||||
| } | ||||||
| field = field.Elem() | ||||||
| } | ||||||
|
|
||||||
| switch field.Kind() { | ||||||
| case reflect.Struct: | ||||||
| // Recursively flatten nested structs | ||||||
| nestedPairs := flattenConfig(field.Interface(), fullKey) | ||||||
| pairs = append(pairs, nestedPairs...) | ||||||
| case reflect.Slice: | ||||||
| if field.Len() == 0 { | ||||||
| pairs = append(pairs, keyValuePair{key: fullKey, value: "[]"}) | ||||||
| } else { | ||||||
| // Marshal slice to JSON for display | ||||||
| jsonBytes, err := json.Marshal(field.Interface()) | ||||||
| if err != nil { | ||||||
| pairs = append(pairs, keyValuePair{key: fullKey, value: fmt.Sprintf("<%d items>", field.Len())}) | ||||||
| } else { | ||||||
| pairs = append(pairs, keyValuePair{key: fullKey, value: string(jsonBytes)}) | ||||||
| } | ||||||
| } | ||||||
| case reflect.Bool: | ||||||
| pairs = append(pairs, keyValuePair{key: fullKey, value: fmt.Sprintf("%v", field.Bool())}) | ||||||
| case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||||||
| pairs = append(pairs, keyValuePair{key: fullKey, value: fmt.Sprintf("%d", field.Int())}) | ||||||
| case reflect.String: | ||||||
| value := field.String() | ||||||
| if value == "" { | ||||||
| value = "<empty>" | ||||||
| } else if strings.Contains(fullKey, "token") || strings.Contains(strings.ToLower(fullKey), "token") { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first condition
Suggested change
|
||||||
| // Mask sensitive fields | ||||||
| if len(value) > 8 { | ||||||
| value = value[:4] + "****" + value[len(value)-4:] | ||||||
| } else { | ||||||
| value = "****" | ||||||
| } | ||||||
| } | ||||||
| pairs = append(pairs, keyValuePair{key: fullKey, value: value}) | ||||||
| default: | ||||||
| pairs = append(pairs, keyValuePair{key: fullKey, value: fmt.Sprintf("%v", field.Interface())}) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return pairs | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding the path separator
/and usingos.ExpandEnv("$HOME")is not platform-independent. According to the repository's general rules, you should useos.UserHomeDir()to get the user's home directory andfilepath.Jointo construct paths. This will ensure your code works correctly across different operating systems like Windows.Here's a suggested implementation:
You'll need to import the
path/filepathpackage.References
filepath.Jointo combine segments andos.UserHomeDir()to get the home directory, rather than hardcoding path separators or environment variables like$HOME.