Skip to content

Commit 2f391e1

Browse files
perhammanton.voskresensky
andauthored
add version flag
Co-authored-by: anton.voskresensky <anton.voskresensky@flant.com>
1 parent 58e23b1 commit 2f391e1

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

.github/workflows/docker.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
with:
4747
context: .
4848
build-args: |
49-
versionflags=-X 'github.com/flant/osctl/pkg/util.Version=${{ github.ref_name }}' -X 'github.com/flant/osctl/pkg/util.Revision=${{ github.sha }}' -X 'github.com/flant/osctl/pkg/util.Branch=${{ github.ref_name }}' -X 'github.com/flant/osctl/pkg/util.BuildUser=${{ github.actor }}' -X 'github.com/flant/osctl/pkg/util.BuildDate=${{ env.BUILD_DATE }}'
49+
versionflags=-X 'osctl/cmd/main.appVersion=${{ github.ref_name }}'
5050
push: ${{ github.event_name != 'pull_request' }}
5151
tags: ${{ steps.meta.outputs.tags }}
5252
labels: ${{ steps.meta.outputs.labels }}

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020

2121
- name: Build
2222
run: |
23-
versionflags="-X 'github.com/flant/osctl/pkg/util.Version=$GITHUB_REF_NAME' -X 'github.com/flant/osctl/pkg/util.Revision=$GITHUB_SHA' -X 'github.com/flant/osctl/pkg/util.Branch=$GITHUB_REF_NAME' -X 'github.com/flant/osctl/pkg/util.BuildUser=$GITHUB_ACTOR' -X 'github.com/flant/osctl/pkg/util.BuildDate=$(date +%Y%m%d%H%M)'"
23+
versionflags="-X 'osctl/cmd/main.appVersion=${{ github.ref_name }}'"
2424
for GOOS in linux; do
2525
for GOARCH in amd64 arm64; do
2626
export GOOS GOARCH

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ RUN chmod +x /app/osctl
4242

4343
ENV PATH="/app:${PATH}"
4444

45+
RUN mkdir -p /etc/bash_completion.d
4546
RUN /app/osctl completion bash > /etc/bash_completion.d/osctl
4647

4748
RUN echo '[ -f /etc/bash_completion ] && . /etc/bash_completion' >> /etc/bash.bashrc

cmd/main.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
56

67
"osctl/commands"
78
)
89

910
var (
10-
vBuild string
11+
appVersion string
1112
)
1213

1314
func main() {
14-
if err := commands.Execute(); err != nil {
15+
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-v") {
16+
if appVersion == "" {
17+
appVersion = "dev"
18+
}
19+
fmt.Println(appVersion)
20+
os.Exit(0)
21+
}
22+
23+
if err := commands.Execute(appVersion); err != nil {
1524
os.Exit(1)
1625
}
1726
}

commands/root.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ package commands
33
import (
44
"fmt"
55
"osctl/pkg/config"
6+
"osctl/pkg/logging"
67

78
"github.com/spf13/cobra"
89
)
910

11+
var (
12+
appVersion = ""
13+
)
14+
1015
var rootCmd = &cobra.Command{
1116
Use: "osctl",
1217
Short: "OpenSearch indices lifecycle management tool",
1318
Long: `osctl is a tool for managing OpenSearch cluster indices lifecycle.`,
1419
RunE: func(cmd *cobra.Command, args []string) error {
15-
1620
actionFlag, _ := cmd.Flags().GetString("action")
1721
if actionFlag != "" {
1822
if err := config.ValidateAction(actionFlag); err != nil {
@@ -44,7 +48,16 @@ var rootCmd = &cobra.Command{
4448
},
4549
}
4650

47-
func Execute() error {
51+
func Execute(version string) error {
52+
appVersion = version
53+
if appVersion == "" {
54+
appVersion = "dev"
55+
}
56+
rootCmd.Version = appVersion
57+
58+
logger := logging.NewLogger()
59+
logger.Info(fmt.Sprintf("osctl version=%s", appVersion))
60+
4861
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
4962
commandName := cmd.Name()
5063
if commandName == "osctl" {
@@ -106,6 +119,8 @@ func executeActionCommand(action string, args []string) error {
106119
func init() {
107120
addFlags(rootCmd)
108121
rootCmd.SilenceUsage = true
122+
rootCmd.Flags().BoolP("version", "v", false, "Print version information")
123+
rootCmd.SetVersionTemplate("{{.Version}}\n")
109124
commands := []*cobra.Command{
110125
snapshotCmd,
111126
snapshotManualCmd,

0 commit comments

Comments
 (0)