-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add --customer-context / -D flag for per-command context override #6
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -31,6 +31,10 @@ var version string = "dev" | |
| //go:embed skills/dci-cli | ||
| var skillFS embed.FS | ||
|
|
||
| // customerContextFlagValue holds the --customer-context / -D flag value when | ||
| // set, used to suppress the Doer hint even when no persistent context file exists. | ||
| var customerContextFlagValue string | ||
|
|
||
| func dciConfigDir() string { | ||
| if dir, err := os.UserConfigDir(); err == nil && dir != "" { | ||
| cfgDir := filepath.Join(dir, "dci") | ||
|
|
@@ -129,6 +133,9 @@ func main() { | |
| } | ||
|
|
||
| func run() (exitCode int) { | ||
| // Reset per-invocation state so repeated calls (e.g. in tests) start clean. | ||
| customerContextFlagValue = "" | ||
|
|
||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| fmt.Fprintf(os.Stderr, "dci encountered an internal error: %v\n", r) | ||
|
|
@@ -193,11 +200,11 @@ func run() (exitCode int) { | |
|
|
||
| if err := cli.Run(); err != nil { | ||
| fmt.Fprintf(os.Stderr, "%v\n", err) | ||
| maybeHintDoerContext(1, configDir) | ||
| maybeHintDoerContext(1, cli.GetLastStatus(), configDir) | ||
| return 1 | ||
| } | ||
| code := cli.GetExitCode() | ||
| maybeHintDoerContext(code, configDir) | ||
| maybeHintDoerContext(code, cli.GetLastStatus(), configDir) | ||
| return code | ||
| } | ||
|
|
||
|
|
@@ -795,15 +802,15 @@ func authSource() string { | |
|
|
||
| // maybeHintDoerContext prints a targeted hint when a @doit.com user hits a 403 | ||
| // without a customer context set — covering both interactive and CI/CD usage. | ||
| func maybeHintDoerContext(exitCode int, configDir string) { | ||
| status := cli.GetLastStatus() | ||
| // status is the HTTP status code from the last request (pass cli.GetLastStatus()). | ||
| func maybeHintDoerContext(exitCode int, status int, configDir string) { | ||
| if exitCode == 0 || (status != 401 && status != 403) { | ||
| return | ||
| } | ||
| if !cachedTokenIsDoer() { | ||
| return | ||
| } | ||
| if readCustomerContext(configDir) != "" { | ||
| if readCustomerContext(configDir) != "" || customerContextFlagValue != "" { | ||
| return | ||
| } | ||
| if term.IsTerminal(int(os.Stderr.Fd())) { | ||
|
|
@@ -983,6 +990,7 @@ func addOutputFlag() { | |
| dciCmd.PersistentFlags().StringP("table-columns", "C", "", "Comma-separated list of columns to include (default: all)") | ||
| dciCmd.PersistentFlags().IntP("table-width", "W", 0, "Table width in columns (default: auto-detect terminal width)") | ||
| dciCmd.PersistentFlags().IntP("table-max-col-width", "X", 0, "Maximum width per column when fitting or wrapping (0 = auto)") | ||
| dciCmd.PersistentFlags().StringP("customer-context", "D", "", "Override the active customer context for this command (e.g. acme.com)") | ||
|
Collaborator
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. [consider] Since
Author
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. Verified — the flag does not appear on |
||
|
|
||
| // Bind table flags into viper so the renderer can pick them up. | ||
| prev := dciCmd.PersistentPreRunE | ||
|
|
@@ -1021,6 +1029,24 @@ func addOutputFlag() { | |
| bindNonNegativeIntFlag(cmd, "table-width") | ||
| bindNonNegativeIntFlag(cmd, "table-max-col-width") | ||
|
|
||
| // If --customer-context / -D was explicitly passed, override whatever | ||
| // applyCustomerContext() injected from the file or env var. | ||
| if flag := cmd.Flags().Lookup("customer-context"); flag != nil && flag.Changed { | ||
| val := strings.TrimSpace(flag.Value.String()) | ||
| if val == "" { | ||
| return fmt.Errorf("--customer-context requires a non-empty domain name") | ||
| } | ||
| existing := viper.GetStringSlice("rsh-query") | ||
| filtered := existing[:0] | ||
| for _, q := range existing { | ||
| if !strings.HasPrefix(q, "customerContext=") { | ||
| filtered = append(filtered, q) | ||
| } | ||
| } | ||
| viper.Set("rsh-query", append(filtered, "customerContext="+val)) | ||
| customerContextFlagValue = val | ||
| } | ||
|
Comment on lines
+1032
to
+1048
|
||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
||
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.
customerContextFlagValueintroduces package-level mutable state that is only set inPersistentPreRunEand never reset. Ifrun()is ever invoked multiple times in-process (e.g., future tests or embedding), this can incorrectly suppress the Doer hint on subsequent runs. Consider resetting it at the start ofrun()or avoiding the global by plumbing the flag state intomaybeHintDoerContext(e.g., pass a boolean/string).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.
Fixed in the follow-up commit.
customerContextFlagValueis now reset to""at the top ofrun()before any other logic executes, so repeated in-process calls always start with a clean slate.