Skip to content

Commit

Permalink
Add version information for minder CLI (#1501)
Browse files Browse the repository at this point in the history
This creates a `version` subcommand so that folks may get version
information easier.

It also adds the version information as part of the GRPC user agent,
so we would be able to gather metrics on the clients that are used.
  • Loading branch information
JAORMX authored Nov 6, 2023
1 parent a3de303 commit 496dd08
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 4 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ builds:
- "-X main.Commit={{ .Env.COMMIT }}"
- "-X main.CommitDate={{ .Env.COMMIT_DATE }}"
- "-X main.TreeState={{ .Env.TREE_STATE }}"
- "-X github.com/stacklok/minder/internal/constants.CLIVersion={{ .Env.VERSION }}"
goos:
- linux
- windows
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ cli-docs:
@go run -tags '$(BUILDTAGS)' cmd/cli/main.go docs

build: ## build golang binary
# @go build -ldflags "-X main.version=$(shell git describe --abbrev=0 --tags)" -o bin/$(projectname)
CGO_ENABLED=0 go build -trimpath -tags '$(BUILDTAGS)' -o ./bin/minder ./cmd/cli
CGO_ENABLED=0 go build \
-trimpath \
-tags '$(BUILDTAGS)' \
-ldflags "-X github.com/stacklok/minder/internal/constants.CLIVersion=$(shell git describe --abbrev=0 --tags)+ref.$(shell git rev-parse --short HEAD)" \
-o ./bin/minder ./cmd/cli
CGO_ENABLED=0 go build -trimpath -tags '$(BUILDTAGS)' -o ./bin/$(projectname)-server ./cmd/server
CGO_ENABLED=0 go build -trimpath -tags '$(BUILDTAGS)' -o ./bin/medev ./cmd/dev

Expand Down
39 changes: 39 additions & 0 deletions cmd/cli/app/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Copyright 2023 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package version provides the version command for the minder CLI
package version

import (
"github.com/spf13/cobra"

"github.com/stacklok/minder/cmd/cli/app"
"github.com/stacklok/minder/internal/constants"
"github.com/stacklok/minder/internal/util/cli"
)

// VersionCmd is the version command
var VersionCmd = &cobra.Command{
Use: "version",
Short: "Print the version of the minder CLI",
Long: `The minder version command prints the version of the minder CLI.`,
Run: func(cmd *cobra.Command, _ []string) {
cli.PrintCmd(cmd, constants.VerboseCLIVersion)
},
}

func init() {
app.RootCmd.AddCommand(VersionCmd)
}
1 change: 1 addition & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
_ "github.com/stacklok/minder/cmd/cli/app/provider"
_ "github.com/stacklok/minder/cmd/cli/app/repo"
_ "github.com/stacklok/minder/cmd/cli/app/rule_type"
_ "github.com/stacklok/minder/cmd/cli/app/version"
)

func main() {
Expand Down
90 changes: 90 additions & 0 deletions internal/constants/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// Copyright 2023 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package constants

import (
"runtime/debug"
"strings"
"text/template"
)

var (
// CLIVersion is the version of the application. Note that this is
// set at compile time using ldflags.
CLIVersion = "no-info"
// VerboseCLIVersion is the verbose version of the application.
// Note that this is set up at init time.
VerboseCLIVersion = ""
)

type versionStruct struct {
Version string
GoVersion string
Time string
Commit string
OS string
Arch string
Modified bool
}

const (
verboseTemplate = `Version: {{.Version}}
Go Version: {{.GoVersion}}
Git Commit: {{.Commit}}
Built: {{.Time}}
OS/Arch: {{.OS}}/{{.Arch}}
Dirty: {{.Modified}}`
)

func init() {
bi, ok := debug.ReadBuildInfo()
if !ok {
return
}

var vvs versionStruct

vvs.Version = CLIVersion
vvs.GoVersion = bi.GoVersion

for _, kv := range bi.Settings {
switch kv.Key {
case "vcs.time":
vvs.Time = kv.Value
case "vcs.revision":
vvs.Commit = kv.Value
case "vcs.modified":
vvs.Modified = kv.Value == "true"
case "GOOS":
vvs.OS = kv.Value
case "GOARCH":
vvs.Arch = kv.Value
}
}

VerboseCLIVersion = vvs.String()
}

func (vvs *versionStruct) String() string {
stringBuilder := &strings.Builder{}
tmpl := template.Must(template.New("version").Parse(verboseTemplate))
err := tmpl.Execute(stringBuilder, vvs)
if err != nil {
panic(err)
}

return stringBuilder.String()
}
7 changes: 5 additions & 2 deletions internal/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,11 @@ func GetGrpcConnection(grpc_host string, grpc_port int, allowInsecure bool, issu
}

// generate credentials
conn, err := grpc.Dial(address, grpc.WithTransportCredentials(credentialOpts),
grpc.WithPerRPCCredentials(JWTTokenCredentials{accessToken: token}))
conn, err := grpc.Dial(
address, grpc.WithTransportCredentials(credentialOpts),
grpc.WithPerRPCCredentials(JWTTokenCredentials{accessToken: token}),
grpc.WithUserAgent(fmt.Sprintf("minder-cli/%s", constants.CLIVersion)),
)
if err != nil {
return nil, fmt.Errorf("error connecting to gRPC server: %v", err)
}
Expand Down

0 comments on commit 496dd08

Please sign in to comment.