Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Transition to Cobra CLI #333

Merged
merged 166 commits into from
Feb 12, 2021
Merged

Transition to Cobra CLI #333

merged 166 commits into from
Feb 12, 2021

Conversation

hculea
Copy link
Member

@hculea hculea commented Aug 14, 2020

The main differences from the KingPin Version of the CLI:

  • The versioning system: while in the previous version commands like secrethub read --version would have been valid, in this version, the --version flag can only be called on the root command, i.e. secrethub --version.
  • When the autocompletion script is installed (see the installation guide), there are flags which already have static autocompletion enabled. (for example, secrethub audit --output-format [tab][tab] will autocomplete to table or json ).
  • The bug involving the NoEnvVar function from env.go not removing the environment variable corresponding to the flag it was called on has been solved.
  • Adds -h shorthand for help texts.
  • Shows help text if you don't provide a subcommand where one is required, e.g. secrethub repo

We no longer have to check whether the flag is persistent, since the bug with env var names is no longer present due to other enhancements.
flag.Changed() is not set if the flag is set through an environment variable.
Remove caching behaviour, since it does not bring any value to flag creation.
For AWS service init, the description would be "AWS role <role-name>" without replacing the placeholder properly. Now it was fixed for both aws and gcp.
This way it is clearer when what kind error is thrown and as an added bonus, we can rely more on the compiler to verify code correctness.
Fixup of previous commit
Copy link
Member

@jpcoenen jpcoenen left a comment

Choose a reason for hiding this comment

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

I would say it's half-past-launch-time!

Because of the long list of commits that have gone back and forth, I think we should consider squashing these commits @florisvdg @SimonBarendse any opinions on this?

.gitignore Outdated Show resolved Hide resolved
@SimonBarendse
Copy link
Member

SimonBarendse commented Jan 27, 2021

I'm usually not a big fan of squashing, as we may lose some context and motivation for a change. However, I can see how the current commit history of going back-and-forth may not be ideal to find the whole set of changes + motivation either. Therefore, I'd like to make an exception to my rule for this PR and suggest to squash the changes. The commit message of the squash should be very verbose and contain motivation for most of the changes.

In my case all the files made by the IDE would show when I wanted to make a commit. Thanks to Simon's tip, this can be solved with a global gitignore to no longer have the same thing on other repos as well.
@edif2008 edif2008 requested review from Marton6 and removed request for Marton6 February 1, 2021 12:46
@edif2008
Copy link
Contributor

edif2008 commented Feb 2, 2021

Here is a draft of the squash commit message.

Commit: Switch the CLI library from KingPin to Cobra

Description:

The change to cobra is made because KingPin is no longer maintained, and Cobra comes with more enhancements than KingPin.
The main one is enabling autocompletion for arguments besides command and flag completion.

The following list of key changes have been made in the codebase:

  • add -h shorthand for help text
  • refactor env.go file's functions into argument.go, app.go, and flag.go
  • the versioning system: while in the previous version commands like secrethub read --version would have been valid, in this version, the --version flag can only be called on the root command, i.e. secrethub --version.
  • the bug involving the NoEnvVar function from env.go not removing the environment variable corresponding to the flag it was called on has been solved.
  • custom template similar to cobra's

For a more detailed version of the changes in the codebase, below is a sqashed version of the commits:

  • 483605e - ba69bc7 -> Implement a first protorype of secrethub-cli with cobra (only secrethub read works)
  • 7f24ee3 - 1b81c0c -> Change creation of all commands to a cobra-style one (based on first prototype)
  • 5d1cddf - de0a964 -> Simplify command creation functionality, add completion functionality
  • c16cb7c -> Remove dynamic path autocompletion (will be done in a seperate PR)
  • 337b3d6 - 35c1b6a -> Make minor adjustments to the error message, improve code
  • a6e678c - 7e519c6 -> Globalize argument registration
  • 673bde5 -> Refactor command registerer
  • f53515f - 39e7352 -> Improve flag creation functionality to make it similar to cobra's syntax
  • b17eb7c -> Rename command receiver
  • 3dc680d - 75f7fc2 -> Create an Argument struct for optimising argument creation and error handling
  • d5c03e6 - 6d075df -> Implement a custom template for help text
  • 2bf3b2b - 1753b22 -> Remove commeted code from KingPin
  • 4070695 -> Adjust argument types
  • 2bd4055 - 093e421 -> Update branch with new features from develop
  • 6419165 - b6f159e -> Enable automcompletion installation from cli for bash, adjust arg struct, fix global flags
  • a9b26c5 - 883cbad -> Attach env var value to the flag value
  • 6b971e9 - deb7e11 -> Refactor cli structs (env, app, argument, flag)
  • 5bf329b - b103c0e -> Fix completion error in installing SecretHub with .deb and .rpm packages, remove autocopletion installation
  • 8591027 - 52d1c98 -> Improve code quality (better naming, removing comments, minor fixes)
  • 81905e1 - bb65d49 -> Simplify debug and mlock flag creation
  • 8ef95aa - 930ced9 -> Implement hidden command, document template functions, fix mkdir and persistent flags envar names, make CLI work with demo, simplify template
  • 8c1a534 - 4051c7c -> Process environment variables for global flags in PersistentRunE, adjust argument struct fields, improve help and error texts, change PreRunE addition function signature
  • f7560ef - 4c4ff88 -> Improve persistent flag functionality, fix mlock and debug flags, improve PreRunE addition function, simplify structures used as argument values, add error message for minimum arguments
  • 3e66d13 - 34eac77 -> Fix default description for service init, improve argument error handling
  • cfe813c - 961229a -> Make small final touches

@edif2008
Copy link
Contributor

edif2008 commented Feb 12, 2021

Here is the new draft of the squash commit

Commit: Switch the CLI library from KingPin to Cobra

Description:

The change to Cobra is made for two main reasons:

  • KingPin is no longer maintained
  • Cobra enables argument autocompletion in addition to command and flag completion

Also, Cobra comes with some other enhancements that make the CLI construction simpler and more consistent.

The following list of key changes have been made in the codebase:

  1. Visible to the user:

    • custom template that is similar to cobra's; the following additions on top of the default cobra template were made:
      • group commands with subcommands under Management Commands section
      • add Arguments section for explaining the arguments
      • divide the help text from command's/flag's name in a separate column for readibility
    • add -h shorthand for help text
    • automatically show help text if the user does not provide a subcommand where one is required, e.g. secrethub repo
    • when the user does not provide the required number of arguments, the CLI will provide an error message similar to docker's
    • the versioning system: while in the previous version commands like secrethub read --version would have been valid, in this version, the --version flag can only be called on the root command, i.e. secrethub --version.
  2. Visible to the developer

    • refactor env.go file's functions into argument.go, app.go, and flag.go for better maintainability
    • cobra's cutom template can be adjusted in cobra_template.go, located now in internals/cli
    • flag registration has a similar syntax to cobra's
    • argument registration is made using BindArguments and BindArgumentsArr (for commands that accept multiple arguments of the same type, e.g. secrethub mkdir), since KingPin includes argumnet registration, while cobra does not; this function enables the developer to save the argument's value to a variable that can be later used when executing the command
    • the bug involving the NoEnvVar function not removing the environment variable corresponding to the flag it was called on has been solved
    • CommandClause has two more new fields: Args (for linking variables to argument values), and flags (for enabling the link between the flag and its euqivalent environment variable)

@edif2008 edif2008 merged commit 078f627 into develop Feb 12, 2021
@SimonBarendse SimonBarendse mentioned this pull request Apr 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants