Skip to content

Commit ffb025b

Browse files
committed
supports catch error/continue
1 parent 4928f7c commit ffb025b

3 files changed

Lines changed: 57 additions & 15 deletions

File tree

lib/v1/executor.go

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,19 @@ type (
4747
FinalizeWindows *Step `yaml:"finalize_windows,omitempty"`
4848
}
4949
Step struct {
50-
Name *string `yaml:"name"`
51-
Use string `yaml:"use"`
52-
Cmd string `yaml:"cmd"`
53-
Dir string `yaml:"dir"`
54-
Env []string `yaml:"env"`
55-
If []If `yaml:"if"`
50+
Name *string `yaml:"name"`
51+
Use string `yaml:"use"`
52+
Cmd string `yaml:"cmd"`
53+
Dir string `yaml:"dir"`
54+
Env []string `yaml:"env"`
55+
If []If `yaml:"if"`
56+
IgnoreError bool `yaml:"ignore_error" mapstructure:"ignore_error"`
5657
}
5758
If struct {
5859
Directory *IfExists `yaml:"directory,omitempty"`
5960
File *IfExists `yaml:"file,omitempty"`
6061
Env *[]string `yarl:"env,omitempty"`
62+
IsError *bool `yaml:"is_error,omitempty" mapstructure:"is_error"`
6163
}
6264
IfExists struct {
6365
Exists *string `yaml:"exists,omitempty"`
@@ -345,6 +347,7 @@ func (e *Executor) finalizePlugins() (result error) {
345347

346348
func (e *Executor) runJob(name string, job *Job) (result error) {
347349
util.LogInfo("[lscbuild] start job: %s\n", name)
350+
util.LogDebug("[lscbuild] job: %+v\n", job)
348351
defer func() {
349352
if err := recover(); err != nil {
350353
switch e := err.(type) {
@@ -374,6 +377,15 @@ func (e *Executor) runJob(name string, job *Job) (result error) {
374377
}
375378

376379
for _, step := range job.Steps {
380+
if result != nil && !step.IsError() {
381+
util.LogInfo("[lscbuild] skip step: %s\n", e.stepName(&step))
382+
continue
383+
}
384+
if result == nil && step.IsError() {
385+
util.LogInfo("[lscbuild] skip step: %s\n", e.stepName(&step))
386+
continue
387+
}
388+
377389
if step.Use != "" {
378390
parsed := e.parsePluginUse(step.Use)
379391
cache := e.pluginCache[parsed.GitRepo]
@@ -389,15 +401,13 @@ func (e *Executor) runJob(name string, job *Job) (result error) {
389401
newStep.Env = append(newStep.Env, "LSCBUILD_CWD="+cwd)
390402

391403
result = e.runStep(job, &newStep)
392-
if result != nil {
393-
return result
394-
}
395404
}
396405
} else {
397406
result = e.runStep(job, &step)
398-
if result != nil {
399-
return result
400-
}
407+
}
408+
409+
if step.IgnoreError {
410+
result = nil
401411
}
402412
}
403413

@@ -519,6 +529,9 @@ func (e *Executor) runStep(job *Job, step *Step) (result error) {
519529
cmd.Stderr = os.Stderr
520530

521531
util.LogInfo("[lscbuild] start step: %s\n", stepName)
532+
defer func() {
533+
util.LogInfo("[lscbuild] end step: %s, exit status: %d\n", stepName, cmd.ProcessState.ExitCode())
534+
}()
522535

523536
if err := cmd.Start(); err != nil {
524537
result = err
@@ -529,11 +542,21 @@ func (e *Executor) runStep(job *Job, step *Step) (result error) {
529542
return result
530543
}
531544

532-
util.LogInfo("[lscbuild] end step: %s\n", stepName)
533-
534545
return result
535546
}
536547

548+
func (s *Step) IsError() bool {
549+
if s.If == nil {
550+
return false
551+
}
552+
for _, i := range s.If {
553+
if i.IsError != nil && *i.IsError {
554+
return true
555+
}
556+
}
557+
return false
558+
}
559+
537560
func (s *Step) MustCopy() Step {
538561
// FIXME: slow zone
539562

sample.lscbuild.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,22 @@ jobs:
9696
- cmd: echo ok && exit 0
9797
- cmd: echo error && exit 1
9898
- cmd: echo not exec
99+
100+
test11:
101+
steps:
102+
- cmd: echo ok1
103+
- cmd: invalidcommand
104+
ignore_error: true
105+
- cmd: echo skipped
106+
if:
107+
- is_error: true
108+
- cmd: echo ok2
109+
110+
test12:
111+
steps:
112+
- cmd: echo ok1
113+
- cmd: invalidcommand
114+
- cmd: echo skip
115+
- cmd: echo ok2
116+
if:
117+
- is_error: true

util/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func isDebug() bool {
2525
if cacheIsDebug == nil {
2626
_, ok := lo.Find(os.Environ(), func(s string) bool {
2727
split := strings.Split(s, "=")
28-
return split[0] == "LSCBUILD_DEBUG" && split[1] != "" && split[1] != "0"
28+
return split[0] == "LSCBUILD_DEBUG" && split[1] != "" && split[1] != "0" && split[1] != "false"
2929
})
3030
cacheIsDebug = &ok
3131
}

0 commit comments

Comments
 (0)