diff --git a/nft/nfctl/internal/cmd/cmd.root.go b/nft/nfctl/internal/cmd/cmd.root.go index fc61e6c..7f94a54 100644 --- a/nft/nfctl/internal/cmd/cmd.root.go +++ b/nft/nfctl/internal/cmd/cmd.root.go @@ -1,6 +1,8 @@ package cmd import ( + "os" + "github.com/loveuer/nf/nft/log" "github.com/loveuer/nf/nft/nfctl/internal/opt" "github.com/spf13/cobra" @@ -14,6 +16,11 @@ var rootCmd = &cobra.Command{ log.SetLogLevel(log.LogLevelDebug) } + if opt.Cfg.Version { + doVersion(cmd, args) + os.Exit(0) + } + if !opt.Cfg.DisableUpdate { doUpdate(cmd.Context()) } @@ -29,5 +36,6 @@ var rootCmd = &cobra.Command{ func initRoot(cmds ...*cobra.Command) { rootCmd.PersistentFlags().BoolVar(&opt.Cfg.Debug, "debug", false, "debug mode") rootCmd.PersistentFlags().BoolVar(&opt.Cfg.DisableUpdate, "disable-update", false, "disable self update") + rootCmd.PersistentFlags().BoolVarP(&opt.Cfg.Version, "version", "v", false, "print nfctl version") rootCmd.AddCommand(cmds...) } diff --git a/nft/nfctl/internal/cmd/cmd.version.go b/nft/nfctl/internal/cmd/cmd.version.go new file mode 100644 index 0000000..bb4c728 --- /dev/null +++ b/nft/nfctl/internal/cmd/cmd.version.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "fmt" + + "github.com/loveuer/nf/nft/nfctl/internal/opt" + "github.com/spf13/cobra" +) + +func initVersion() *cobra.Command { + return &cobra.Command{ + Use: "version", + Run: doVersion, + } +} + +func doVersion(cmd *cobra.Command, args []string) { + fmt.Printf("%s\nnfctl: %s\n\n", opt.Banner, opt.Version) +} diff --git a/nft/nfctl/internal/cmd/init.go b/nft/nfctl/internal/cmd/init.go index bd6ca9f..f4107d5 100644 --- a/nft/nfctl/internal/cmd/init.go +++ b/nft/nfctl/internal/cmd/init.go @@ -9,6 +9,7 @@ import ( func Init() { initRoot( + initVersion(), initUpdate(), initNew(), ) diff --git a/nft/nfctl/internal/opt/opt.go b/nft/nfctl/internal/opt/opt.go index d5e2d4c..0833e60 100644 --- a/nft/nfctl/internal/opt/opt.go +++ b/nft/nfctl/internal/opt/opt.go @@ -8,6 +8,7 @@ type _new struct { type config struct { Debug bool DisableUpdate bool + Version bool New _new } diff --git a/nft/nfctl/internal/opt/version.go b/nft/nfctl/internal/opt/version.go index 518247e..137f315 100644 --- a/nft/nfctl/internal/opt/version.go +++ b/nft/nfctl/internal/opt/version.go @@ -1,7 +1,13 @@ package opt -const Version = "v24.12.27-r01" +const Version = "v24.12.27-r02" // const VersionURL = "https://github.com/loveuer/nf/nft/nfctl/internal/opt/version.go" const VersionURL = "https://raw.githubusercontent.com/loveuer/nf/refs/heads/master/nft/nfctl/internal/opt/version.go" + +const Banner = ` ___ __ __ + ___ / _/___/ /_/ / + / _ \/ _/ __/ __/ / +/_//_/_/ \__/\__/_/ + ` diff --git a/nft/nfctl/internal/opt/version_test.go b/nft/nfctl/internal/opt/version_test.go new file mode 100644 index 0000000..374ac14 --- /dev/null +++ b/nft/nfctl/internal/opt/version_test.go @@ -0,0 +1,10 @@ +package opt + +import ( + "fmt" + "testing" +) + +func TestBanner(t *testing.T) { + fmt.Printf("%s\nnfctl: %s\n\n", Banner, Version) +}