refactor(cmd/joj3): re-org functions
This commit is contained in:
		
							parent
							
								
									8506887680
								
							
						
					
					
						commit
						1b6fc08451
					
				|  | @ -39,9 +39,27 @@ func getCommitMsg() (string, error) { | ||||||
| 		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 | 	return commitMsg, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func getConf(commitMsg string) (*joj3Conf.Conf, *joj3Conf.ConventionalCommit, error) { | ||||||
|  | 	confPath, confStat, conventionalCommit, err := getConfPath(commitMsg) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, nil, err | ||||||
|  | 	} | ||||||
|  | 	conf, err := loadConf(confPath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, nil, err | ||||||
|  | 	} | ||||||
|  | 	env.Attr.ConfName = conf.Name | ||||||
|  | 	env.Attr.OutputPath = conf.Stage.OutputPath | ||||||
|  | 	if err := showConfStat(confPath, confStat); err != nil { | ||||||
|  | 		return nil, nil, err | ||||||
|  | 	} | ||||||
|  | 	return conf, conventionalCommit, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func getConfPath(commitMsg string) (string, fs.FileInfo, *joj3Conf.ConventionalCommit, error) { | 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, | ||||||
|  | @ -120,23 +138,13 @@ func mainImpl() (err error) { | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	env.Attr.CommitMsg = commitMsg | 	conf, conventionalCommit, err := getConf(commitMsg) | ||||||
| 	confPath, confStat, conventionalCommit, err := getConfPath(commitMsg) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		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 { | 	if err := setupSlog(conf); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	if err := showConfStat(confPath, confStat); err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	if err := validateConf(conf); err != nil { | 	if err := validateConf(conf); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -17,6 +17,27 @@ import ( | ||||||
| 
 | 
 | ||||||
| type StageResult stage.StageResult | type StageResult stage.StageResult | ||||||
| 
 | 
 | ||||||
|  | func newStageCmd(defaultCmd stage.Cmd, optionalCmd conf.OptionalCmd) (stage.Cmd, error) { | ||||||
|  | 	var cmd stage.Cmd | ||||||
|  | 	err := copier.CopyWithOption( | ||||||
|  | 		&cmd, | ||||||
|  | 		&defaultCmd, | ||||||
|  | 		copier.Option{DeepCopy: true}, | ||||||
|  | 	) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return cmd, err | ||||||
|  | 	} | ||||||
|  | 	err = copier.CopyWithOption( | ||||||
|  | 		&cmd, | ||||||
|  | 		&optionalCmd, | ||||||
|  | 		copier.Option{DeepCopy: true, IgnoreEmpty: true}, | ||||||
|  | 	) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return cmd, err | ||||||
|  | 	} | ||||||
|  | 	return cmd, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func generateStages(confStages []conf.ConfStage, groups []string) ( | func generateStages(confStages []conf.ConfStage, groups []string) ( | ||||||
| 	[]stage.Stage, error, | 	[]stage.Stage, error, | ||||||
| ) { | ) { | ||||||
|  | @ -45,71 +66,17 @@ func generateStages(confStages []conf.ConfStage, groups []string) ( | ||||||
| 		existNames[s.Name] = true | 		existNames[s.Name] = true | ||||||
| 		var cmds []stage.Cmd | 		var cmds []stage.Cmd | ||||||
| 		defaultCmd := s.Executor.With.Default | 		defaultCmd := s.Executor.With.Default | ||||||
|  | 		if len(s.Executor.With.Cases) == 0 { | ||||||
|  | 			cmds = []stage.Cmd{defaultCmd} | ||||||
|  | 		} else { | ||||||
| 			for _, optionalCmd := range s.Executor.With.Cases { | 			for _, optionalCmd := range s.Executor.With.Cases { | ||||||
| 			var cmd stage.Cmd | 				cmd, err := newStageCmd(defaultCmd, optionalCmd) | ||||||
| 			err := copier.CopyWithOption( |  | ||||||
| 				&cmd, |  | ||||||
| 				&defaultCmd, |  | ||||||
| 				copier.Option{DeepCopy: true}, |  | ||||||
| 			) |  | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
| 					slog.Error("generate stages", "error", err) | 					slog.Error("generate stages", "error", err) | ||||||
| 					return stages, err | 					return stages, err | ||||||
| 				} | 				} | ||||||
| 			err = copier.CopyWithOption( |  | ||||||
| 				&cmd, |  | ||||||
| 				&optionalCmd, |  | ||||||
| 				copier.Option{DeepCopy: true}, |  | ||||||
| 			) |  | ||||||
| 			if err != nil { |  | ||||||
| 				slog.Error("generate stages", "error", err) |  | ||||||
| 				return stages, err |  | ||||||
| 			} |  | ||||||
| 			// since these 3 values are pointers, copier will always copy
 |  | ||||||
| 			// them, so we need to check them manually
 |  | ||||||
| 			if defaultCmd.Stdin != nil && optionalCmd.Stdin == nil { |  | ||||||
| 				var stdin stage.CmdFile |  | ||||||
| 				err := copier.CopyWithOption( |  | ||||||
| 					&stdin, |  | ||||||
| 					defaultCmd.Stdin, |  | ||||||
| 					copier.Option{DeepCopy: true}, |  | ||||||
| 				) |  | ||||||
| 				if err != nil { |  | ||||||
| 					slog.Error("generate stages", "error", err) |  | ||||||
| 					return stages, err |  | ||||||
| 				} |  | ||||||
| 				cmd.Stdin = &stdin |  | ||||||
| 			} |  | ||||||
| 			if defaultCmd.Stdout != nil && optionalCmd.Stdout == nil { |  | ||||||
| 				var stdout stage.CmdFile |  | ||||||
| 				err := copier.CopyWithOption( |  | ||||||
| 					&stdout, |  | ||||||
| 					defaultCmd.Stdout, |  | ||||||
| 					copier.Option{DeepCopy: true}, |  | ||||||
| 				) |  | ||||||
| 				if err != nil { |  | ||||||
| 					slog.Error("generate stages", "error", err) |  | ||||||
| 					return stages, err |  | ||||||
| 				} |  | ||||||
| 				cmd.Stdout = &stdout |  | ||||||
| 			} |  | ||||||
| 			if defaultCmd.Stderr != nil && optionalCmd.Stderr == nil { |  | ||||||
| 				var stderr stage.CmdFile |  | ||||||
| 				err := copier.CopyWithOption( |  | ||||||
| 					&stderr, |  | ||||||
| 					defaultCmd.Stderr, |  | ||||||
| 					copier.Option{DeepCopy: true}, |  | ||||||
| 				) |  | ||||||
| 				if err != nil { |  | ||||||
| 					slog.Error("generate stages", "error", err) |  | ||||||
| 					return stages, err |  | ||||||
| 				} |  | ||||||
| 				cmd.Stderr = &stderr |  | ||||||
| 			} |  | ||||||
| 				cmds = append(cmds, cmd) | 				cmds = append(cmds, cmd) | ||||||
| 			} | 			} | ||||||
| 		if len(s.Executor.With.Cases) == 0 { |  | ||||||
| 			cmds = []stage.Cmd{defaultCmd} |  | ||||||
| 		} | 		} | ||||||
| 		parsers := []stage.StageParser{} | 		parsers := []stage.StageParser{} | ||||||
| 		for _, p := range s.Parsers { | 		for _, p := range s.Parsers { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user