Skip to content

Commit

Permalink
Add version (#154)
Browse files Browse the repository at this point in the history
* Add version command

Adding tool version to CLI that should return version number and commit
hash. Usage: `kantra version`.

Fixes: https://issues.redhat.com/browse/MTA-2201

Signed-off-by: Marek Aufart <maufart@redhat.com>

* Update build flags

Signed-off-by: Marek Aufart <maufart@redhat.com>

* Update build scripts

Signed-off-by: Marek Aufart <maufart@redhat.com>

* Try fix buildah cmd

Signed-off-by: Marek Aufart <maufart@redhat.com>

* Set buildcommit before

Signed-off-by: Marek Aufart <maufart@redhat.com>

* Update cmd/version.go

Co-authored-by: David Zager <dzager@redhat.com>
Signed-off-by: Marek Aufart <aufi.cz@gmail.com>

---------

Signed-off-by: Marek Aufart <maufart@redhat.com>
Signed-off-by: Marek Aufart <aufi.cz@gmail.com>
Co-authored-by: David Zager <dzager@redhat.com>
  • Loading branch information
aufi and djzager committed Feb 16, 2024
1 parent dcfad6b commit 2437f36
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/multi-arch-image-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
sed -i "s,FROM quay.io/konveyor/windup-shim\:latest,FROM quay.io/konveyor/windup-shim:${TAG}," Dockerfile
sed -i "s,FROM quay.io/konveyor/static-report\:latest,FROM quay.io/konveyor/static-report:${TAG}," Dockerfile
sed -i "s,FROM quay.io/konveyor/analyzer-lsp\:latest,FROM quay.io/konveyor/analyzer-lsp:${TAG}," Dockerfile
export BUILD_COMMIT=$(git rev-parse HEAD)
extra-args: |
--build-arg VERSION=${tag} --build-arg BUILD_COMMIT=${BUILD_COMMIT}
secrets:
registry_username: ${{ secrets.QUAY_PUBLISH_ROBOT }}
registry_password: ${{ secrets.QUAY_PUBLISH_TOKEN }}
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ COPY main.go main.go
COPY cmd/ cmd/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o kantra main.go
RUN CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -a -o darwin-kantra main.go
RUN CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -a -o windows-kantra main.go
ARG VERSION
ARG BUILD_COMMIT
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT'" -a -o kantra main.go
RUN CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT'" -a -o darwin-kantra main.go
RUN CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT'" -a -o windows-kantra main.go

FROM quay.io/konveyor/analyzer-lsp:latest

Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var noCleanup bool

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Short: "A cli tool for analysis and transformation of applications",
Short: "A CLI tool for analysis and transformation of applications",
Long: ``,
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
Expand All @@ -46,6 +46,7 @@ func init() {
logger := logrusr.New(logrusLog)
rootCmd.AddCommand(NewTransformCommand(logger))
rootCmd.AddCommand(NewAnalyzeCmd(logger))
rootCmd.AddCommand(NewVersionCommand())
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
28 changes: 28 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var (
BuildCommit = ""
Version = "v99.0.0"
)

// Use build flags to set correct Version and BuildCommit
// e.g.:
// --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=1.2.3' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$(git rev-parse HEAD)'"
func NewVersionCommand() *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the tool version",
Long: "Print this tool version number",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("version: %s\n", Version)
fmt.Printf("SHA: %s\n", BuildCommit)
},
}
return versionCmd
}

0 comments on commit 2437f36

Please sign in to comment.