Skip to content

Commit

Permalink
Implement task env
Browse files Browse the repository at this point in the history
  • Loading branch information
zakuro9715 committed Jan 14, 2021
1 parent 1d9cd07 commit 995bd56
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type task struct {
FullName string
Shell string `yaml:"shell"`
Cmds Cmds `yaml:"run"`
Envs Envs `yaml:"env"`
Config *Config
Parent *Task
Description string `yaml:"desc"`
Expand Down
21 changes: 16 additions & 5 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,32 @@ func isScriptFile(name string) bool {

func setDefaultEnvs(envs config.Envs) error {
for k, v := range envs {
if _, found := os.LookupEnv(k); !found {
if err := os.Setenv(k, v); err != nil {
return err
}
if got := os.Getenv(k); len(got) > 0 {
continue
}
if err := os.Setenv(k, v); err != nil {
return err
}
}
return nil
}

func setTaskDefaultEnvs(task *config.Task) error {
if err := setDefaultEnvs(task.Envs); err != nil {
return err
}
if task.Parent == nil {
return setDefaultEnvs(task.Config.Envs)
}
return setTaskDefaultEnvs(task.Parent)
}

func (r *TaskRunner) Run(args []string) error {
if err := r.task.Verify(); err != nil {
return err
}

if err := setDefaultEnvs(r.task.Config.Envs); err != nil {
if err := setTaskDefaultEnvs(r.task); err != nil {
return err
}

Expand Down

0 comments on commit 995bd56

Please sign in to comment.