gocli/cmd/root.go
2024-02-13 18:00:35 +03:30

58 lines
1.5 KiB
Go

package cmd
import (
"fmt"
"os"
"git.behzadan.ir/p/gocli.git/internal"
"github.com/spf13/cobra"
)
var (
// figlet -f slant "My CLI"
_banner string = `
===================================================
__ ___ ________ ____
/ |/ /_ __ / ____/ / / _/
/ /|_/ / / / / / / / / / /
/ / / / /_/ / / /___/ /____/ /
/_/ /_/\__, / \____/_____/___/
/____/
My CLI Tool
(v%s)
===================================================
`
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "mycli",
Short: "Simple cli program in Go",
Run: func(cmd *cobra.Command, args []string) {
showVersion, _ := cmd.Flags().GetBool("version")
// debugFlag, _ := cmd.Flags().GetBool("debug")
if showVersion {
fmt.Println(internal.GetVersion())
os.Exit(0)
}
fmt.Printf(_banner[1:], internal.GetVersion())
cmd.Help()
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.Flags().BoolP("version", "v", false, "Print version number")
rootCmd.Flags().BoolP("debug", "", false, "Turn on debug mode")
}