Skip to content
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

change log-format to string-enum #711

Merged
merged 4 commits into from
Nov 19, 2024
Merged
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
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
6 changes: 6 additions & 0 deletions temporalcli/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,9 @@ func TestUnknownCommandExitsNonzero(t *testing.T) {
res := commandHarness.Execute("blerkflow")
assert.Contains(t, res.Err.Error(), "unknown command")
}

func (s *SharedServerSuite) TestHiddenAliasLogFormat() {
_ = s.waitActivityStarted().GetID()
res := s.Execute("workflow", "list", "--log-format", "pretty", "--address", s.Address())
s.NoError(res.Err)
}
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
Loading