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

⚠Provide version information as a CLI option #1773

Merged
merged 1 commit into from
Nov 11, 2020

Conversation

Adirio
Copy link
Contributor

@Adirio Adirio commented Nov 3, 2020

Description

Provide a WithVersion CLI Option that allows to provide a version string instead of having to create a version command and provide it as an extra command. When a version is provided this way, the CLI will create a version command automatically that prints this version string.

Changes

  • ✨ New argument type for cli.New: WithVersion. Creating a command and providing it with WithExtraCommands should still work with the previous behavior, so this is not a breaking change, but frameworks using kubebuilder should migrate to the new approach.
  • ⚠️ Removed Version and NewCmd from package cmd/version. This package is not part of the library itself, but as it is exported, it may be used by third parties.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Nov 3, 2020
@Adirio
Copy link
Contributor Author

Adirio commented Nov 3, 2020

@camilamacedo86 This PR goes in the same direction as #1691, moving another command out of the extra commands. I think that the API for frameworks with the new options is clearer, they only have to provide a string now instead of a full COBRA command, and it also has the added benefot of including kubebuilder --version and kubebuilder -v in addition to kubebuilder version. WDYT?

@Adirio
Copy link
Contributor Author

Adirio commented Nov 4, 2020

A note in coverage: this PR moves part of the remaining code from cmd to pkg. As cmd was not being checked by coveralls, it says that the coverage has decreased, but in reality we just have included in the covered package part of the code that wasn't being covered by coveralls.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 4, 2020
cmd/main.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 4, 2020
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 4, 2020
@Adirio Adirio closed this Nov 5, 2020
@Adirio Adirio reopened this Nov 5, 2020
Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

I am ok with. 👍 I have just a nit. See; https://github.com/kubernetes-sigs/kubebuilder/pull/1773/files#r518800235
Otherwise:
/approve

@estroz do you see any reason for we do not move forward here? WDYT?

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 6, 2020
pkg/cli/cli.go Outdated
@@ -424,7 +442,7 @@ After the scaffold is written, api will run make on the project.
make run
`,
c.commandName, c.commandName),

Version: c.version,
Copy link
Contributor

Choose a reason for hiding this comment

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

According to the docs, this is only necessary if the command doesn't define a version command.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to the docs this has to do with -v and --version flags, not with version command:

// Version defines the version for this command. If this value is non-empty and the command does not
// define a "version" flag, a "version" boolean flag will be added to the command and, if specified,
// will print content of the "Version" variable. A shorthand "v" flag will also be added if the
// command does not define one.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I read that wrong. I don't think we want to expose a --version and a version command though.

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 don't see any damage done by being able to do kubebuilder version, kubebuilder --version and kubebuilder -v. I think it actually improves UX as it doesn't have to know if it is a command or a flag. kubebuilder help and kubebuilder --help have the same behavior, right?

pkg/cli/version.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 8, 2020
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 8, 2020
@Adirio
Copy link
Contributor Author

Adirio commented Nov 10, 2020

@estroz can we merge this? The only remaining comment is that you wouldn't add kubebuilder --version in addition to kubebuilder version but I don't see any harm indoing so. Do you have any reason to say so?

@estroz
Copy link
Contributor

estroz commented Nov 10, 2020

@Adirio the only hesitation I have is that most k8s tools like kubectl only have the version command, and I'd prefer to adhere to that standard in kubebuilder. Also this reserves -v for verbose mode, which is also common.

@Adirio
Copy link
Contributor Author

Adirio commented Nov 10, 2020

@estroz Removed the --version and -v flags.

P.S.: if we set a -v flag for verbose, setting Version won't create that flag.

@estroz
Copy link
Contributor

estroz commented Nov 10, 2020

/retest

# TODO: what does this thing do.
opts=-ldflags "-X sigs.k8s.io/kubebuilder/v2/cmd/version.kubeBuilderVersion=$INJECT_KB_VERSION"
# Injects the version into the cmd/version.go file
opts=-ldflags "-X sigs.k8s.io/kubebuilder/v2/cmd.kubeBuilderVersion=$INJECT_KB_VERSION"
Copy link
Contributor

Choose a reason for hiding this comment

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

Wdyt about adding adding something like this to make build, ex.

BUILD_VERSION=$(shell git descripe --dirty --tags --always)
BUILD_COMMIT=$(shell git rev-parse HEAD)
LD_FLAGS=-ldflags "-X sigs.k8s.io/kubebuilder/v2/cmd.kubeBuilderVersion=$(BUILD_VERSION) -X sigs.k8s.io/kubebuilder/v2/cmd.gitCommit=$(BUILD_COMMIT)"
build: ## Build the project locally
        go build $(LD_FLAGS) -o bin/kubebuilder ./cmd

perhaps in a follow-up?

Copy link
Contributor Author

@Adirio Adirio Nov 11, 2020

Choose a reason for hiding this comment

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

@estroz I tracked this change in an Issue some time ago: #1401
goos, goarch and buildDate should also be doable.

Copy link
Contributor

@estroz estroz left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 10, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Adirio, camilamacedo86, estroz

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [camilamacedo86,estroz]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit c158f4f into kubernetes-sigs:master Nov 11, 2020
@Adirio Adirio deleted the version-cmd branch November 11, 2020 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants