Skip to content

Commit

Permalink
add version flag and variable (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 1, 2023
1 parent a8e54ba commit f8c863a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 17 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,31 @@ import (
)

var (
client *store.Client
client *store.Client
goitVersion = ""
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "goit",
Short: "Git made by Golang",
Long: "This is a Git-like CLI tool made by Golang",
RunE: func(cmd *cobra.Command, args []string) error {
versionFlag, err := cmd.Flags().GetBool("version")
if err != nil {
return fmt.Errorf("fail to get version flag: %w", err)
}

if versionFlag {
fmt.Println(goitVersion)
}

return nil
},
}

func Execute() {
func Execute(version string) {
goitVersion = version
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand Down Expand Up @@ -51,4 +65,5 @@ func init() {
client = store.NewClient(config, index, head, rootGoitPath)

rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.Flags().BoolP("version", "v", false, "Show Goit version")
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package main

import "github.com/JunNishimura/Goit/cmd"

var version = ""

func main() {
cmd.Execute()
cmd.Execute(version)
}

0 comments on commit f8c863a

Please sign in to comment.