Skip to content

Commit

Permalink
feat(jzero): support jzero -v and jzero --version
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Jan 6, 2025
1 parent 5e3f396 commit 1eae350
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
16 changes: 7 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"fmt"
"log"
"os"
"strconv"
"time"

"github.com/a8m/envsubst"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"gopkg.in/yaml.v3"

Expand All @@ -33,6 +33,11 @@ var rootCmd = &cobra.Command{
Use: "jzero",
Short: `Used to create project by templates and generate server/client code by proto and api file.
`,
Run: func(cmd *cobra.Command, args []string) {
if parseBool, err := strconv.ParseBool(cmd.Flags().Lookup("version").Value.String()); err == nil && parseBool {
getVersion()
}
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -47,6 +52,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

rootCmd.Flags().BoolP("version", "v", false, "show version")
rootCmd.PersistentFlags().StringVarP(&CfgFile, "config", "f", ".jzero.yaml", "set config file")
rootCmd.PersistentFlags().StringVarP(&CfgEnvFile, "config-env", "", ".jzero.env.yaml", "set config env file")
rootCmd.PersistentFlags().BoolP("debug", "", false, "debug mode")
Expand Down Expand Up @@ -89,14 +95,6 @@ func initConfig() {
panic(err)
}

// patch jzero version
if config.C.Version != "" {
fmt.Printf("use jzero version: %s\n", config.C.Version)
if err := golang.Install(fmt.Sprintf("github.com/jzero-io/jzero@%s", config.C.Version)); err != nil {
cobra.CheckErr(err)
}
}

if config.C.Debug {
logx.MustSetup(logx.LogConf{Encoding: "plain"})
logx.SetLevel(logx.DebugLevel)
Expand Down
7 changes: 4 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ var (
var versionCmd = &cobra.Command{
Use: "version",
Short: `Print jzero version`,
RunE: getVersion,
Run: func(cmd *cobra.Command, args []string) {
getVersion()
},
}

func getVersion(_ *cobra.Command, _ []string) error {
func getVersion() {
var versionBuffer bytes.Buffer

if Version != "" {
Expand All @@ -51,7 +53,6 @@ func getVersion(_ *cobra.Command, _ []string) error {
}

fmt.Print(versionBuffer.String())
return nil
}

func init() {
Expand Down
3 changes: 0 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
var C Config

type Config struct {
Syntax string `mapstructure:"syntax"`
Version string `mapstructure:"version"`

/*
===============================command flags start========================================
*/
Expand Down

0 comments on commit 1eae350

Please sign in to comment.