Skip to content
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

Refactor the cli functions to use urfave/cli instead of cobra. #218

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

souleb
Copy link
Contributor

@souleb souleb commented Nov 5, 2021

Signed-off-by: Soule BA soule@weave.works

kind/refactor

What this PR does / why we need it:
Refactor the cli functions to use urfave/cli instead of cobra.
Viper is replaced by urfave/cli atlsrc.

A few variable have been renamed to match their default conterparts, e.g.
cfg.CtrKernelSnapshotter to match defaults.ContainerdKernelSnapshotter

this fixes #81

Special notes for your reviewer:

Checklist:

  • squashed commits into logical changes
  • includes documentation
  • adds unit tests
  • adds or updates e2e tests

@souleb souleb marked this pull request as draft November 5, 2021 11:12
@codecov-commenter
Copy link

codecov-commenter commented Nov 5, 2021

Codecov Report

Merging #218 (f099f13) into main (aa5916e) will decrease coverage by 0.18%.
The diff coverage is 0.00%.

❗ Current head f099f13 differs from pull request most recent head 1c80a95. Consider uploading reports for the commit 1c80a95 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main     #218      +/-   ##
==========================================
- Coverage   40.29%   40.11%   -0.19%     
==========================================
  Files          46       46              
  Lines        2169     2179      +10     
==========================================
  Hits          874      874              
- Misses       1236     1246      +10     
  Partials       59       59              
Impacted Files Coverage Δ
pkg/log/flags.go 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 17de35e...1c80a95. Read the comment docs.

@yitsushi yitsushi added the kind/refactor Only refactoring changes label Nov 8, 2021
@souleb souleb force-pushed the cobra-to-urfavecli branch 10 times, most recently from bc5ee7f to 49d5d24 Compare November 12, 2021 11:26
@souleb souleb marked this pull request as ready for review November 12, 2021 11:26
}

logger := log.GetLogger(context.Context)
logger.Infof(
Copy link
Contributor

Choose a reason for hiding this comment

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

We could probably move the version output to the root command?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually how does this work? Is there the equivalent of the persistent pre-run?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no persistent pre-run or persistent flag. App.Before does what pre-run does. And we have access to parent flags through the context.

Now I think moving this to root command (app) is a good idea. But it will be shown only when calling that command

Copy link
Contributor

Choose a reason for hiding this comment

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

App.before on the App root will run on any subcommand right?

	// An action to execute before any subcommands are run, but after the context is ready
	// If a non-nil error is returned, no subcommands are run
	Before BeforeFunc

https://github.com/urfave/cli/blob/v1.22.5/app.go#L28

Or is that what you're saying? Maybe I'm not understanding

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm sorry I forgot the context of this comment. But yeah App.before always run. A subCommand can also declare command.Before.

internal/command/root.go Outdated Show resolved Hide resolved

if err := cmdflags.AddHiddenFlagsToCommand(cmd, cfg); err != nil {
return nil, fmt.Errorf("adding hidden flags to run command: %w", err)
if cfg.ParentIface == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does urfave/cli handle required fields and raising an error if its missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes there is required: true, but the way it behave is that you have to set the flag manually, I think the check happens before the command.before where we set the flag with the config file, causing it to fail

Copy link
Member

Choose a reason for hiding this comment

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

is it possible to set a config file and flags at the same time? which takes precedence?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The precedence for flag value sources is as follows (highest to lowest):

  1. Command line flag value from user
  2. Environment variable (if specified)
  3. Configuration file (if specified)
  4. Default defined on the flag

@souleb souleb force-pushed the cobra-to-urfavecli branch 4 times, most recently from dd6917b to 2225d51 Compare November 17, 2021 09:33
A few variable have been renamed to match, e.g.
cfg.CtrKernelSnapshotter to match defaults.ContainerdKernelSnapshotter

Signed-off-by: Soule BA <soule@weave.works>
@github-actions
Copy link
Contributor

This PR is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Dec 18, 2021
@richardcase
Copy link
Contributor

I need to properly review this

if err := rootCmd.Execute(); err != nil {
log.Fatalln(err)
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "flintlockd: %s\n", err)
Copy link
Contributor

@jmickey jmickey Jan 12, 2022

Choose a reason for hiding this comment

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

Should this be log.Error?

@richardcase richardcase removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 20, 2022
@github-actions
Copy link
Contributor

This PR is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 20, 2022
@Callisto13 Callisto13 removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 21, 2022
@github-actions
Copy link
Contributor

This PR is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 19, 2023
@yitsushi
Copy link
Contributor

Is this still in progress ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/refactor Only refactoring changes lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change to urfave/cli
6 participants