-
Notifications
You must be signed in to change notification settings - Fork 6
/
version.go
35 lines (29 loc) 路 916 Bytes
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cmd
import (
"fmt"
"os"
"strings"
"github.com/criteo/command-launcher/internal/context"
"github.com/spf13/cobra"
)
func printVersion() {
ctx, _ := context.AppContext()
fmt.Printf("%s version %s\n", ctx.AppName(), getVersion(ctx.AppVersion(), ctx.AppBuildNum()))
}
func getVersion(version string, buildNum string) string {
if version == "" {
return fmt.Sprintf("dev, build %s", os.Getenv("USER"))
}
return fmt.Sprintf("%s, build %s", version, buildNum)
}
func AddVersionCmd(rootCmd *cobra.Command, appCtx context.LauncherContext) {
versionCmd := &cobra.Command{
Use: "version",
Short: fmt.Sprintf("Print the version number of %s command", strings.ToTitle(appCtx.AppName())),
Long: fmt.Sprintf(`All software has versions. This is %s's`, strings.ToTitle(appCtx.AppName())),
Run: func(cmd *cobra.Command, args []string) {
printVersion()
},
}
rootCmd.AddCommand(versionCmd)
}