@@ -3,6 +3,7 @@ package main
33import (
44 "fmt"
55 "os"
6+ "path/filepath"
67
78 "github.com/spf13/cobra"
89 "github.com/spf13/viper"
@@ -37,7 +38,7 @@ func init() {
3738 rootCmd .SetVersionTemplate ("CodeI18n CLI 版本: {{.Version}}\n " )
3839
3940 // Global flags
40- rootCmd .PersistentFlags ().StringVar (& cfgFile , "config" , "" , "配置文件路径 (默认是 $HOME /.codei18n.json 或项目根目录 .codei18n/config.json)" )
41+ rootCmd .PersistentFlags ().StringVar (& cfgFile , "config" , "" , "配置文件路径 (默认读取 ~ /.codei18n/config .json 以及当前目录的 .codei18n/config.json)" )
4142 rootCmd .PersistentFlags ().BoolVar (& verbose , "verbose" , false , "显示详细日志" )
4243
4344 // Bind flags to viper
@@ -46,31 +47,69 @@ func init() {
4647
4748// initConfig reads in config file and ENV variables if set.
4849func initConfig () {
50+ viper .AutomaticEnv () // read in environment variables that match
51+
4952 if cfgFile != "" {
50- // Use config file from the flag.
5153 viper .SetConfigFile (cfgFile )
52- } else {
53- // Find home directory.
54- home , err := os .UserHomeDir ()
55- if err != nil {
54+ if err := viper .ReadInConfig (); err != nil {
5655 cobra .CheckErr (err )
5756 }
57+ if verbose {
58+ fmt .Fprintf (os .Stderr , "Using config file: %s\n " , viper .ConfigFileUsed ())
59+ }
60+ return
61+ }
5862
59- // Search config in home directory with name ".codei18n" (without extension).
60- viper .AddConfigPath (home )
61- viper .AddConfigPath (".codei18n" )
62- viper .SetConfigType ("json" )
63- viper .SetConfigName ("config" )
63+ home , err := os .UserHomeDir ()
64+ if err != nil {
65+ cobra .CheckErr (err )
6466 }
6567
66- viper .AutomaticEnv () // read in environment variables that match
68+ configLoaded := false
69+
70+ if loaded , err := loadConfigFile (filepath .Join (home , ".codei18n" , "config.json" ), false ); err != nil {
71+ cobra .CheckErr (err )
72+ } else if loaded {
73+ configLoaded = true
74+ }
75+
76+ if loaded , err := loadConfigFile (filepath .Join (".codei18n" , "config.json" ), configLoaded ); err != nil {
77+ cobra .CheckErr (err )
78+ } else if loaded {
79+ configLoaded = true
80+ }
81+
82+ if ! configLoaded && verbose {
83+ fmt .Fprintln (os .Stderr , "未找到配置文件,使用内置默认配置" )
84+ }
85+ }
6786
68- // If a config file is found, read it in.
69- if err := viper .ReadInConfig (); err == nil && verbose {
70- fmt .Fprintf (os .Stderr , "Using config file: %s\n " , viper .ConfigFileUsed ())
87+ func loadConfigFile (path string , merge bool ) (bool , error ) {
88+ if _ , err := os .Stat (path ); err != nil {
89+ if os .IsNotExist (err ) {
90+ return false , nil
91+ }
92+ return false , err
7193 }
94+
95+ viper .SetConfigFile (path )
96+
97+ var err error
98+ if merge {
99+ err = viper .MergeInConfig ()
100+ } else {
101+ err = viper .ReadInConfig ()
102+ }
103+ if err != nil {
104+ return false , err
105+ }
106+
107+ if verbose {
108+ fmt .Fprintf (os .Stderr , "Using config file: %s\n " , path )
109+ }
110+ return true , nil
72111}
73112
74113func main () {
75114 Execute ()
76- }
115+ }
0 commit comments