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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user