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

Rewrite CLI: nknc and nknd #857

Merged
merged 1 commit into from
Jun 21, 2022
Merged

Rewrite CLI: nknc and nknd #857

merged 1 commit into from
Jun 21, 2022

Conversation

omani
Copy link
Contributor

@omani omani commented May 15, 2022

Proposed changes in this pull request

Switch from urfave/cli to spf13/cobra. Same functionality, just different cli library, resulting in less loc.

Additionally, minor changes in code to fix a few warnings by compiler.

Type (put an x where ever applicable)

  • Bug fix: Link to the issue
  • Feature (Non-breaking change)
  • Feature (Breaking change)
  • Documentation Improvement

Extra information

cobra will have a new release around June 2022 with new features like MarkFlagsRequiredTogether() and MarkFlagsMutuallyExclusive() which will reduce the loc even further, since we won't have to check for set flags manually anymore.

Im planning to implement them once cobra releases v1.5.0.

@omani omani force-pushed the rewrite_nknd branch 2 times, most recently from 1e63b23 to 39a7704 Compare May 15, 2022 00:48
@omani omani changed the title Rewrite: /cmd/nknd Rewrite: /cmd/nknd cli May 15, 2022
cmd/nknd/commands/root.go Outdated Show resolved Hide resolved
timer.Reset(30 * time.Minute)
break
Copy link
Member

Choose a reason for hiding this comment

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

break should be changed to continue since you removed select

@@ -1,4 +1,4 @@
package main
package cmd
Copy link
Member

Choose a reason for hiding this comment

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

We follow the convention to match package name with directory name. Since the directory name is commands, the package name should also be commands. Same for 'root.go

@yilunzhang
Copy link
Member

I think it's better to submit the changes for nknc in the same PR so we can remove the dependency of urfave/cli.

@omani
Copy link
Contributor Author

omani commented May 18, 2022

pushed nknc cli. fixed review.

@omani omani changed the title Rewrite: /cmd/nknd cli Rewrite CLI: nknc and nknd May 18, 2022
@omani omani requested review from yilunzhang and gdmmx May 18, 2022 22:03
cmd/nknd/commands/root.go Outdated Show resolved Hide resolved
cmd/nknc/commands/asset.go Outdated Show resolved Hide resolved
@omani
Copy link
Contributor Author

omani commented May 20, 2022

squashed.

cmd/nknc/commands/asset.go Outdated Show resolved Hide resolved
cmd/nknc/commands/asset.go Outdated Show resolved Hide resolved
cmd/nknc/commands/debug.go Outdated Show resolved Hide resolved
cmd/nknc/commands/id.go Outdated Show resolved Hide resolved
@yilunzhang
Copy link
Member

With the new structure, all commands (e.g. nknc wallet, nknc asset, etc) are defined in the same package, so all of them share private variables (e.g. fee for all commands are defined in asset.go). It's better to move those shared variable (e.g. fee, nonce) to a file that is not specific to a command, like root.go, this way removing or change one command won't affect the other ones.

@omani
Copy link
Contributor Author

omani commented May 27, 2022

With the new structure, all commands (e.g. nknc wallet, nknc asset, etc) are defined in the same package, so all of them share private variables (e.g. fee for all commands are defined in asset.go). It's better to move those shared variable (e.g. fee, nonce) to a file that is not specific to a command, like root.go, this way removing or change one command won't affect the other ones.

yeah I already moved all shared vars to root.go. and kept all local (file scoped) vars to the appropriate files. to distinguish them from global vars. or in other words: if any var is being used in more than one file, it is declared in root.go. any other var is in the file itself.

so, as I understand it, you want me to make all vars global?

@yilunzhang
Copy link
Member

yeah I already moved all shared vars to root.go. and kept all local (file scoped) vars to the appropriate files. to distinguish them from global vars. or in other words: if any var is being used in more than one file, it is declared in root.go. any other var is in the file itself.

This is exactly what I'm thinking as well, but there are a few variables that are used in more than one files but not defined in root.go. Specifically, fee and nonce are defined in asset.go but used in multiple files. I'm not sure if there are more variables like this but we can all check that.

@omani
Copy link
Contributor Author

omani commented May 27, 2022

This is exactly what I'm thinking as well, but there are a few variables that are used in more than one files but not defined in root.go. Specifically, fee and nonce are defined in asset.go but used in multiple files. I'm not sure if there are more variables like this but we can all check that.

ok. I will check everything and commit soon.

@omani
Copy link
Contributor Author

omani commented May 28, 2022

This is exactly what I'm thinking as well, but there are a few variables that are used in more than one files but not defined in root.go. Specifically, fee and nonce are defined in asset.go but used in multiple files. I'm not sure if there are more variables like this but we can all check that.

done

@omani omani force-pushed the rewrite_nknd branch 2 times, most recently from 57100ac to 02bbe3e Compare June 9, 2022 01:42
@omani omani requested a review from yilunzhang June 9, 2022 01:44
Copy link
Member

@yilunzhang yilunzhang left a comment

Choose a reason for hiding this comment

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

Looks good to me

@omani
Copy link
Contributor Author

omani commented Jun 9, 2022

rebased

Copy link
Member

@gdmmx gdmmx left a comment

Choose a reason for hiding this comment

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

LGTM

@yilunzhang
Copy link
Member

Upon further review it seems that print version feature is missing. Previously nknc --version and nknd --version will print out nknc/nknd version (set in Makefile https://github.com/nknorg/nkn/blob/master/Makefile#L13), but I can't find it in the new code. Is it something missing here?

@omani
Copy link
Contributor Author

omani commented Jun 14, 2022

Upon further review it seems that print version feature is missing. Previously nknc --version and nknd --version will print out nknc/nknd version (set in Makefile https://github.com/nknorg/nkn/blob/master/Makefile#L13), but I can't find it in the new code. Is it something missing here?

$ go run main.go --version
nknc version 1.0
$ go run main.go --version
nknd version 1.0

is what I get.

@yilunzhang
Copy link
Member

yilunzhang commented Jun 14, 2022

Yep that's what I meant by missing because it's always 1.0... The expected behavior is to print version set in Makefile, to be more specific, it should be git tag and commit hash. You should pass version when creating cobra.Command (to config.Version) like spf13/cobra#943 and also change the part in Makefile L14 from
github.com/nknorg/nkn/v2/cmd/nknc/common.Version=$(VERSION)
to
github.com/nknorg/nkn/v2/config.Version=$(VERSION)

@omani
Copy link
Contributor Author

omani commented Jun 14, 2022

NKND_BUILD_PARAM is already set to config.Version. can't we just get rid of NKNC_BUILD_PARAM in Makefile and (optionally rename the var to something better) and use only that var for both nknd and nknc? currently it only exists for assigning VERSION and doesnt do more.

@yilunzhang
Copy link
Member

For now they are the same, but as you can see they might contain different build parameters in the future, so it's better to keep both NKND_BUILD_PARAM and NKNC_BUILD_PARAM. If you want to reduce redundancy I would suggest something like this:

NKNC_BUILD_PARAM=$NKND_BUILD_PARAM

Switch from urfave/cli to spf13/cobra. Same functionality, just

different cli library, resulting in less loc.

Signed-off-by: omani <3346207+omani@users.noreply.github.com>
@omani
Copy link
Contributor Author

omani commented Jun 14, 2022

pls review the change. I kept the version flag for rootCmd only.

@yilunzhang
Copy link
Member

Looks good to me!

@yilunzhang yilunzhang merged commit 7cf5df1 into nknorg:master Jun 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants