Add 'useSystemEnv' config variable
By default - true (use system environment variables, otherwise use only "PATH" variable)
This commit is contained in:
parent
8f18eb8183
commit
2d69cc5be5
@ -46,10 +46,11 @@ type Task struct {
|
|||||||
Command string `yaml:"cmd"`
|
Command string `yaml:"cmd"`
|
||||||
Args []string `yaml:"args"`
|
Args []string `yaml:"args"`
|
||||||
Deadline uint32 `yaml:"deadline"`
|
Deadline uint32 `yaml:"deadline"`
|
||||||
|
UseSystemEnv *bool `yaml:"useSystemEnv,omitempty"`
|
||||||
ConcurrencyPolicy string `yaml:"concurrencyPolicy"`
|
ConcurrencyPolicy string `yaml:"concurrencyPolicy"`
|
||||||
|
|
||||||
// internal values
|
// internal values
|
||||||
path string
|
envVars []string
|
||||||
cmdExe string
|
cmdExe string
|
||||||
cmdArg []string
|
cmdArg []string
|
||||||
buf bytes.Buffer
|
buf bytes.Buffer
|
||||||
@ -60,9 +61,20 @@ type Task struct {
|
|||||||
execMap map[int]*taskExecution
|
execMap map[int]*taskExecution
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) init(shell string, systemPath string) error {
|
func (t *Task) init(shell string, systemEnvs *map[string]string) error {
|
||||||
t.execCount = 0
|
t.execCount = 0
|
||||||
t.path = fmt.Sprintf("PATH=%s", systemPath)
|
|
||||||
|
if t.UseSystemEnv == nil {
|
||||||
|
b := true
|
||||||
|
t.UseSystemEnv = &b
|
||||||
|
}
|
||||||
|
if *t.UseSystemEnv == true {
|
||||||
|
for k, v := range *systemEnvs {
|
||||||
|
t.envVars = append(t.envVars, fmt.Sprintf("%s=%s", k, v))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.envVars = append(t.envVars, fmt.Sprintf("PATH=%s", (*systemEnvs)["PATH"]))
|
||||||
|
}
|
||||||
if shell != "" && t.UseShell {
|
if shell != "" && t.UseShell {
|
||||||
t.cmdExe = shell
|
t.cmdExe = shell
|
||||||
t.cmdArg = []string{"-c", t.Command + " " + strings.Join(t.Args, " ")}
|
t.cmdArg = []string{"-c", t.Command + " " + strings.Join(t.Args, " ")}
|
||||||
@ -87,7 +99,7 @@ func (t *Task) getCmd() *exec.Cmd {
|
|||||||
if t.Dir != "" {
|
if t.Dir != "" {
|
||||||
cmd.Dir = t.Dir
|
cmd.Dir = t.Dir
|
||||||
}
|
}
|
||||||
cmd.Env = append(cmd.Env, t.path)
|
cmd.Env = t.envVars
|
||||||
|
|
||||||
// setup out pipe
|
// setup out pipe
|
||||||
t.buf.Reset()
|
t.buf.Reset()
|
||||||
@ -151,11 +163,15 @@ func (di *ConfigDescriptiveInfo) InitTasks() []error {
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
// get os PATH env
|
// get os PATH env
|
||||||
osPath := os.Getenv("PATH")
|
env := make(map[string]string, 0)
|
||||||
|
for _, e := range os.Environ() {
|
||||||
|
pair := strings.Split(e, "=")
|
||||||
|
env[pair[0]] = pair[1]
|
||||||
|
}
|
||||||
|
|
||||||
var taskNames []string
|
var taskNames []string
|
||||||
for _, t := range di.Tasks {
|
for _, t := range di.Tasks {
|
||||||
if err = t.init(di.Shell, osPath); err != nil {
|
if err = t.init(di.Shell, &env); err != nil {
|
||||||
errList = append(errList, err)
|
errList = append(errList, err)
|
||||||
}
|
}
|
||||||
taskNames = append(taskNames, t.Name)
|
taskNames = append(taskNames, t.Name)
|
||||||
|
@ -17,3 +17,4 @@ tasks:
|
|||||||
crontab: "*/15 * * * * *"
|
crontab: "*/15 * * * * *"
|
||||||
useShell: true
|
useShell: true
|
||||||
cmd: "env"
|
cmd: "env"
|
||||||
|
useSystemEnv: false
|
Loading…
x
Reference in New Issue
Block a user