Skip to content

Commit

Permalink
fix remote auth username env and env add SetEnvKeyReplacer (#176)
Browse files Browse the repository at this point in the history
* fix(jzero): fix remote auth username env and env add SetEnvKeyReplacer
  • Loading branch information
jaronnie authored Jan 6, 2025
1 parent bc8fd08 commit eee04c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
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
24 changes: 14 additions & 10 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 Down Expand Up @@ -44,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 @@ -243,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

0 comments on commit eee04c1

Please sign in to comment.