Skip to content

Commit f632f5f

Browse files
author
Sébastien HOUZÉ
committed
feat: add version informations
1 parent 0fd081c commit f632f5f

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

cmd/root.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212
"golang.org/x/oauth2"
1313
)
1414

15+
var (
16+
version = "devel"
17+
buildDate string
18+
commitHash string
19+
)
20+
1521
// Github owner (user or organization)
1622
var owner string
1723

@@ -33,7 +39,7 @@ var rootCmd = &cobra.Command{
3339
Long: ``,
3440
PersistentPreRun: func(cmd *cobra.Command, args []string) {
3541
token := os.Getenv("GITHUB_TOKEN")
36-
if cmd.Name() != "help" && cmd.Name() != "deployment" && cmd.Name() != "deployment_status" && token == "" {
42+
if cmd.Name() != "version" && cmd.Name() != "help" && cmd.Name() != "deployment" && cmd.Name() != "deployment_status" && token == "" {
3743
log.Fatal("Please define GITHUB_TOKEN. See documentation to obtain one if needed: https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line")
3844
}
3945
ctx = context.Background()
@@ -45,7 +51,7 @@ var rootCmd = &cobra.Command{
4551
githubRepository = os.Getenv("GITHUB_REPOSITORY")
4652
}
4753
owner, repository = splitGithubRepository(githubRepository)
48-
if cmd.Name() != "help" && cmd.Name() != "deployment" && cmd.Name() != "deployment_status" && owner == "" && repository == "" {
54+
if cmd.Name() != "version" && cmd.Name() != "help" && cmd.Name() != "deployment" && cmd.Name() != "deployment_status" && owner == "" && repository == "" {
4955
log.Fatal("Github repository is required.")
5056
}
5157
},
@@ -72,8 +78,17 @@ func splitGithubRepository(repository string) (string, string) {
7278
return values[0], values[1]
7379
}
7480

81+
var versionCmd = &cobra.Command{
82+
Use: "version",
83+
Short: "Print the version number of github",
84+
Run: func(cmd *cobra.Command, args []string) {
85+
fmt.Printf("github version %s\nbuild date %s\ncommit %s\n", version, buildDate, commitHash)
86+
},
87+
}
88+
7589
func init() {
90+
rootCmd.AddCommand(versionCmd)
91+
rootCmd.Version = version
7692
rootCmd.PersistentFlags().StringVarP(&githubRepository, "repository", "r", "", "the owner and repository name. For example, octocat/Hello-World. Environment variable GITHUB_REPOSITORY will be used as a fallback.")
7793

78-
rootCmd.Version = "1.0.0"
7994
}

0 commit comments

Comments
 (0)