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

Log env vars which override the env file #483

Merged
merged 2 commits into from
Feb 28, 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
8 changes: 8 additions & 0 deletions temporalcli/commands.env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ func TestEnv_Simple(t *testing.T) {
res = h.Execute("env", "list")
h.NoError(res.Err)
h.NotContains(res.Stdout.String(), "myenv2")

// Ensure env var overrides env file
res = h.Execute("env", "set", "myenv1.address", "something:1234")
h.NoError(res.Err)
h.NoError(os.Setenv("TEMPORAL_ADDRESS", "overridden:1235"))
defer os.Unsetenv("TEMPORAL_ADDRESS")
res = h.Execute("workflow", "list", "--env", "myenv1")
h.Contains(res.Stderr.String(), "Env var overrode --env setting")
}
24 changes: 20 additions & 4 deletions temporalcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,13 @@ func UnmarshalProtoJSONWithOptions(b []byte, m proto.Message, jsonShorthandPaylo
return opts.Unmarshal(b, m)
}

func (c *CommandContext) populateFlagsFromEnv(flags *pflag.FlagSet) error {
// Set flag values from environment file & variables. Returns a callback to log anything interesting
// since logging will not yet be initialized when this runs.
func (c *CommandContext) populateFlagsFromEnv(flags *pflag.FlagSet) (func(*slog.Logger), error) {
if flags == nil {
return nil
return func(logger *slog.Logger) {}, nil
}
var logCalls []func(*slog.Logger)
var flagErr error
flags.VisitAll(func(flag *pflag.Flag) {
// If the flag was already changed by the user, we don't overwrite
Expand All @@ -254,11 +257,21 @@ func (c *CommandContext) populateFlagsFromEnv(flags *pflag.FlagSet) error {
flag.Name, anns[0], envVal, err)
return
}
if flag.Changed {
logCalls = append(logCalls, func(l *slog.Logger) {
l.Info("Env var overrode --env setting", "env_var", anns[0], "flag", flag.Name)
})
}
flag.Changed = true
}
}
})
return flagErr
logFn := func(logger *slog.Logger) {
for _, call := range logCalls {
call(logger)
}
}
return logFn, flagErr
}

// Returns error if JSON output enabled
Expand Down Expand Up @@ -307,7 +320,8 @@ func (c *TemporalCommand) initCommand(cctx *CommandContext) {
c.Command.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
// Populate environ. We will make the error return here which will cause
// usage to be printed.
if err := cctx.populateFlagsFromEnv(cmd.Flags()); err != nil {
logCalls, err := cctx.populateFlagsFromEnv(cmd.Flags())
if err != nil {
return err
}

Expand All @@ -319,6 +333,8 @@ func (c *TemporalCommand) initCommand(cctx *CommandContext) {

res := c.preRun(cctx)

logCalls(cctx.Logger)

// Always disable color if JSON output is on (must be run after preRun so JSONOutput is set)
if cctx.JSONOutput {
color.NoColor = true
Expand Down
Loading