Skip to content

Commit

Permalink
change log-format to string-enum
Browse files Browse the repository at this point in the history
  • Loading branch information
yuandrew committed Nov 19, 2024
1 parent e3ae3be commit 9bf7b1d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
5 changes: 3 additions & 2 deletions temporalcli/commands.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ type TemporalCommand struct {
Env string
EnvFile string
LogLevel StringEnum
LogFormat string
LogFormat StringEnum
Output StringEnum
TimeFormat StringEnum
Color StringEnum
Expand Down Expand Up @@ -302,7 +302,8 @@ func NewTemporalCommand(cctx *CommandContext) *TemporalCommand {
s.Command.PersistentFlags().StringVar(&s.EnvFile, "env-file", "", "Path to environment settings file. Defaults to `$HOME/.config/temporalio/temporal.yaml`.")
s.LogLevel = NewStringEnum([]string{"debug", "info", "warn", "error", "never"}, "info")
s.Command.PersistentFlags().Var(&s.LogLevel, "log-level", "Log level. Default is \"info\" for most commands and \"warn\" for `server start-dev`. Accepted values: debug, info, warn, error, never.")
s.Command.PersistentFlags().StringVar(&s.LogFormat, "log-format", "text", "Log format. Options are: text, json.")
s.LogFormat = NewStringEnum([]string{"text", "json", "pretty"}, "text")
s.Command.PersistentFlags().Var(&s.LogFormat, "log-format", "Log format. Accepted values: text, json.")
s.Output = NewStringEnum([]string{"text", "json", "jsonl", "none"}, "text")
s.Command.PersistentFlags().VarP(&s.Output, "output", "o", "Non-logging data output format. Accepted values: text, json, jsonl, none.")
s.TimeFormat = NewStringEnum([]string{"relative", "iso", "raw"}, "relative")
Expand Down
4 changes: 2 additions & 2 deletions temporalcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func (c *TemporalCommand) preRun(cctx *CommandContext) error {
return fmt.Errorf("invalid log level %q: %w", c.LogLevel.Value, err)
}
var handler slog.Handler
switch c.LogFormat {
switch c.LogFormat.Value {
// We have a "pretty" alias for compatibility
case "", "text", "pretty":
handler = slog.NewTextHandler(cctx.Options.Stderr, &slog.HandlerOptions{
Expand All @@ -458,7 +458,7 @@ func (c *TemporalCommand) preRun(cctx *CommandContext) error {
case "json":
handler = slog.NewJSONHandler(cctx.Options.Stderr, &slog.HandlerOptions{Level: level})
default:
return fmt.Errorf("invalid log format %q", c.LogFormat)
return fmt.Errorf("invalid log format %q", c.LogFormat.Value)
}
cctx.Logger = slog.New(handler)
}
Expand Down
11 changes: 9 additions & 2 deletions temporalcli/commandsgen/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,14 @@ func (o *Option) writeFlagBuilding(selfVar, flagVar string, w *codeWriter) error
return fmt.Errorf("missing enum values")
}
// Create enum
pieces := make([]string, len(o.EnumValues))
pieces := make([]string, len(o.EnumValues)+len(o.HiddenLegacyValues))
for i, enumVal := range o.EnumValues {
pieces[i] = fmt.Sprintf("%q", enumVal)
}
for i, legacyVal := range o.HiddenLegacyValues {
pieces[i+len(o.EnumValues)] = fmt.Sprintf("%q", legacyVal)
}

w.writeLinef("%v.%v = NewStringEnum([]string{%v}, %q)",
selfVar, o.fieldName(), strings.Join(pieces, ", "), o.Default)
flagMeth = "Var"
Expand All @@ -371,10 +375,13 @@ func (o *Option) writeFlagBuilding(selfVar, flagVar string, w *codeWriter) error
return fmt.Errorf("missing enum values")
}
// Create enum
pieces := make([]string, len(o.EnumValues))
pieces := make([]string, len(o.EnumValues)+len(o.HiddenLegacyValues))
for i, enumVal := range o.EnumValues {
pieces[i] = fmt.Sprintf("%q", enumVal)
}
for i, legacyVal := range o.HiddenLegacyValues {
pieces[i+len(o.EnumValues)] = fmt.Sprintf("%q", legacyVal)
}

if o.Default != "" {
w.writeLinef("%v.%v = NewStringEnumArray([]string{%v}, %q)",
Expand Down
11 changes: 7 additions & 4 deletions temporalcli/commandsgen/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ commands:
Default is "info" for most commands and "warn" for `server start-dev`.
default: info
- name: log-format
type: string
description: |
Log format.
Options are: text, json.
type: string-enum
description: Log format.
enum-values:
- text
- json
hidden-legacy-values:
- pretty
default: text
- name: output
type: string-enum
Expand Down
21 changes: 11 additions & 10 deletions temporalcli/commandsgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ var CommandsYAML []byte
type (
// Option represents the structure of an option within option sets.
Option struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Description string `yaml:"description"`
Short string `yaml:"short,omitempty"`
Default string `yaml:"default,omitempty"`
Env string `yaml:"env,omitempty"`
Required bool `yaml:"required,omitempty"`
Aliases []string `yaml:"aliases,omitempty"`
EnumValues []string `yaml:"enum-values,omitempty"`
Experimental bool `yaml:"experimental,omitempty"`
Name string `yaml:"name"`
Type string `yaml:"type"`
Description string `yaml:"description"`
Short string `yaml:"short,omitempty"`
Default string `yaml:"default,omitempty"`
Env string `yaml:"env,omitempty"`
Required bool `yaml:"required,omitempty"`
Aliases []string `yaml:"aliases,omitempty"`
EnumValues []string `yaml:"enum-values,omitempty"`
Experimental bool `yaml:"experimental,omitempty"`
HiddenLegacyValues []string `yaml:"hidden-legacy-values,omitempty"`
}

// Command represents the structure of each command in the commands map.
Expand Down

0 comments on commit 9bf7b1d

Please sign in to comment.