Skip to content

Simplify CLI commands #6229

Closed
Closed

Description

@sipsma

Related discord thread: https://discord.com/channels/707636530424053791/1174585230649217055/1174847141265547324

Problem

We've accumulated quite a few new CLI commands as part of module support:

  • call
  • download
  • shell
  • up
  • mod

In addition to some existing ones that have varying degrees of conceptual and/or implementation overlap

  • run
  • query
  • listen

It's becoming overwhelming and convoluted. Examples of confusion:

  1. There's 3 ways of running a container that are all subtly different: call, shell and up technically all give you ways of running a container but with different features and behavior in terms of caching and what is actually executed in the container.
  2. It's not always clear whether what you are operating on for a given command is just the final return value, the pipeline, or both

As part of this there's also been a point raised about trying to minimize the cases in which users feel compelled to wrap the CLI in complicated shell scripts. We don't want to make that impossible or anything, but in general it seems we're all of the opinion that the functionality we provide in the CLI should ideally minimize cases in which users even feel compelled to resort to complex shell script wrapping.

@shykes summed this up as:

  1. The CLI should be a great entrypoint: an intuitive and painless way to run pipelines with Dagger, even if you're not an expert.
  2. The CLI should be a good swiss army knife for power users who want to poke at a module without writing code. Debugging, troubleshooting, inspecting, etc.
  3. Non-goal: extreme composability, ie. your shell is now a Dagger SDK

Solutions

There's quite a few possible solutions here, and it's entirely possible the end result will be some combination of a few different solutions.

One of the top ideas currently is to consolidate as many commands as possible to a single command, with different behaviors triggered by different flags. @shykes gave a hypothetical example of consolidating around run:

  • dagger run --print -> just print information about the result to the terminal
  • dagger run --export TARGET -> save the result to the local filesystem, or a remote registry
  • dagger run --exec -> execute the result (default behavior for container results)
  • dagger run --exec-command -> change the command to exec
  • dagger run --exec-interactive -> change whether to attach an interactive terminal on exec
  • dagger run --exec-port PORT -> forward ports on exec
  • dagger run --up -> bring a service up (redundant with exec?)

In general, consolidation in some form or another seems like a baseline requirement for any solution.

As an example of one other variation brought up: we could decide that there shouldn't be any commands whose purpose is to just "run a container" and instead require that pipelines always operate on scalars (or "simple objects" consisting only of scalars). This could look like:

  • dagger call - call the dagger API, including any user modules loaded into that API (must be a pipeline that ends in a scalar or simple object)
  • dagger debug - the functionality of shell and up rolled into one command whose focus is on debugging pipelines rather than just "running containers defined by the API"

There's of course many other viable variations on all of the above possible too, just throwing out the suggestions brought up so far.

Intertwined issues

There's also quite a few open issues/discussions that feel to me like they are potentially related and are worth considering alongside all of this. It's obviously ideal to keep decisions + designs self-contained if possible, so just bringing these up in the case that they end up overlapping to the point that they can't be self-contained:

  1. Support for the core API in the CLI
    • This especially seems highly interrelated because support for core would imply that a function that returns, e.g. a Directory, could be chained with the export API (dagger run ... export --path ./where/i/want/the/file), which could eliminate use cases for download and/or an --export flag on a consolidated dagger run command.
  2. With* pattern translates awkwardly to CLI
    • This problem makes me question if the overall api chaining approach in the CLI needs general re-thinking (not sure, many possibilities here but worth consideration)
  3. Make passing caller environment variables to modules more seamless
    • This may end up orthogonal, but if we decide on a solution that involves a formalized difference between main and library modules this will definitely overlap (see below)
  4. main vs. library modules
    • This is still just a vague idea that gets brought up a lot in various threads/issues, so it doesn't have a dedicated issue yet
    • The idea is that we could formalize the difference between modules meant to be invoked directly by end CLI users (main modules) and modules meant to be used as libraries by other modules for re-usable functionality (library modules). main libraries would potentially have special features around interaction w/ the caller and possibly restrictions in terms of the APIs they expose in order to optimize them for a CLI UX
    • If we do end up going down this route, it definitely could have an impact on the command structure
  5. dagger script: invoke any function from any module, in your favorite language, from the CLI
    • Basically, we may want to support both language snippet one-liners as an alternative to posix-y commands+flags in addition to potential full-blown repl support
  6. Duck typing for CLI verbs

