Skip to content

Use spf13/cobra for the cli#22769

Merged
LK4D4 merged 8 commits intomoby:masterfrom
dnephin:integrate_cobra
Jun 1, 2016
Merged

Use spf13/cobra for the cli#22769
LK4D4 merged 8 commits intomoby:masterfrom
dnephin:integrate_cobra

Conversation

@dnephin
Copy link
Copy Markdown
Member

@dnephin dnephin commented May 16, 2016

https://github.com/spf13/cobra is a widely used framework for creating clis. I believe migrating to this framework comes with many advantages:

  • reduced duplication - currently usage strings exist in two places. Some of them are oddly centralized in a separate package, away from the command itself. There is also a lot of boilerplate for setting up and parsing args. Using cobra this is handled by the framework.
  • better organization - commands are split into separate packages for each group of commands. This structure seems to be a common convention used by many go clis. Separate packages means that compilation and unit tests will be faster. There is also a better structure for each command. The command definition, flags, options and implementation are all clearly separated instead of mixed into a single function.
  • less code to maintain - we can benefit from all the features in cobra without adding them to the docker cli framework
  • aliases and command migration - each command is an object that can be re-used, so if we wanted to migrate commands to a new structure, and keep backwards compatibility, this would allow us to accomplish such a migration very easily. (Ex: both docker images and docker image ls could exist by calling addCommand() on two different commands).

What I did

  • Moved more of the inspector/inspect functionality into api/client/inspect so that it can be used from multiple packages. This is necessary because with cobra the commands are in different packages, not a single package.
  • Created a new class CobraAdaptor that acts as an adaptor between the existing docker cli command delegation and the cobra command delegation. This allows us to migrate a few commands at a time, instead of doing it all in a single massive PR.
  • Use the existing DockerCli as a data object, and expose Out(), Err() and Client() so they can be used from separate packages.
  • Convert the volume commands to use cobra commands.

Comment thread cli/cobradaptor/adaptor.go Outdated
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It feels a little weird to me that the struct is called CobraAdaptor but the package name is cobradaptor (one fewer a). I don't have anything against the portmanteau but I'd rather not have two confusingly similar names.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fair enough, I'll make these consistent

@icecrime icecrime added the status/failing-ci Indicates that the PR in its current state fails the test suite label May 16, 2016
@cpuguy83
Copy link
Copy Markdown
Member

COBRA!

@MHBauer
Copy link
Copy Markdown
Contributor

MHBauer commented May 19, 2016

Does it also get rid of the reflection that ends up leaving the code oddly disconnected?

@cpuguy83
Copy link
Copy Markdown
Member

Reflection is already gone :)

@dnephin
Copy link
Copy Markdown
Member Author

dnephin commented May 20, 2016

A few tests are breaking because pflag doesn't do this: 0e9c40e

Personally that behaviour feels wrong. Do we want to continue to support that quote trimming?

@dnephin dnephin force-pushed the integrate_cobra branch from 8101266 to 49af78f Compare May 20, 2016 22:00
@LK4D4
Copy link
Copy Markdown
Contributor

LK4D4 commented May 23, 2016

@dnephin it actually maps to other cli stuff in linux. But I dunno, maybe we should ask @cyphar

@dnephin
Copy link
Copy Markdown
Member Author

dnephin commented May 23, 2016

@LK4D4 really? What other CLI removes quotes from the middle of an argument?

Normally the shell strips them off for you, but I don't think the application does anything with them.

@LK4D4
Copy link
Copy Markdown
Contributor

LK4D4 commented May 23, 2016

@dnephin yeah, maybe it's shell

@thaJeztah
Copy link
Copy Markdown
Member

I had a quick look at some features cobra offers, and it looks pretty neat. We may want to look into the auto-generation of man-pages and completion scripts if this is accepted, so "+1" from me

@cpuguy83
Copy link
Copy Markdown
Member

Design is OK for me.

@LK4D4
Copy link
Copy Markdown
Contributor

LK4D4 commented May 23, 2016

@dnephin I think it's okay to start to work on rebase and CI issues.

@cyphar
Copy link
Copy Markdown
Contributor

cyphar commented May 24, 2016

@dnephin @LK4D4 Yeah, I agree we can remove the quote trimming code. I'm not actually sure why I added it, but shells should be doing the trimming for us. :P

