feat(cmd/joj3): add -fallback-conf-name flag
This commit is contained in:
		
							parent
							
								
									896c4ac220
								
							
						
					
					
						commit
						34f1cbc711
					
				|  | @ -282,14 +282,16 @@ func hintValidScopes(confRoot, confName string) { | ||||||
| 		"valid scopes", validScopes) | 		"valid scopes", validScopes) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func GetConfPath(confRoot, confName, msg, tag string) ( | func GetConfPath(confRoot, confName, fallbackConfName, msg, tag string) ( | ||||||
| 	confPath, group string, confStat fs.FileInfo, err error, | 	confPath, group string, confStat fs.FileInfo, err error, | ||||||
| ) { | ) { | ||||||
| 	confPath, group, err = parseMsg(confRoot, confName, msg, tag) | 	confPath, group, err = parseMsg(confRoot, confName, msg, tag) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		slog.Error("parse msg", "error", err) | 		slog.Error("parse msg", "error", err) | ||||||
| 		// fallback to conf file in conf root on parse error
 | 		// fallback to conf file in conf root on parse error
 | ||||||
| 		confPath = filepath.Clean(fmt.Sprintf("%s/%s", confRoot, confName)) | 		confPath = filepath.Clean( | ||||||
|  | 			fmt.Sprintf("%s/%s", confRoot, fallbackConfName)) | ||||||
|  | 		slog.Info("fallback to conf", "path", confPath) | ||||||
| 	} | 	} | ||||||
| 	confStat, err = os.Stat(confPath) | 	confStat, err = os.Stat(confPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | @ -298,7 +300,9 @@ func GetConfPath(confRoot, confName, msg, tag string) ( | ||||||
| 		} | 		} | ||||||
| 		slog.Error("stat conf", "error", err) | 		slog.Error("stat conf", "error", err) | ||||||
| 		// fallback to conf file in conf root on conf not exist
 | 		// fallback to conf file in conf root on conf not exist
 | ||||||
| 		confPath = filepath.Clean(fmt.Sprintf("%s/%s", confRoot, confName)) | 		confPath = filepath.Clean( | ||||||
|  | 			fmt.Sprintf("%s/%s", confRoot, fallbackConfName)) | ||||||
|  | 		slog.Info("fallback to conf", "path", confPath) | ||||||
| 		confStat, err = os.Stat(confPath) | 		confStat, err = os.Stat(confPath) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			slog.Error("stat fallback conf", "error", err) | 			slog.Error("stat fallback conf", "error", err) | ||||||
|  |  | ||||||
|  | @ -12,17 +12,19 @@ import ( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var ( | var ( | ||||||
| 	confRoot    string | 	confRoot         string | ||||||
| 	confName    string | 	confName         string | ||||||
| 	tag         string | 	fallbackConfName string | ||||||
| 	msg         string | 	tag              string | ||||||
| 	showVersion *bool | 	msg              string | ||||||
| 	Version     string = "debug" | 	showVersion      *bool | ||||||
|  | 	Version          string = "debug" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func init() { | func init() { | ||||||
| 	flag.StringVar(&confRoot, "conf-root", ".", "root path for all config files") | 	flag.StringVar(&confRoot, "conf-root", ".", "root path for all config files") | ||||||
| 	flag.StringVar(&confName, "conf-name", "conf.json", "filename for config files") | 	flag.StringVar(&confName, "conf-name", "conf.json", "filename for config files") | ||||||
|  | 	flag.StringVar(&fallbackConfName, "fallback-conf-name", "", "filename for the fallback config file in conf-root, leave empty to use conf-name") | ||||||
| 	flag.StringVar(&tag, "tag", "", "tag to trigger the running, when non-empty, should equal to the scope in msg") | 	flag.StringVar(&tag, "tag", "", "tag to trigger the running, when non-empty, should equal to the scope in msg") | ||||||
| 	// TODO: remove this flag
 | 	// TODO: remove this flag
 | ||||||
| 	flag.StringVar(&msg, "msg", "", "[DEPRECATED] will be ignored") | 	flag.StringVar(&msg, "msg", "", "[DEPRECATED] will be ignored") | ||||||
|  | @ -39,6 +41,9 @@ func mainImpl() error { | ||||||
| 		fmt.Println(Version) | 		fmt.Println(Version) | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
|  | 	if fallbackConfName == "" { | ||||||
|  | 		fallbackConfName = confName | ||||||
|  | 	} | ||||||
| 	slog.Info("start joj3", "version", Version) | 	slog.Info("start joj3", "version", Version) | ||||||
| 	msg, err := conf.GetCommitMsg() | 	msg, err := conf.GetCommitMsg() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | @ -46,7 +51,7 @@ func mainImpl() error { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	confPath, group, confStat, err := conf.GetConfPath( | 	confPath, group, confStat, err := conf.GetConfPath( | ||||||
| 		confRoot, confName, msg, tag) | 		confRoot, confName, fallbackConfName, msg, tag) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		slog.Error("get conf path", "error", err) | 		slog.Error("get conf path", "error", err) | ||||||
| 		return err | 		return err | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user