github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/pkg/config/config.go (about) 1 package configUtils 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "os/user" 8 "path/filepath" 9 "reflect" 10 "strings" 11 12 consts "github.com/easysoft/zendata/internal/pkg/const" 13 "github.com/easysoft/zendata/internal/pkg/model" 14 commonUtils "github.com/easysoft/zendata/pkg/utils/common" 15 fileUtils "github.com/easysoft/zendata/pkg/utils/file" 16 i118Utils "github.com/easysoft/zendata/pkg/utils/i118" 17 logUtils "github.com/easysoft/zendata/pkg/utils/log" 18 shellUtils "github.com/easysoft/zendata/pkg/utils/shell" 19 stdinUtils "github.com/easysoft/zendata/pkg/utils/stdin" 20 "github.com/easysoft/zendata/pkg/utils/vari" 21 "github.com/fatih/color" 22 "gopkg.in/ini.v1" 23 "gorm.io/gorm" 24 ) 25 26 func InitConfig(root string) { 27 log.SetFlags(0) 28 29 vari.WorkDir, _ = fileUtils.GetWorkDir(root) 30 vari.CfgFile = vari.WorkDir + ".zd.conf" 31 32 vari.Config = getInst() 33 34 i118Utils.InitI118(vari.Config.Language) 35 36 temp := filepath.Join(vari.WorkDir, "tmp", "cache") 37 if !fileUtils.FileExist(temp) { 38 logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("root_invalid", vari.WorkDir), color.FgRed) 39 os.Exit(1) 40 } 41 42 CheckConfigPermission() 43 44 if commonUtils.IsWin() { 45 shellUtils.Exec("chcp 65001") 46 } 47 48 consts.SqliteFile = strings.Replace(consts.SqliteFile, "file:", "file:"+vari.WorkDir, 1) 49 } 50 51 func SaveConfig(conf model.Config) error { 52 fileUtils.MkDirIfNeeded(filepath.Dir(vari.CfgFile)) 53 54 if conf.Version == 0 { 55 conf.Version = consts.ConfigVer 56 } 57 58 cfg := ini.Empty() 59 cfg.ReflectFrom(&conf) 60 61 cfg.SaveTo(vari.CfgFile) 62 63 vari.Config = ReadCurrConfig() 64 return nil 65 } 66 67 func PrintCurrConfig() { 68 logUtils.PrintToWithColor("\n"+i118Utils.I118Prt.Sprintf("current_config"), color.FgCyan) 69 70 val := reflect.ValueOf(vari.Config) 71 typeOfS := val.Type() 72 for i := 0; i < reflect.ValueOf(vari.Config).NumField(); i++ { 73 if !commonUtils.IsWin() && i > 4 { 74 break 75 } 76 77 val := val.Field(i) 78 name := typeOfS.Field(i).Name 79 80 fmt.Printf(" %s: %v \n", name, val.Interface()) 81 } 82 } 83 84 func ReadCurrConfig() model.Config { 85 config := model.Config{} 86 87 if !fileUtils.FileExist(vari.CfgFile) { 88 config.Language = "en" 89 i118Utils.InitI118("en") 90 91 return config 92 } 93 94 ini.MapTo(&config, vari.CfgFile) 95 96 return config 97 } 98 99 func getInst() model.Config { 100 isSetAction := len(os.Args) > 1 && (os.Args[1] == "set" || os.Args[1] == "-set") 101 if !isSetAction { 102 CheckConfigReady() 103 } 104 105 ini.MapTo(&vari.Config, vari.CfgFile) 106 107 if vari.Config.Version != consts.ConfigVer { // old config file, re-init 108 if vari.Config.Language != "en" && vari.Config.Language != "zh" { 109 vari.Config.Language = "en" 110 } 111 112 SaveConfig(vari.Config) 113 } 114 115 return vari.Config 116 } 117 118 func CheckConfigPermission() { 119 //err := syscall.Access(vari.ExeDir, syscall.O_RDWR) 120 err := fileUtils.MkDirIfNeeded(filepath.Dir(vari.CfgFile)) 121 if err != nil { 122 logUtils.PrintToWithColor( 123 fmt.Sprintf("Permission denied, please change the dir %s.", vari.WorkDir), color.FgRed) 124 os.Exit(0) 125 } 126 } 127 128 func CheckConfigReady() { 129 if !fileUtils.FileExist(vari.CfgFile) { 130 if !commonUtils.IsRelease() || vari.GlobalVars.RunMode == consts.RunModeServer { 131 conf := model.Config{Language: "zh", Version: 1} 132 SaveConfig(conf) 133 } else { 134 InputForSet() 135 } 136 } 137 } 138 139 func InputForSet() { 140 conf := ReadCurrConfig() 141 142 //logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("begin_config"), color.FgCyan) 143 144 enCheck := "" 145 var numb string 146 if conf.Language == "zh" { 147 enCheck = "*" 148 numb = "1" 149 } 150 zhCheck := "" 151 if conf.Language == "en" { 152 zhCheck = "*" 153 numb = "2" 154 } 155 156 // set lang 157 langNo := stdinUtils.GetInput("(1|2)", numb, "enter_language", enCheck, zhCheck) 158 if langNo == "1" { 159 conf.Language = "zh" 160 } else { 161 conf.Language = "en" 162 } 163 164 // set PATH environment vari 165 var addToPath bool 166 if commonUtils.IsWin() { 167 addToPath = true 168 // stdinUtils.InputForBool(&addToPath, true, "add_to_path_win") 169 } else { 170 stdinUtils.InputForBool(&addToPath, true, "add_to_path_linux") 171 } 172 173 if addToPath { 174 AddZdToPath() 175 } 176 177 SaveConfig(conf) 178 PrintCurrConfig() 179 } 180 181 func AddZdToPath() { 182 userProfile, _ := user.Current() 183 home := userProfile.HomeDir 184 185 if commonUtils.IsWin() { 186 addZdToPathEnvVarWin(home) 187 } else { 188 addZdToPathEnvVarForLinux(home) 189 } 190 } 191 192 func addZdToPathEnvVarWin(home string) { 193 pathVar := os.Getenv("PATH") 194 if strings.Contains(pathVar, vari.WorkDir) { 195 return 196 } 197 198 cmd := `setx Path "%%Path%%;` + vari.WorkDir + `"` 199 logUtils.PrintToWithColor("\n"+i118Utils.I118Prt.Sprintf("add_to_path_tips_win", cmd), color.FgRed) 200 201 // TODO: fix the space issue 202 //out, err := shellUtils.Exec(cmd) 203 // 204 //if err == nil { 205 // msg := i118Utils.I118Prt.Sprintf("add_to_path_success_win") 206 // logUtils.PrintToWithColor(msg, color.FgRed) 207 //} else { 208 // logUtils.PrintToWithColor( 209 // i118Utils.I118Prt.Sprintf("fail_to_exec_cmd", cmd, err.Error() + ": " + out), color.FgRed) 210 //} 211 } 212 213 func addZdToPathEnvVarForLinux(home string) { 214 path := fmt.Sprintf("%s%s%s", home, consts.PthSep, ".bash_profile") 215 216 content := fileUtils.ReadFile(path) 217 if strings.Contains(content, vari.WorkDir) { 218 return 219 } 220 221 cmd := fmt.Sprintf("echo 'export PATH=$PATH:%s' >> %s", vari.WorkDir, path) 222 out, err := shellUtils.Exec(cmd) 223 224 if err == nil { 225 msg := i118Utils.I118Prt.Sprintf("add_to_path_success_linux", path) 226 logUtils.PrintToWithColor(msg, color.FgRed) 227 } else { 228 logUtils.PrintToWithColor( 229 i118Utils.I118Prt.Sprintf("fail_to_exec_cmd", cmd, err.Error()+": "+out), color.FgRed) 230 } 231 } 232 233 func isDataInit(gormDb *gorm.DB) bool { 234 def := model.ZdDef{} 235 err := gormDb.Find(def).Error 236 237 if err == nil { 238 return true 239 } else { 240 return false 241 } 242 }