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

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

merged 6 commits into from
Aug 1, 2023

Conversation

mikeland73
Copy link
Contributor

@mikeland73 mikeland73 commented Jul 27, 2023

Summary

Inspired by @jimmidyson's awesome post I thought we could remove the need for -- every time you want to run something with devbox that has flags.

This change assumes that when a user runs devbox run the first non-flag arg after "run" is part of the command or the script name the user wants to run. This relies on run never having subcommands (which I think will be true forever). So we transform:

devbox run python --version

into

devbox run -- python --version

If command already has -- then we do nothing.

This change is fully backward compatible.

@LucilleH I'm somewhat sure I fixed the issues from demo. PTAL

How was it tested?

devbox add python
devbox run python --version
Python 3.11.4
devbox run -c examples/development/python/pip python --version

@mikeland73 mikeland73 requested review from gcurtis and ipince July 27, 2023 23:16
@mikeland73 mikeland73 changed the title [run] improve UX by not requiring '--' and pass args to scripts [run] improve UX by not requiring '--' Jul 28, 2023
@mikeland73 mikeland73 requested a review from LucilleH July 28, 2023 22:42
// 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"
if len(args) <= 2 || args[0] != "run" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we move this clause up first, given that it is only applicable to run? Something like:

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

}
}

return append(args[:i+1], append([]string{"--"}, args[i+1:]...)...)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this logic make sense. Can we simplify a bit? Took me a while to understand what is going on. like:

for i < len(args) {
  arg := args[i]
  if !strings.HasPrefix(arg, "-") {
    // finally found it!
    return append(args[:i+1], append([]string{"--"}, args[i+1:]...)...)
  }

  if strings.Contains(arg, "=") {
    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 {
    if flag.NoOptDefVal == "" {
	i := i+2
    } else {
        i++
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah. I would also add a comment in english stating what the loop is doing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Something like: "adds a -- after all flags that come before the command that will be run. Thus, it transforms something like run --flag1 val1 command --flag2 val2 into run --flag1 val1 -- command --flag2 val2".

That would save a code reader quite a bit of time I think.

Copy link
Contributor

@ipince ipince left a comment

Choose a reason for hiding this comment

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

Approving to unblock!

}
}

return append(args[:i+1], append([]string{"--"}, args[i+1:]...)...)
Copy link
Contributor

Choose a reason for hiding this comment

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

Something like: "adds a -- after all flags that come before the command that will be run. Thus, it transforms something like run --flag1 val1 command --flag2 val2 into run --flag1 val1 -- command --flag2 val2".

That would save a code reader quite a bit of time I think.

// 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.

@jimmidyson
Copy link

jimmidyson commented Jul 29, 2023

Just FYI the comment re devbox run in my blog post wasn't about the need for --, I actually like that too demarcate the command, but about needing devbox run at all - I couldn't figure out a way to massage the environment so PATH, etc was configured properly to use the devbox installed tools.

@mikeland73
Copy link
Contributor Author

thanks for the clarification @jimmidyson!

We chatted about that as well and I think @LucilleH has some ideas to make the action better.

Not sure if it helps your particular use case, but if you do:

eval "$(devbox shellenv)" in a directory with a devbox.json it's the equivalent of turning the current shell into the devbox environment. I think in Github actions the environment is not persisted between steps so not sure if this is helpful, we may need to make a chance to the action itself.

@vercel
Copy link

vercel bot commented Aug 1, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
devbox-docs ❌ Failed (Inspect) Aug 1, 2023 7:18pm

@mikeland73 mikeland73 merged commit 767becb into main Aug 1, 2023
@mikeland73 mikeland73 deleted the landau/better-run branch August 1, 2023 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants