Skip to content

Commit e0e1e1e

Browse files
committed
feat: version subcommand
1 parent c4330cd commit e0e1e1e

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,8 @@ xca create-ca --help
176176
xca sign --help
177177
178178
```
179+
180+
181+
1. readme
182+
2. 根据不同的系统,设置不同的目录
183+
3. version bug

cmd/root.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ import (
1818
"os"
1919

2020
"github.com/spf13/cobra"
21-
xca "go.xiexianbin.cn/x-ca"
22-
)
23-
24-
var (
25-
showVersion bool
2621
)
2722

2823
// rootCmd represents the base command
@@ -41,22 +36,18 @@ Examples:
4136
xca create-ca --key-type ec --curve P256
4237
xca sign example.com --domains "example.com,www.example.com"
4338
xca sign 192.168.1.1 --ips "192.168.1.1"`,
44-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
45-
if showVersion {
46-
v := xca.GetVersion()
47-
xca.PrintVersion("xca", v, false)
48-
os.Exit(0)
49-
}
39+
Run: func(cmd *cobra.Command, args []string) {
40+
// If no subcommand, show help
41+
cmd.Help()
5042
},
5143
}
5244

5345
func init() {
54-
rootCmd.PersistentFlags().BoolVar(&showVersion, "version", false, "show version information")
55-
5646
// Add subcommands
5747
rootCmd.AddCommand(createCaCmd)
5848
rootCmd.AddCommand(infoCmd)
5949
rootCmd.AddCommand(signCmd)
50+
rootCmd.AddCommand(versionCmd)
6051
}
6152

6253
func initCommands() {

cmd/version.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright © 2025 xiexianbin.cn
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package main
15+
16+
import (
17+
"github.com/spf13/cobra"
18+
xca "go.xiexianbin.cn/x-ca"
19+
)
20+
21+
// versionCmd represents the version command
22+
var versionCmd = &cobra.Command{
23+
Use: "version",
24+
Short: "Show version information",
25+
Long: `Show version information for XCA including build date, git commit, and platform details.`,
26+
Run: func(cmd *cobra.Command, args []string) {
27+
v := xca.GetVersion()
28+
xca.PrintVersion("xca", v, false)
29+
},
30+
}

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func GetVersion() Version {
5757
}
5858

5959
func PrintVersion(cliName string, version Version, short bool) {
60-
fmt.Printf("%s: %s\n", cliName, version.Version)
60+
fmt.Printf("%s(https://github.com/x-ca/go-ca): %s\n", cliName, version.Version)
6161
if short {
6262
return
6363
}

0 commit comments

Comments
 (0)