fix(version): command line flag

This commit is contained in:
Suyono 2024-03-30 00:37:51 +00:00
parent 3bdca8c540
commit 6092629cb4
4 changed files with 18 additions and 7 deletions

View File

@ -2,9 +2,10 @@ package cli
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)
type Version string
@ -27,12 +28,12 @@ func (v Version) Cmd(cmd *cobra.Command) {
}
func (v Version) Flag(cmd *cobra.Command) {
cmd.PersistentFlags().Bool(versionFlag, false, "print version")
cmd.PersistentFlags().Count(versionFlag, "print version")
_ = viper.BindPFlag(versionFlag, cmd.PersistentFlags().Lookup(versionFlag))
}
func (v Version) FlagHook() {
if viper.GetBool(versionFlag) {
if viper.GetInt(versionFlag) > 0 {
v.Print()
}
}

View File

@ -7,6 +7,7 @@ import (
"gitea.suyono.dev/suyono/wingmate"
"gitea.suyono.dev/suyono/wingmate/config"
wminit "gitea.suyono.dev/suyono/wingmate/init"
"github.com/spf13/viper"
)
var (
@ -22,6 +23,10 @@ func main() {
_ = wingmate.NewLog(os.Stderr)
config.SetVersion(version)
config.ParseFlags()
wingmate.Log().Info().Msgf("starting wingmate version %s", viper.GetString(config.WingmateVersion))
if cfg, err = config.Read(); err != nil {
wingmate.Log().Error().Msgf("failed to read config %#v", err)
}

View File

@ -79,7 +79,6 @@ type CronSchedule struct {
func SetVersion(version string) {
version = strings.TrimRight(version, versionTrimRightCutSet)
viper.Set(WingmateVersion, version)
wingmate.Log().Info().Msgf("starting wingmate version %s", version)
}
func Read() (*Config, error) {
@ -91,8 +90,6 @@ func Read() (*Config, error) {
viper.SetDefault(PidProxyPathConfig, PidProxyPathDefault)
viper.SetDefault(ExecPathConfig, ExecPathDefault)
ParseFlags()
var (
dirent []os.DirEntry
err error

View File

@ -1,12 +1,15 @@
package config
import (
"fmt"
"os"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
func ParseFlags() {
pflag.BoolP(VersionFlag, "v", false, "check version")
pflag.CountP(VersionFlag, "v", "check version")
pflag.String(WMPidProxyPathFlag, "", "wmpidproxy path")
pflag.String(WMExecPathFlag, "", "wmexec path")
pflag.StringP(ConfigPathFlag, "c", "", "config path")
@ -17,4 +20,9 @@ func ParseFlags() {
_ = viper.BindPFlag(ConfigPath, pflag.CommandLine.Lookup(ConfigPathFlag))
_ = viper.BindPFlag(PidProxyPathConfig, pflag.CommandLine.Lookup(WMPidProxyPathFlag))
_ = viper.BindPFlag(ExecPathConfig, pflag.CommandLine.Lookup(WMExecPathFlag))
if viper.GetInt(VersionCheckKey) > 0 {
fmt.Println(viper.GetString(WingmateVersion))
os.Exit(0)
}
}