Skip to content

Commit 0adfba8

Browse files
authored
add version subcommand to csmctl (#68)
Signed-off-by: Anurag <[email protected]>
1 parent ad4eac7 commit 0adfba8

File tree

6 files changed

+78
-3
lines changed

6 files changed

+78
-3
lines changed

.goreleaser.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ builds:
44
- binary: csctl
55
goos:
66
- linux
7+
- darwin
78
goarch:
89
- amd64
10+
- arm64
911
env:
1012
- CGO_ENABLED=0
13+
ldflags: -w -s -X github.com/SovereignCloudStack/csmctl/pkg/cmd.Version={{.Version}} -X github.com/SovereignCloudStack/csmctl/pkg/cmd.Commit={{.Commit}}

Makefile

+15-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ ARCH ?= amd64
2020
IMAGE_PREFIX ?= ghcr.io/sovereigncloudstack
2121
BUILDER_IMAGE = $(IMAGE_PREFIX)/csctl-builder
2222
BUILDER_IMAGE_VERSION = $(shell cat .builder-image-version.txt)
23+
Version := $(shell git describe --tags --always --dirty)
24+
Commit := $(shell git rev-parse HEAD)
25+
LDFLAGS := -X github.com/SovereignCloudStack/csmctl/pkg/cmd.Version=$(Version) -X github.com/SovereignCloudStack/csmctl/pkg/cmd.Commit=$(Commit)
2326

2427
# Certain aspects of the build are done in containers for consistency (e.g. protobuf generation)
2528
# If you have the correct tools installed and you want to speed up development you can run
@@ -39,13 +42,23 @@ TOOLS_BIN_DIR := $(TOOLS_DIR)/$(BIN_DIR)
3942
export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH)
4043
export GOBIN := $(abspath $(TOOLS_BIN_DIR))
4144

45+
##@ Clean
46+
#########
47+
# Clean #
48+
#########
49+
50+
.PHONY: clean
51+
clean: ## cleans the csmctl binary
52+
@if [ -f csmctl ]; then rm csmctl; fi
53+
54+
4255
##@ Common
4356
##########
4457
# Common #
4558
##########
4659
.PHONY: build
47-
build: ## build the csctl binary
48-
go build -o csctl main.go
60+
build: # build the csmctl binary
61+
go build -ldflags "$(LDFLAGS)" -o csmctl main.go
4962

5063
.PHONY: lint-golang
5164
lint-golang: ## Lint Golang codebase

pkg/clusterstack/mode.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package clusterstack
218

319
import (

pkg/cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ func Execute() {
4242

4343
func init() {
4444
rootCmd.AddCommand(createCmd)
45+
rootCmd.AddCommand(versionCmd)
4546
}

pkg/cmd/version.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
var (
26+
// Version of csctl.
27+
Version = "dev"
28+
// Commit against which csctl version is cut.
29+
Commit = "unknown"
30+
)
31+
32+
var versionCmd = &cobra.Command{
33+
Use: "version",
34+
Short: "prints the latest version of csmctl",
35+
Run: printVersion,
36+
SilenceUsage: true,
37+
}
38+
39+
func printVersion(_ *cobra.Command, _ []string) {
40+
fmt.Println("csmctl version:", Version)
41+
fmt.Println("commit:", Commit)
42+
}

pkg/template/template.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type CustomWalkFunc func(src, dst, path string, info os.FileInfo, meta *csctlclu
3131

3232
// MyWalk is the custom walking function to walk in the cluster stacks.
3333
func MyWalk(src, dst string, walkFn CustomWalkFunc, meta *csctlclusterstack.MetaData) error {
34-
if err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
34+
if err := filepath.Walk(src, func(path string, info os.FileInfo, _ error) error {
3535
return walkFn(src, dst, path, info, meta)
3636
}); err != nil {
3737
return fmt.Errorf("failed to walk files: %w", err)

0 commit comments

Comments
 (0)