fix(bonfire): read the =value forms of the global flags and use the parsed globals in the error block (IN-02, quick-261028-rqd)

This commit is contained in:
Jakub Zych
2026-10-28 20:29:16 +01:00
parent 6294ac4141
commit e34d113c80
6 changed files with 140 additions and 11 deletions

View File

@@ -56,10 +56,16 @@ func Execute(ctx context.Context, root *cobra.Command, args []string) int {
lipgloss.EnableLegacyWindowsANSI(f)
}
}
err := root.ExecuteContext(ctx)
parsed := &parsedGlobals{}
err := root.ExecuteContext(context.WithValue(ctx, parsedGlobalsKey{}, parsed))
if err == nil {
return ExitOK
}
if parsed.set {
// cobra parsed the command's flags: use its reading, which knows
// which tokens are the values of command flags.
g = parsed.g
}
err = unknownCommand(root, args, err)
name := root.Name()
RenderError(root.ErrOrStderr(), name, wrongDirectory(name, err, args, ""), g)
@@ -79,6 +85,18 @@ func Fail(w io.Writer, name string, err error, g Globals) int {
return ExitFailure
}
// parsedGlobalsKey is the context key of the parsedGlobals Execute
// installs.
type parsedGlobalsKey struct{}
// parsedGlobals receives the globals the root's PersistentPreRunE applied,
// so Execute's error block can use cobra's reading of the command line
// instead of PeekGlobals. Call never installs one.
type parsedGlobals struct {
g Globals
set bool
}
func exitCode(err error) int {
var carrier *exitCodeError
switch {