Activity

  1. sipsma commented on Dec 13, 2023

    @sipsma
    ContributorAuthor

    Finally had time at the end of today to start diving into this. Have just done some more thinking so far, want to checkpoint with one other promising line of thought building off the previous discussions.


    One potential issue with consolidating everything to a single (or even small set) of commands is that we'll be trading "command bloat" for "flag bloat".

    i.e. if everything is done via dagger run + flags, dagger run --help will show a ton of flags and whether those flags are even applicable can't be known yet (certain flags only make sense for certain pipeline "return values").

    I can imagine in the long term this growing more and more. E.g. we start with --export but then someone wants a pipeline that results in a directory to not be written directly to your host but instead be written as a .tar.gz. Then we need to choose whether to say "no" or to add another flag like --export-tarball.

    This is not a deal-breaker per-se, but does seem like a legitimate downside to this sort of approach worth noting.

    Idea - Chaining is all you need

    One potential way to both consolidate commands but also not bloat with a ton of top-level flags is to start with the idea of adding support for the core APIs to the dagger cli.

    Say we do both that and consolidate everything to dagger run.

    That by itself would make the following work out of the box:

    // "my-binary build" is a pipeline of user functions that returns a File 
    dagger run my-binary build export --path ./bin/my-binary
    

    The above works because the core API's File object already today has a function defined on it called export that accepts an arg named path and results in the file being written to the caller's filesystem at that path.

    Basically, there's no need for dagger download or dagger run --export because we inherently already have that functionality due to it existing in the core api as a function already.

    Publishing an image would look like this:

    dagger run my-container publish --address ttl.sh/foobar:latest
    

    A few of our current top-level cli commands don't quite fit as nicely as those, but I think they could with small adjustments (and with the help of some more ideas I'll mention later).

    e.g. with some adjustments to the core api dagger shell could become

    dagger run my-container shell
    

    dagger up could perhaps become something like this with some core api adjustments (pending bikeshedding, just a strawman to get idea across)

    dagger run my-container listen -p 8080:8081
    

    dagger call just sort of melds seamlessly into dagger run as part of this too.

    In this model, the CLI would have the rule that the pipeline specified ends in a scalar. So you can't just specify a pipeline that ends in Container, you must choose something from the container or run a function that is "side-effects" only (aka void return type).

    • The same applies to non-core user objects too of course
    • This is a nice way of avoiding a lot of the ambiguity around whether what you're operating on is the final return value, the pipeline, or both, etc. And the similar questions around what the default behavior should be for each possible return type (we only need to handle scalars).

    To be clear, there still may be some top-level flags under dagger run. Flags for controlling the progress output could still be there for instance. But they would be much more limited in long-term scope by being solely focused on flags for the CLI itself.

    Pros

    1. Avoids both top-level-command bloat and top-level-flag bloat.
      • Along the same lines, avoids showing a bunch of top-level flags under dagger run that may or may not actually work depending on the pipeline that actually gets run. Only valid subcommands are available for each function in the pipeline.
    2. We wanted to add support for the core API in the CLI anyways, so supporting something like this almost comes along with that inherently, why not just fully embrace it?
    3. Simplifies CLI behavior by requiring that every pipeline end in a scalar
    4. Works seamlessly across both user modules and the core API; everything falls under one cohesive model rather than trying to make the core api objects+functions overly special in the CLI.

    Cons (with possible mitigations)

    The Core API was not optimized for a CLI interface

    One problem with this is that if a user had a module function called my-container that returned a Container and ran this:

    dagger run my-container --help
    

    By default they'd see an outrageous amount of subcommands, including tons of with* APIs.

    This is very significant, but interestingly it's the exact same problem we're facing today with user modules (i.e. #6115).

    So as a small consolation it's not exactly introducing a new problem, just bringing an existing one to a head more immediately.

    I don't want to go way off on a tangent here so I'll be brief, but I do see some potential ways of fixing this. One idea would be to add an advanced feature to our API that gives more custom control over what functions are visible in the CLI by default and which are hidden by default. Maybe this could be used for more than just visibility on the CLI level too.

    This would be reserved for advanced users who want finer grain control of the CLI UX their crafting. However, as dagger developers we are most certainly advanced users and can make liberal use of it in the core API to customize how it's presented in the CLI.

    Overall, whatever the solution here, I do like that at least we'd be solving it simultaneously for both users and our own core APIs.

    Discoverability

    Avoiding top-level-command and top-level-flag bloat does have the downside in that even if there's a ton of commands/flags, at least when you run dagger --help or dagger run --help you get a "lay of the land" in terms of all the things dagger can do.

    I think this is just an inherent tradeoff that doesn't have one silver bullet solution. It can definitely be mitigated to a decent extent through a combination of:

    1. Obvious stuff - good docs, tutorials (maybe a dagger -m github.com/dagger/getstarted interactive shell module? 🙂)
    2. Perhaps showing more than just one hierarchy of commands when running dagger run --help
      • So for simple cases like where the user module just has function build that returns a File, we show two levels deep of functions available - both build and the subcommands available on build like export
    3. Improvements to overall user module documentation/example support (which we want to do anyways)

    @shykes @helderco (and anyone else interested) thoughts? I'm still very much up in the air about all of this, but the line of thought here seemed interesting enough to share and bounce off every else's heads 😄

  2. sipsma commented on Dec 20, 2023

    @sipsma
    ContributorAuthor

    There's been some more ongoing discussion about #6229 (comment) in other mediums, will summarize here.

    The overall status is that we're going to pursue that approach (with some modifications mentioned below). There's a "first step" PR here: #6293

    I'll also separately start breaking this down into subissues that can be fanned out to the team once that PR is merged.

    Loosening restrictions on return type being a scalar

    (Discussed, among other things, in this discord thread: https://discord.com/channels/707636530424053791/1184618502699614298)

    In the previous comment, I said that "the CLI would have the rule that the pipeline specified ends in a scalar".

    However, that can be safely relaxed without compromising on the other benefits. Namely, what's actually important to the design is that the CLI only has one behavior for a given return type and that the behavior is consistent and non-surprising.

    • Previously, the problem was that there's many possible things a CLI caller could want to do with, for example, a Container/Directory/File/etc. and we were resolving that ambiguity by adding lots of commands for all the stuff you could want to do
    • The new approach helps resolve that ambiguity by allowing the caller to continue chaining off core type returns values. But that doesn't actually imply the CLI must error out in the case where a return value is not a scalar.

    The exact behavior for different return types is TBD, but as a strawman:

    1. Primitive types are obvious: just print them
    2. Objects can be printed as json, showing their fields (i.e. don't call functions on them, just simple pre-populated fields)
    3. Container/Directory/File are less immediately obvious but plenty of reasonable options to choose from
      • The most obvious option is to still not handle these as return types and just show --help output asking the user to select what they want from them. There's not a requirement that all types must be handled, just an option in the design to do so now.
      • If we do want to handle them, I think the general goal should be something simple and obvious, like their metadata (or I suppose even just their id once ids v2 makes that system more sane)
      • What I think we should avoid is something like showing the stdout of a Container if that's the return type, which creates a backchannel way of running container execs that's now better served by having users actually select ... stdout if that's what they want to see from the returned container

    -o flag for writing to caller's filesystem

    (Discussed in this discord thread: https://discord.com/channels/707636530424053791/1186785873770074203)

    The first important note coming out of that discussion is that even if the new approach gets rid of ambiguity + special casing around return types (e.g. what does the CLI do with a user function returning a Directory), it doesn't get rid of the need for some special sugar handling of core types as function arg inputs.

    • E.g. you can specify an input Directory arg as --dir ./some/input/dir but to write a Directory to the caller's filesystem you have to go through the whole ... export --path ./some/output/dir
    • This is okay generally speaking, but does create some asymmetry

    Coming out of that, @shykes brought up idea of supporting a generic -o <path> (--output <path> in long form) flag on the CLI command. This flag would be applicable for any return type and result in that result being written to the caller's filesystem rather than written to stdout.

    For simple cases like a string return type, this would have the same end effect as a shell redirection. The difference is that we would allow some special case handling for it too. So if myfunc returns a Directory, this would work:

    dagger run -o ./some/output/dir myfunc
    

    and result in the the directory returned by myfunc to be written to ./some/output/dir.

    To start, -o would be exclusively for writing to the cli caller's filesystem.

    • I suppose you could in theory expand to other output destinations via a scheme:// type approach if we ever wanted to, but that's just a possible future extension point, not in scope now and just a random idea.

    Also important to note that this would not remove support for the full ... export --path ./some/output/dir style either, it would just be a supplement to it.

  3. sagikazarmark commented on Jan 3, 2024

    @sagikazarmark
    Contributor

    If I may add my two cents to the discussion:

    • I kinda like dagger export, because it's clear what it does. I would also love to see some extra features (for example, extracting a file from a container) that may be less easy to implement and explain in call/run.
    • I find myself wanting to use shell in some cases, especially when I'm trying to debug a pipeline, but when the last command fails, I often have to change the code first, so it's not actually that useful on its own. Something like run --shell --entrypoint <bash> replacing the last command (which is often what I'd like to replace during debugging) would be more useful to me. Having shell as an alias is also okay.
    • As a docker-compose up -d user (if you see me run it without -d, I'll buy you a beer), up is less useful to me, since it requires a shell. It also requires me to restart containers when something changes (unlike docker compose with volume mounts). Although I'd really like to, I don't really see myself using it as it is right now. I might reconsider if it becomes more akin to docker compose.

    Generally speaking, Dagger seems to be getting close to a semi-replacement for some docker/docker compose commands. People already used to those (like myself) may appreciate similar commands and behavior in the Dagger CLI.

  4. sipsma commented on Jan 3, 2024

    @sipsma
    ContributorAuthor

    I kinda like dagger export, because it's clear what it does. I would also love to see some extra features (for example, extracting a file from a container) that may be less easy to implement and explain in call/run.

    We are now planning on adding a -o flag (--output in long-form) that essentially triggers the same behavior dagger export does today, but as a flag on the consolidated dagger run command rather than separate command entirely.

    Combined with the other adjustments that use case of extracting a file from a container should actually be possible entirely from the CLI.

    E.g. if you have a function foo that returns a Container:

    • dagger run -o ./output/foo.tar.gz foo
      • export the Container returned by foo to ./output/foo.tar.gz as a tarball
    • dagger run -o ./output/some-file foo file --path /some/file/in/the/container
      • select the file at /some/file/in/the/container and write that to ./output/some-file

    I find myself wanting to use shell in some cases, especially when I'm trying to debug a pipeline, but when the last command fails, I often have to change the code first, so it's not actually that useful on its own. Something like run --shell --entrypoint replacing the last command (which is often what I'd like to replace during debugging) would be more useful to me. Having shell as an alias is also okay.

    That makes sense. There's also the slightly more general problem of failures being possible in any execs that are executed as part of a function call too (whether the "final" container in the chain or not) and wanting to drop into a debug shell for any of those.

    There's some under the hood technical complications that make the "drop into shell for failed exec" use case probably be out of scope at the moment just due to time+priorities, but I will keep it in mind while implementing the updates for all of this to make sure we at least keep our options open for supporting it down the line.

    As a docker-compose up -d user (if you see me run it without -d, I'll buy you a beer), up is less useful to me, since it requires a shell. It also requires me to restart containers when something changes (unlike docker compose with volume mounts). Although I'd really like to, I don't really see myself using it as it is right now. I might reconsider if it becomes more akin to docker compose.

    Makes sense too. Backgrounding services like that is a pretty significant can of worms for various reasons, but the utility is undeniable I agree and it has come up more than once. It'll have to be out of scope to this effort too but if you want to open a separate issue requesting support for it and describing the use case that would definitely be appreciated and we can keep it on our radar if nothing else.

  5. sipsma commented on Jan 3, 2024

    @sipsma
    ContributorAuthor

    The initial PR for exposing core APIs in dagger call is merged: #6293

    I created a handful of follow up issues for the rest of the major adjustments needed:

    I'll also leave this issue open for the final consolidation to a single command we can do once those have been tackled.

  6. helderco commented on Jan 29, 2024

    @helderco
    Contributor

    Now that we have core types chaining:

    And since we’ve consolidated the commands:

    I think we can close this issue and track the remaining work in their own issues.

  7. helderco commented on Jan 29, 2024

    @helderco
    Contributor

    Note that I've added the removed commands, just to make it easier for users to know:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions