From 995bd56064501f7a51815a0c90e7486074a6aa1c Mon Sep 17 00:00:00 2001 From: zakuro Date: Thu, 14 Jan 2021 18:37:27 +0900 Subject: [PATCH] Implement task env --- config/task.go | 1 + runner/runner.go | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/config/task.go b/config/task.go index 8b22968..c6b105b 100644 --- a/config/task.go +++ b/config/task.go @@ -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"` diff --git a/runner/runner.go b/runner/runner.go index 4a76549..2d2d332 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -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 }