refactor(bonfire): run the root without fang so bonfire's root help stays installed (IN-06, quick-261028-rqd)
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"charm.land/fang/v2"
|
||||
"charm.land/lipgloss/v2"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -27,8 +27,9 @@ const (
|
||||
// ExitFailure, ExitUsage for usage errors, or the code an error carries
|
||||
// through WithExitCode. With no command word in args (only global options,
|
||||
// -h or --help) it prints the command list. Errors are rendered on the
|
||||
// root's error stream by RenderError through fang's error handler; fang is
|
||||
// used without its version flag, man page and completion commands.
|
||||
// root's error stream by RenderError. Execute leaves the root's help
|
||||
// function alone, and on Windows enables ANSI processing on the output and
|
||||
// error streams when they are files (a no-op elsewhere).
|
||||
func Execute(ctx context.Context, root *cobra.Command, args []string) int {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
@@ -45,20 +46,24 @@ func Execute(ctx context.Context, root *cobra.Command, args []string) int {
|
||||
}
|
||||
resetFlags(root)
|
||||
root.SetArgs(args)
|
||||
// The settings fang applied; NewRootIO sets them already, a root built
|
||||
// elsewhere gets them here.
|
||||
root.SilenceUsage = true
|
||||
root.SilenceErrors = true
|
||||
root.CompletionOptions.DisableDefaultCmd = true
|
||||
for _, w := range []io.Writer{root.OutOrStdout(), root.ErrOrStderr()} {
|
||||
if f, ok := w.(*os.File); ok {
|
||||
lipgloss.EnableLegacyWindowsANSI(f)
|
||||
}
|
||||
}
|
||||
err := root.ExecuteContext(ctx)
|
||||
if err == nil {
|
||||
return ExitOK
|
||||
}
|
||||
err = unknownCommand(root, args, err)
|
||||
name := root.Name()
|
||||
err := fang.Execute(ctx, root,
|
||||
fang.WithoutVersion(),
|
||||
fang.WithoutManpage(),
|
||||
fang.WithoutCompletions(),
|
||||
fang.WithErrorHandler(func(w io.Writer, _ fang.Styles, err error) {
|
||||
// fang hands over a colorprofile writer built from the
|
||||
// environment only; RenderError unwraps it and applies
|
||||
// --ansi and --no-ansi itself.
|
||||
err = unknownCommand(root, args, err)
|
||||
RenderError(w, name, wrongDirectory(name, err, args, ""), g)
|
||||
}),
|
||||
)
|
||||
return exitCode(unknownCommand(root, args, err))
|
||||
RenderError(root.ErrOrStderr(), name, wrongDirectory(name, err, args, ""), g)
|
||||
return exitCode(err)
|
||||
}
|
||||
|
||||
// Fail renders err like Execute does and returns ExitFailure (ExitOK for a
|
||||
|
||||
Reference in New Issue
Block a user