@dnephin dnephin force-pushed the integrate_cobra branch from 49af78f to 125a525 Compare May 24, 2016 22:40
@dnephin dnephin force-pushed the integrate_cobra branch 2 times, most recently from 5c6ddac to 7f5732c Compare May 25, 2016 22:45
@dnephin dnephin changed the title PoC: use spf13/cobra for the cli Use spf13/cobra for the cli May 26, 2016
@thaJeztah
Copy link
Copy Markdown
Member

ping @docker/core-engine-maintainers PTAL

@duglin
Copy link
Copy Markdown
Contributor

duglin commented May 31, 2016

@dnephin I gotta ask about something really odd with the old cli. If someone typed:

docker run -wl /tmp foo=bar ubuntu pwd

it would do some really weird parsing and connect the /tmp with -w and then foo=bar with -l.
Did you have to do something special to keep that odd parsing or does cobra automatically do that weird thing too?

btw - I say its weird because normally I don't think parsers do that. For example:
ps -C ps -O pid,state
is not the same as
ps -CO ps pid,state

@dnephin dnephin force-pushed the integrate_cobra branch from 4e087a9 to c8136e6 Compare May 31, 2016 21:48
@thaJeztah
Copy link
Copy Markdown
Member

@GordonTheTurtle died, so stay tuned while we're figuring that out, so don't merge before it's back

@dnephin
Copy link
Copy Markdown
Member Author

dnephin commented May 31, 2016

@thaJeztah just pushed a commit that marks -h as deprecated, so it's removed from the usage text, and it even prints "Flag shorthand -h has been deprecated, please use --help" if someone tries to use it.

@thaJeztah
Copy link
Copy Markdown
Member

@dnephin thanks! I'll give it a spin locally

@calavera
Copy link
Copy Markdown
Contributor

@duglin I think that behavior comes from the pflags library, and they should be compatible, but I haven't tested it.

@dnephin
Copy link
Copy Markdown
Member Author

dnephin commented May 31, 2016

@duglin hmm, I haven't encountered that yet, and I don't know if that works with cobra and pflags. Note this only converts the docker volume command for now, so I haven't tackled docker run yet.

Let me experiment and see how that works.

@duglin
Copy link
Copy Markdown
Contributor

duglin commented May 31, 2016

ah that would explain why it still parses in that weird way :-)

@duglin
Copy link
Copy Markdown
Contributor

duglin commented May 31, 2016

actually, if this just moves over "volume" I'd prefer if we did some other command (like "run") too because if we encounter something really nasty that would prevent us from migrating all commands then I'm not sure we should move over just some of them.

@LK4D4
Copy link
Copy Markdown
Contributor

LK4D4 commented May 31, 2016

I agree with @duglin . Run and would be great tests to see what can be broken.

@dnephin
Copy link
Copy Markdown
Member Author

dnephin commented May 31, 2016

run is going to be the largest piece of work. I've done some experimentation and haven't hit any major problems, but not everything is immediately obvious (like the parsing behaviour you described).

One known "limitation" (which is really by design) is that the very old flags that use single dash but multi character (-foo) are not supported. However maybe those have already been removed? I can't seem to find an example of that now, and I know they were deprecated a very long time ago.

I can start working on a PR to convert run, but it would be nice to keep it as a separate PR as this is already quite large.

I don't think we'll encounter any serious problems, we can always patch cobra/pflags to do what we need them to do.

@thaJeztah
Copy link
Copy Markdown
Member

@dnephin I think all single-dash flags have been removed https://github.com/docker/docker/blob/master/docs/deprecated.md#old-command-line-options

@dnephin
Copy link
Copy Markdown
Member Author

dnephin commented May 31, 2016

I'm going to create a separate PR to show docker run using cobra as a PoC. I'm not sure how many tests will break because of the single quote usage, but it should at least give us a good idea of what issues we can expect.

dnephin added 4 commits May 31, 2016 15:43
- adds support for usage strings on flag errors
- adds support for arg validation

Signed-off-by: Daniel Nephin <[email protected]>
Also re-use context.

Signed-off-by: Daniel Nephin <[email protected]>
Signed-off-by: Daniel Nephin <[email protected]>
@dnephin dnephin force-pushed the integrate_cobra branch from c8136e6 to 9b2bb64 Compare May 31, 2016 22:45
@tiborvass
Copy link
Copy Markdown
Contributor

@duglin I think the weird behavior of docker run -wl /tmp foo=bar ubuntu pwd qualifies as a bug. We should error out because you're not supposed to merge non-boolean short flags.

@LK4D4
Copy link
Copy Markdown
Contributor

LK4D4 commented Jun 1, 2016

LGTM
Let's test run in other PR

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.