Skip to content

Commit

Permalink
optimize: print info. and exit status
Browse files Browse the repository at this point in the history
  • Loading branch information
chaunsin committed Nov 26, 2024
1 parent 88f8809 commit 7d86818
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/ncmctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ var (

func main() {
c := ncmctl.New()
c.Version(Version)
c.Version(Version, BuildTime, Commit)
c.Execute()
}
9 changes: 7 additions & 2 deletions internal/ncmctl/ncmctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ package ncmctl

import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/chaunsin/netease-cloud-music/config"
"github.com/chaunsin/netease-cloud-music/pkg/log"
Expand Down Expand Up @@ -60,6 +62,7 @@ func New() *Root {
ncmctl partner`,
},
}
c.cmd.SetVersionTemplate(`{{printf "%s\n" .Version}}`)
c.cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
var (
cfgPath = c.Opts.Config
Expand Down Expand Up @@ -126,8 +129,9 @@ func (c *Root) addFlags() {
c.cmd.PersistentFlags().StringVar(&c.Opts.Home, "home", config.HomeDir, "configuration home path. the home path is used to store running information")
}

func (c *Root) Version(version string) {
c.cmd.Version = version
func (c *Root) Version(version, buildTime, commitHash string) {
c.cmd.Version = fmt.Sprintf("Version: %s\nBuildTime: %s\nCommit: %s\nGoVersion: %s",
version, buildTime, commitHash, runtime.Version())
}

func (c *Root) Add(command ...*cobra.Command) {
Expand All @@ -137,5 +141,6 @@ func (c *Root) Add(command ...*cobra.Command) {
func (c *Root) Execute() {
if err := c.cmd.Execute(); err != nil {
c.cmd.PrintErrln(err)
os.Exit(1)
}
}

0 comments on commit 7d86818

Please sign in to comment.