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

fix remote auth username env and env add SetEnvKeyReplacer #176

Merged
merged 2 commits 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
8 changes: 5 additions & 3 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ var newCmd = &cobra.Command{
Depth: 0,
ReferenceName: plumbing.ReferenceName("refs/heads/" + config.C.New.Branch),
Auth: &http.BasicAuth{
Username: os.Getenv("JZERO_REMOTE_USERNAME"),
Password: os.Getenv("JZERO_REMOTE_PASSWORD"),
Username: config.C.New.RemoteAuthUsername,
Password: config.C.New.RemoteAuthPassword,
},
})
cobra.CheckErr(err)
Expand Down Expand Up @@ -102,8 +102,10 @@ func init() {
newCmd.Flags().StringP("module", "m", "", "set go module")
newCmd.Flags().StringP("output", "o", "", "set output dir")
newCmd.Flags().StringP("home", "", "", "use the specified template.")
newCmd.Flags().StringP("frame", "", "api", "frame")
newCmd.Flags().StringP("frame", "", "api", "set frame")
newCmd.Flags().StringP("remote", "r", "https://github.com/jzero-io/templates", "remote templates repo")
newCmd.Flags().StringP("remote-auth-username", "", "", "remote templates repo auth username")
newCmd.Flags().StringP("remote-auth-password", "", "", "remote templates repo auth password")
newCmd.Flags().StringP("branch", "b", "", "use remote template repo branch")
newCmd.Flags().StringP("local", "", "", "use local template")
newCmd.Flags().StringSliceP("features", "", []string{}, "select features")
Expand Down
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
27 changes: 14 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"strings"

"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -12,9 +13,6 @@ import (
var C Config

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

/*
===============================command flags start========================================
*/
Expand Down Expand Up @@ -47,16 +45,18 @@ type Config struct {
}

type NewConfig struct {
Core bool `mapstructure:"core"`
Home string `mapstructure:"home"` // 新建项目使用的模板文件目录
Module string `mapstructure:"module"` // 新建的项目的 go module
Mono bool `mapstructure:"mono"` // 是否是 mono 项目(即在一个mod项目之下, 但该项目本身无 go.mod 文件)
Output string `mapstructure:"output"` // 输出到的目录
Remote string `mapstructure:"remote"` // 远程仓库地址
Frame string `mapstructure:"frame"` // 使用 jzero 内置的框架
Branch string `mapstructure:"branch"` // 使用远程模板仓库的某个分支
Local string `mapstructure:"local"` // 使用本地模板与 branch 对应
Features []string `mapstructure:"features"` // 新建项目使用哪些特性, 灵活构建模板
Core bool `mapstructure:"core"`
Home string `mapstructure:"home"` // 新建项目使用的模板文件目录
Module string `mapstructure:"module"` // 新建的项目的 go module
Mono bool `mapstructure:"mono"` // 是否是 mono 项目(即在一个mod项目之下, 但该项目本身无 go.mod 文件)
Output string `mapstructure:"output"` // 输出到的目录
Remote string `mapstructure:"remote"` // 远程仓库地址
RemoteAuthUsername string `mapstructure:"remote-auth-username"` // 远程仓库的认证用户名
RemoteAuthPassword string `mapstructure:"remote-auth-password"` // 远程仓库的认证密码
Frame string `mapstructure:"frame"` // 使用 jzero 内置的框架
Branch string `mapstructure:"branch"` // 使用远程模板仓库的某个分支
Local string `mapstructure:"local"` // 使用本地模板与 branch 对应
Features []string `mapstructure:"features"` // 新建项目使用哪些特性, 灵活构建模板
}

type GenConfig struct {
Expand Down Expand Up @@ -246,6 +246,7 @@ func SetConfig(command string, flagSet *pflag.FlagSet) error {
})

viper.SetEnvPrefix("JZERO")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.AutomaticEnv()

if err := viper.Unmarshal(&C); err != nil {
Expand Down
Loading