Skip to content

[run] improve UX by not requiring '--' #1326

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

Merged
merged 6 commits into from
Aug 1, 2023
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
2 changes: 1 addition & 1 deletion examples/development/python/pip/devbox.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"version": "23.0.1"
}
}
}
}
3 changes: 3 additions & 0 deletions internal/boxcli/midcobra/midcobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (ex *midcobraExecutable) Execute(ctx context.Context, args []string) int {
m.preRun(ex.cmd, args)
}

// set args (needed in case caller transforms args in any way)
ex.cmd.SetArgs(args)

// Execute the cobra command:
err := ex.cmd.Execute()

Expand Down
5 changes: 3 additions & 2 deletions internal/boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ func RootCmd() *cobra.Command {

func Execute(ctx context.Context, args []string) int {
defer debug.Recover()
exe := midcobra.New(RootCmd())
rootCmd := RootCmd()
exe := midcobra.New(rootCmd)
exe.AddMiddleware(traceMiddleware)
exe.AddMiddleware(midcobra.Telemetry())
exe.AddMiddleware(debugMiddleware)
return exe.Execute(ctx, args)
return exe.Execute(ctx, wrapArgsForRun(rootCmd, args))
}

func Main() {
Expand Down
71 changes: 71 additions & 0 deletions internal/boxcli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ package boxcli

import (
"fmt"
"strings"

"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/exp/slices"

"go.jetpack.io/devbox"
"go.jetpack.io/devbox/internal/boxcli/usererr"
Expand Down Expand Up @@ -113,3 +117,70 @@ func parseScriptArgs(args []string, flags runCmdFlags) (string, string, []string

return flags.config.path, script, scriptArgs, nil
}

func wrapArgsForRun(rootCmd *cobra.Command, args []string) []string {
// if the first argument is not "run", we don't need to do anything. If there
// are 2 or fewer arguments, we also don't need to do anything because there
// are no flags after a non-run non-flag arg.
// IMPROVEMENT: technically users can pass a flag before the subcommand "run"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if they do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will fail if you don't include -- and have non-devbox flags in there, e.g.

devbox -c path/to/conf run python --version

Will say --version is not a valid flag.

This can be fixed, it just requires finding the run command and ensuring everything before it is a flag and not a command.

if len(args) <= 2 || args[0] != "run" || slices.Contains(args, "--") {
return args
}

cmd, found := lo.Find(
rootCmd.Commands(),
func(item *cobra.Command) bool { return item.Name() == "run" },
)
if !found {
return args
}
_ = cmd.InheritedFlags() // bug in cobra requires this to be called to ensure flags contains inherited flags.
runFlags := cmd.Flags()
// typical args can be of the form:
// run --flag1 val1 -f val2 --flag3=val3 --bool-flag python --version
// We handle each different type of flag
// (flag with equals, long-form, short-form, and defaulted flags)
// Note that defaulted does not mean initial value, it only means flags
// that don't require a value.
// For example, --bool-flag has NoOptDefVal set to "true".
i := 1
for i < len(args) {
arg := args[i]
if !strings.HasPrefix(arg, "-") {
// We found and argument that is not part of the flags, so we can stop
// This inserts a "--" before the first non-flag argument
// Turning
// run --flag1 val1 command --flag2 val2
// into
// run --flag1 val1 -- command --flag2 val2
return append(args[:i+1], append([]string{"--"}, args[i+1:]...)...)
}

if strings.HasPrefix(arg, "-") && strings.Contains(arg, "=") {
// This is a flag with an equals sign, so we can skip it
i++
continue
}

var flag *pflag.Flag
if strings.HasPrefix(arg, "--") {
flag = runFlags.Lookup(strings.TrimLeft(arg, "-"))
} else {
flag = runFlags.ShorthandLookup(strings.TrimLeft(arg, "-"))
}
if flag == nil {
// found an invalid flag, just return args as-is
return args
}
if flag.NoOptDefVal == "" {
// This is a non-boolean flag, e.g. --flag1 val1
i += 2
} else {
// This is a boolean flag, e.g. --bool-flag
i++
}
}

// This means there is no non-flag command. Just return as is.
return args
}