refactor(cmd/joj3): re-org functions
All checks were successful
submodules sync / sync (push) Successful in 42s
build / build (push) Successful in 1m35s
build / trigger-build-image (push) Successful in 9s

This commit is contained in:
张泊明518370910136 2025-06-29 05:43:35 -04:00
parent 8506887680
commit 1b6fc08451
GPG Key ID: D47306D7062CDA9D
2 changed files with 49 additions and 74 deletions

View File

@ -39,9 +39,27 @@ func getCommitMsg() (string, error) {
slog.Error("get commit msg", "error", err)
return "", err
}
env.Attr.CommitMsg = commitMsg
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) {
confPath, confStat, conventionalCommit, err := joj3Conf.GetConfPath(
confFileRoot, confFileName, fallbackConfFileName, commitMsg, tag,
@ -120,23 +138,13 @@ func mainImpl() (err error) {
if err != nil {
return err
}
env.Attr.CommitMsg = commitMsg
confPath, confStat, conventionalCommit, err := getConfPath(commitMsg)
conf, conventionalCommit, err := getConf(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
}

View File

@ -17,6 +17,27 @@ import (
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) (
[]stage.Stage, error,
) {
@ -45,71 +66,17 @@ func generateStages(confStages []conf.ConfStage, groups []string) (
existNames[s.Name] = true
var cmds []stage.Cmd
defaultCmd := s.Executor.With.Default
for _, optionalCmd := range s.Executor.With.Cases {
var cmd stage.Cmd
err := copier.CopyWithOption(
&cmd,
&defaultCmd,
copier.Option{DeepCopy: true},
)
if err != nil {
slog.Error("generate stages", "error", 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)
}
if len(s.Executor.With.Cases) == 0 {
cmds = []stage.Cmd{defaultCmd}
} else {
for _, optionalCmd := range s.Executor.With.Cases {
cmd, err := newStageCmd(defaultCmd, optionalCmd)
if err != nil {
slog.Error("generate stages", "error", err)
return stages, err
}
cmds = append(cmds, cmd)
}
}
parsers := []stage.StageParser{}
for _, p := range s.Parsers {