Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 06ac522

Browse files
committedFeb 25, 2025··
feat: add version command for modctl
Signed-off-by: Gaius <[email protected]>
1 parent 8c2702b commit 06ac522

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed
 

‎.goreleaser.yml

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ builds:
1616
goarch:
1717
- amd64
1818
- arm64
19+
env:
20+
- CGO_ENABLED=0
21+
ldflags:
22+
- -X github.com/CloudNativeAI/modctl/pkg/version.GitVersion={{ .Tag }}
23+
- -X github.com/CloudNativeAI/modctl/pkg/version.GitCommit={{ .ShortCommit }}
24+
- -X github.com/CloudNativeAI/modctl/pkg/version.BuildTime={{ .Date }}
1925

2026
archives:
2127
- name_template: "modctl-{{ .Version }}-{{ .Os }}-{{ .Arch }}"

‎cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func init() {
6767
}
6868

6969
// Add sub command.
70+
rootCmd.AddCommand(versionCmd)
7071
rootCmd.AddCommand(buildCmd)
7172
rootCmd.AddCommand(listCmd)
7273
rootCmd.AddCommand(loginCmd)

‎cmd/version.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2025 The CNAI 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/CloudNativeAI/modctl/pkg/version"
23+
24+
"github.com/spf13/cobra"
25+
"github.com/spf13/viper"
26+
)
27+
28+
// versionCmd represents the modctl command for version.
29+
var versionCmd = &cobra.Command{
30+
Use: "version",
31+
Short: "A command line tool for modctl version",
32+
DisableAutoGenTag: true,
33+
SilenceUsage: true,
34+
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
35+
RunE: func(cmd *cobra.Command, args []string) error {
36+
return runVersion()
37+
},
38+
}
39+
40+
// init initializes version command.
41+
func init() {
42+
flags := rmCmd.Flags()
43+
44+
if err := viper.BindPFlags(flags); err != nil {
45+
panic(fmt.Errorf("bind version flags to viper: %w", err))
46+
}
47+
}
48+
49+
// runVersion runs the version modctl.
50+
func runVersion() error {
51+
fmt.Printf("%-12s%s\n", "Version:", version.GitVersion)
52+
fmt.Printf("%-12s%s\n", "Commit:", version.GitCommit)
53+
fmt.Printf("%-12s%s\n", "Platform:", version.Platform)
54+
fmt.Printf("%-12s%s\n", "BuildTime:", version.BuildTime)
55+
return nil
56+
}

‎pkg/version/platform_darwin.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2025 The CNAI 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 version
18+
19+
const platform = "darwin"

‎pkg/version/platform_linux.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2025 The CNAI 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 version
18+
19+
const platform = "linux"

‎pkg/version/version.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2025 The CNAI 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 version
18+
19+
var (
20+
GitVersion = "v0.0.1"
21+
GitCommit = "unknown"
22+
Platform = platform
23+
BuildTime = "unknown"
24+
)

0 commit comments

Comments
 (0)
Please sign in to comment.