Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jzero): support jzero -v and jzero --version #174

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading