feat: nfctl(add version)

This commit is contained in:
loveuer 2024-12-26 19:51:31 -08:00
parent ad6b4fe7b6
commit 8235631d4f
6 changed files with 46 additions and 1 deletions

View File

@ -1,6 +1,8 @@
package cmd package cmd
import ( import (
"os"
"github.com/loveuer/nf/nft/log" "github.com/loveuer/nf/nft/log"
"github.com/loveuer/nf/nft/nfctl/internal/opt" "github.com/loveuer/nf/nft/nfctl/internal/opt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -14,6 +16,11 @@ var rootCmd = &cobra.Command{
log.SetLogLevel(log.LogLevelDebug) log.SetLogLevel(log.LogLevelDebug)
} }
if opt.Cfg.Version {
doVersion(cmd, args)
os.Exit(0)
}
if !opt.Cfg.DisableUpdate { if !opt.Cfg.DisableUpdate {
doUpdate(cmd.Context()) doUpdate(cmd.Context())
} }
@ -29,5 +36,6 @@ var rootCmd = &cobra.Command{
func initRoot(cmds ...*cobra.Command) { func initRoot(cmds ...*cobra.Command) {
rootCmd.PersistentFlags().BoolVar(&opt.Cfg.Debug, "debug", false, "debug mode") rootCmd.PersistentFlags().BoolVar(&opt.Cfg.Debug, "debug", false, "debug mode")
rootCmd.PersistentFlags().BoolVar(&opt.Cfg.DisableUpdate, "disable-update", false, "disable self update") 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...) rootCmd.AddCommand(cmds...)
} }

View File

@ -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)
}

View File

@ -9,6 +9,7 @@ import (
func Init() { func Init() {
initRoot( initRoot(
initVersion(),
initUpdate(), initUpdate(),
initNew(), initNew(),
) )

View File

@ -8,6 +8,7 @@ type _new struct {
type config struct { type config struct {
Debug bool Debug bool
DisableUpdate bool DisableUpdate bool
Version bool
New _new New _new
} }

View File

@ -1,7 +1,13 @@
package opt 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://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 VersionURL = "https://raw.githubusercontent.com/loveuer/nf/refs/heads/master/nft/nfctl/internal/opt/version.go"
const Banner = ` ___ __ __
___ / _/___/ /_/ /
/ _ \/ _/ __/ __/ /
/_//_/_/ \__/\__/_/
`

View File

@ -0,0 +1,10 @@
package opt
import (
"fmt"
"testing"
)
func TestBanner(t *testing.T) {
fmt.Printf("%s\nnfctl: %s\n\n", Banner, Version)
}