From 5ef1c8a3918dc00f27b8f6a4dbfed9972b9d9104 Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Tue, 25 Oct 2022 22:24:22 +0100 Subject: [PATCH 1/2] chore: adding a --version flag --- Makefile | 2 +- pkg/cmd/permissions.go | 15 ++++++++++++++- pkg/version/info.go | 3 +++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 pkg/version/info.go diff --git a/Makefile b/Makefile index ce18fa8..705725e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ build: - go build -o kubectl-permissions cmd/kubectl-permissions.go + go build -o kubectl-permissions -trimpath -ldflags "-X github.com/garethjevans/permissions/pkg/version.Version=dev" cmd/kubectl-permissions.go install: build sudo cp -f kubectl-permissions /usr/local/bin diff --git a/pkg/cmd/permissions.go b/pkg/cmd/permissions.go index c7704fc..335f0f1 100644 --- a/pkg/cmd/permissions.go +++ b/pkg/cmd/permissions.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/garethjevans/permissions/pkg/asciitree" + "github.com/garethjevans/permissions/pkg/version" "github.com/kyokomi/emoji/v2" "github.com/mgutz/ansi" "github.com/spf13/cobra" @@ -38,6 +39,8 @@ type PermissionsOptions struct { configFlags *genericclioptions.ConfigFlags genericclioptions.IOStreams + Version bool + Cmd *cobra.Command Args []string } @@ -59,7 +62,7 @@ func NewCmdPermissions(streams genericclioptions.IOStreams) *cobra.Command { Short: "View the permissions inherited by the specified service account", Example: fmt.Sprintf(permissionsExample, binaryName), SilenceUsage: true, - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), RunE: func(c *cobra.Command, args []string) error { o.Args = args o.Cmd = c @@ -70,6 +73,8 @@ func NewCmdPermissions(streams genericclioptions.IOStreams) *cobra.Command { }, } + cmd.Flags().BoolVarP(&o.Version, "version", "v", false, "Display the version of the permissions plugin") + o.configFlags.AddFlags(cmd.Flags()) return cmd @@ -78,6 +83,14 @@ func NewCmdPermissions(streams genericclioptions.IOStreams) *cobra.Command { // Run lists all available namespaces on a user's KUBECONFIG or updates the // current context based on a provided namespace. func (o *PermissionsOptions) Run() error { + if o.Version { + fmt.Println(version.Version) + return nil + } + + if len(o.Args) != 1 { + panic(fmt.Sprintf("Error: accepts 1 arg(s), received %d", len(o.Args))) + } var err error config, err := o.configFlags.ToRESTConfig() diff --git a/pkg/version/info.go b/pkg/version/info.go new file mode 100644 index 0000000..7f6928e --- /dev/null +++ b/pkg/version/info.go @@ -0,0 +1,3 @@ +package version + +var Version string From fc2b7aeadcaa1c31e02635ae319a404cf38e5ad9 Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Tue, 25 Oct 2022 22:38:33 +0100 Subject: [PATCH 2/2] chore: configuring release version via goreleaser --- .github/workflows/release.yaml | 10 +++++++--- .goreleaser.yml | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 62db33a..727895c 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,8 +1,8 @@ name: release on: - push: - tags: - - 'v*.*.*' + release: + types: [published] + jobs: goreleaser: runs-on: ubuntu-latest @@ -11,10 +11,12 @@ jobs: uses: actions/checkout@v3 with: fetch-depth: 0 + - name: Setup Go uses: actions/setup-go@v1 with: go-version: 1.18 + - name: GoReleaser uses: goreleaser/goreleaser-action@v1 with: @@ -22,6 +24,8 @@ jobs: args: release --rm-dist env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + VERSION: ${{ github.event.release.tag_name }} + # until https://github.com/kubernetes-sigs/krew-index/pull/2661 is merged #- name: Update new version in krew-index #uses: rajatjindal/krew-release-bot@v0.0.25 diff --git a/.goreleaser.yml b/.goreleaser.yml index 9653c9b..2824b56 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -5,6 +5,8 @@ builds: - id: kubectl-permissions main: ./cmd/kubectl-permissions.go binary: kubectl-permissions + ldflags: + - -X github.com/garethjevans/permissions/pkg/version.Version={{.Version}} env: - CGO_ENABLED=0 goos: