-
Notifications
You must be signed in to change notification settings - Fork 27
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
Add --version flag to pelican-client #199
Conversation
On the Cobra side -- would it be better to take the approach here: https://gianarb.it/blog/golang-mockmania-cli-command-with-cobra where we invoke the cobra command object directly and provide it with a byte buffer? E.g.,
This is a little less thorough than invoking the top-level function (meaning it'd potentially miss bugs). Would it simplify the unit test though? |
Yeah I think for Cobra this would be nice way to unit test instead of invoking the main function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As is, it looks good to me. I'll approve for now and let you decide if you want to simplify the unit test. If you decide not to change anything, go ahead and merge when you get the chance. If you make changes, I'll re-review.
I think for the |
According to issue #150 , we want to expose
--version
flag for pelican-client and display the version/build info to the user in a similar behavior asstashcp
did. This PR addressed this issue but in a bit hacky way under various constraints from cobra.PersistentPreRun
function, to add one handler for the root command and its child subcommands. However, cobra will not executePersistentPreRun
function for the command if theRun
function of the command is not defined. See issue 2039. For us, many subcommands are only a level of hierarchy and won't handle any command logic themselves, such asorigin
. AddingRun
functions will break it.main
function of the command, i.e. inroot.go
. However, the flag value won't be evaluated untilExecute()
function is called, which means we can't do anything using cobra inroot.go
--version
logic inmain.go
by reading the last command line argument. This means that for any command ending with--version
, the version info will be popped up no matter what command/argument/flag before it was. This should be the expected behavior for the--version
flag.--help
command will it as a global flag to users, but cobra didn't handle any of the actual logic.-v
shorthand was disabled because we had one reserved fororigin serve
where-v
means the volume to attach to the origin server.Another note for those who want to improve the test coverage for the
cmd
package. You might want to clear theos.Args
before your test logic and restore it afterwards if you are not testing cobra but just the manually-added CLI logic. I had this issue with the test case in CI for Windows OS where the test will pass some weird arguments to mymain.go
(which wasn't even part of my testing code) and cause the test case fail by exiting the program withcommand not found
error.