style(cmd/joj3): split main logic
This commit is contained in:
		
							parent
							
								
									d2325d675f
								
							
						
					
					
						commit
						0cdf665910
					
				|  | @ -72,21 +72,25 @@ func ParseConfFile(path string) (conf *Conf, err error) { | ||||||
| 	return | 	return | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func GetSHA256(filePath string) (string, error) { | func GetSHA256(filePath string) (hashStr string, err error) { | ||||||
| 	// Open the file
 | 	// Open the file
 | ||||||
| 	file, err := os.Open(filePath) | 	file, err := os.Open(filePath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return | ||||||
| 	} | 	} | ||||||
| 	defer file.Close() | 	defer func() { | ||||||
|  | 		if cerr := file.Close(); cerr != nil && err == nil { | ||||||
|  | 			err = cerr | ||||||
|  | 		} | ||||||
|  | 	}() | ||||||
| 
 | 
 | ||||||
| 	// Calculate SHA-256
 | 	// Calculate SHA-256
 | ||||||
| 	hash := sha256.New() | 	hash := sha256.New() | ||||||
| 	if _, err := io.Copy(hash, file); err != nil { | 	if _, err = io.Copy(hash, file); err != nil { | ||||||
| 		return "", err | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 	hashStr = hex.EncodeToString(hash.Sum(nil)) | ||||||
| 	return hex.EncodeToString(hash.Sum(nil)), nil | 	return hashStr, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func parseMsg(confRoot, confName, msg, tag string) ( | func parseMsg(confRoot, confName, msg, tag string) ( | ||||||
|  | @ -104,7 +108,7 @@ func parseMsg(confRoot, confName, msg, tag string) ( | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	if strings.HasPrefix(relPath, "..") { | 	if strings.HasPrefix(relPath, "..") || filepath.IsAbs(relPath) { | ||||||
| 		err = fmt.Errorf("invalid scope as path: %s", conventionalCommit.Scope) | 		err = fmt.Errorf("invalid scope as path: %s", conventionalCommit.Scope) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  | @ -119,14 +123,14 @@ func parseMsg(confRoot, confName, msg, tag string) ( | ||||||
| func hintValidScopes(confRoot, confName string) { | func hintValidScopes(confRoot, confName string) { | ||||||
| 	confRoot = filepath.Clean(confRoot) | 	confRoot = filepath.Clean(confRoot) | ||||||
| 	validScopes := []string{} | 	validScopes := []string{} | ||||||
| 	_ = filepath.Walk(confRoot, func( | 	_ = filepath.WalkDir(confRoot, func( | ||||||
| 		path string, info os.FileInfo, err error, | 		path string, d fs.DirEntry, err error, | ||||||
| 	) error { | 	) error { | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			slog.Error("list valid scopes", "error", err) | 			slog.Error("list valid scopes", "error", err) | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| 		if info.IsDir() { | 		if d.IsDir() { | ||||||
| 			confPath := filepath.Join(path, confName) | 			confPath := filepath.Join(path, confName) | ||||||
| 			if _, err := os.Stat(confPath); err == nil { | 			if _, err := os.Stat(confPath); err == nil { | ||||||
| 				relPath, err := filepath.Rel(confRoot, path) | 				relPath, err := filepath.Rel(confRoot, path) | ||||||
|  |  | ||||||
							
								
								
									
										103
									
								
								cmd/joj3/main.go
									
									
									
									
									
								
							
							
						
						
									
										103
									
								
								cmd/joj3/main.go
									
									
									
									
									
								
							|  | @ -5,6 +5,7 @@ package main | ||||||
| import ( | import ( | ||||||
| 	"flag" | 	"flag" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"io/fs" | ||||||
| 	"log/slog" | 	"log/slog" | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
|  | @ -31,73 +32,63 @@ func init() { | ||||||
| 	printVersion = flag.Bool("version", false, "print current version") | 	printVersion = flag.Bool("version", false, "print current version") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func mainImpl() (err error) { | func getCommitMsg() (string, error) { | ||||||
| 	conf := new(joj3Conf.Conf) |  | ||||||
| 
 |  | ||||||
| 	logger := slog.New(slog.NewTextHandler(os.Stderr, nil)) |  | ||||||
| 	slog.SetDefault(logger) |  | ||||||
| 
 |  | ||||||
| 	// parse flag & conf file
 |  | ||||||
| 	flag.Parse() |  | ||||||
| 	if *printVersion { |  | ||||||
| 		fmt.Println(Version) |  | ||||||
| 		return nil |  | ||||||
| 	} |  | ||||||
| 	if fallbackConfFileName == "" { |  | ||||||
| 		fallbackConfFileName = confFileName |  | ||||||
| 	} |  | ||||||
| 	slog.Info("start joj3", "version", Version) |  | ||||||
| 	commitMsg, err := joj3Conf.GetCommitMsg() | 	commitMsg, err := joj3Conf.GetCommitMsg() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		slog.Error("get commit msg", "error", err) | 		slog.Error("get commit msg", "error", err) | ||||||
| 		return err | 		return "", err | ||||||
| 	} | 	} | ||||||
| 	env.Attr.CommitMsg = commitMsg | 	return commitMsg, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func getConfPath(commitMsg string) (string, fs.FileInfo, *joj3Conf.ConventionalCommit, error) { | ||||||
| 	confPath, confStat, conventionalCommit, err := joj3Conf.GetConfPath( | 	confPath, confStat, conventionalCommit, err := joj3Conf.GetConfPath( | ||||||
| 		confFileRoot, confFileName, fallbackConfFileName, commitMsg, tag, | 		confFileRoot, confFileName, fallbackConfFileName, commitMsg, tag, | ||||||
| 	) | 	) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		slog.Error("get conf path", "error", err) | 		slog.Error("get conf path", "error", err) | ||||||
| 		return err | 		return "", nil, nil, err | ||||||
| 	} | 	} | ||||||
| 	slog.Info("try to load conf", "path", confPath) | 	slog.Info("try to load conf", "path", confPath) | ||||||
| 	conf, err = joj3Conf.ParseConfFile(confPath) | 	return confPath, confStat, conventionalCommit, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func loadConf(confPath string) (*joj3Conf.Conf, error) { | ||||||
|  | 	conf, err := joj3Conf.ParseConfFile(confPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		slog.Error("parse conf", "error", err) | 		slog.Error("parse conf", "error", err) | ||||||
| 		return err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	env.Attr.ConfName = conf.Name |  | ||||||
| 	env.Attr.OutputPath = conf.Stage.OutputPath |  | ||||||
| 	slog.Debug("conf loaded", "conf", conf, "joj3 version", Version) | 	slog.Debug("conf loaded", "conf", conf, "joj3 version", Version) | ||||||
| 	if err := setupSlog(conf); err != nil { | 	return conf, nil | ||||||
| 		slog.Error("setup slog", "error", err) | } | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	// log conf file info
 | func showConfStat(confPath string, confStat fs.FileInfo) error { | ||||||
| 	confSHA256, err := joj3Conf.GetSHA256(confPath) | 	confSHA256, err := joj3Conf.GetSHA256(confPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		slog.Error("get sha256", "error", err) | 		slog.Error("get sha256", "error", err) | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	slog.Info("conf info", "sha256", confSHA256, "modTime", confStat.ModTime(), | 	slog.Info("conf info", "sha256", confSHA256, "modTime", confStat.ModTime(), "size", confStat.Size()) | ||||||
| 		"size", confStat.Size()) | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func validateConf(conf *joj3Conf.Conf) error { | ||||||
| 	if err := joj3Conf.CheckValid(conf); err != nil { | 	if err := joj3Conf.CheckValid(conf); err != nil { | ||||||
| 		slog.Error("conf not valid now", "error", err) | 		slog.Error("conf not valid now", "error", err) | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| 	// run stages
 | func run(conf *joj3Conf.Conf, conventionalCommit *joj3Conf.ConventionalCommit) error { | ||||||
| 	groups := joj3Conf.MatchGroups(conf, conventionalCommit) | 	groups := joj3Conf.MatchGroups(conf, conventionalCommit) | ||||||
| 	env.Attr.Groups = strings.Join(groups, ",") | 	env.Attr.Groups = strings.Join(groups, ",") | ||||||
| 	env.Set() | 	env.Set() | ||||||
| 	_, forceQuitStageName, err := runStages( | 	_, forceQuitStageName, err := runStages( | ||||||
| 		conf, | 		conf, | ||||||
| 		groups, | 		groups, | ||||||
| 		func( | 		func(stageResults []stage.StageResult, forceQuitStageName string) { | ||||||
| 			stageResults []stage.StageResult, |  | ||||||
| 			forceQuitStageName string, |  | ||||||
| 		) { |  | ||||||
| 			env.Attr.ForceQuitStageName = forceQuitStageName | 			env.Attr.ForceQuitStageName = forceQuitStageName | ||||||
| 			env.Set() | 			env.Set() | ||||||
| 		}, | 		}, | ||||||
|  | @ -112,6 +103,48 @@ func mainImpl() (err error) { | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func mainImpl() (err error) { | ||||||
|  | 	logger := slog.New(slog.NewTextHandler(os.Stderr, nil)) | ||||||
|  | 	slog.SetDefault(logger) | ||||||
|  | 	flag.Parse() | ||||||
|  | 	if *printVersion { | ||||||
|  | 		fmt.Println(Version) | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	if fallbackConfFileName == "" { | ||||||
|  | 		fallbackConfFileName = confFileName | ||||||
|  | 	} | ||||||
|  | 	slog.Info("start joj3", "version", Version) | ||||||
|  | 	commitMsg, err := getCommitMsg() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	env.Attr.CommitMsg = commitMsg | ||||||
|  | 	confPath, confStat, conventionalCommit, err := getConfPath(commitMsg) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	conf, err := loadConf(confPath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	env.Attr.ConfName = conf.Name | ||||||
|  | 	env.Attr.OutputPath = conf.Stage.OutputPath | ||||||
|  | 	if err := setupSlog(conf); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	if err := showConfStat(confPath, confStat); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	if err := validateConf(conf); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	if err := run(conf, conventionalCommit); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func main() { | func main() { | ||||||
| 	if err := mainImpl(); err != nil { | 	if err := mainImpl(); err != nil { | ||||||
| 		slog.Error("main exit", "error", err) | 		slog.Error("main exit", "error", err) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user