forked from webdevops/go-crond
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
797999c
commit c5e6b86
Showing
2 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var ( | ||
preloadEnvVars map[string]struct{} = map[string]struct{}{ | ||
"SHELL": struct{}{}, | ||
"PWD": struct{}{}, | ||
"LOGNAME": struct{}{}, | ||
"HOME": struct{}{}, | ||
"LANG": struct{}{}, | ||
"USER": struct{}{}, | ||
"SHLVL": struct{}{}, | ||
"MAILTO": struct{}{}, | ||
"LC_ALL": struct{}{}, | ||
"PATH": struct{}{}, | ||
"_": struct{}{}, | ||
} | ||
reservedEnvVars []string = []string{"LOGNAME", "USER"} | ||
) | ||
|
||
type CronEnvs struct { | ||
envMap map[string]string | ||
} | ||
|
||
func NewCronEnvs(parentEnvs []string) *CronEnvs { | ||
pEnvs := make(map[string]string) | ||
for _, e := range parentEnvs { | ||
splitAt := strings.Index(e, "=") | ||
key := e[:splitAt] | ||
if _, ok := preloadEnvVars[key]; ok { | ||
pEnvs[key] = e[splitAt+1:] | ||
} | ||
} | ||
|
||
return &CronEnvs{ | ||
envMap: pEnvs, | ||
} | ||
} | ||
|
||
func (e *CronEnvs) SetEnv(key, value string) { | ||
e.envMap[key] = value | ||
} | ||
|
||
func (e *CronEnvs) GetEnv(key string) string { | ||
if value, ok := e.envMap[key]; ok { | ||
return value | ||
} else { | ||
return "" | ||
} | ||
} | ||
|
||
func (e *CronEnvs) UpdateEnvForJob(c *CrontabEntry) error { | ||
for _, envStr := range c.Env { | ||
splitAt := strings.Index(envStr, "=") | ||
key := envStr[:splitAt] | ||
for _, env := range reservedEnvVars { | ||
if env == key { | ||
return fmt.Errorf("User can't set env %s", key) | ||
} | ||
} | ||
e.envMap[key] = envStr[splitAt+1:] | ||
} | ||
return nil | ||
} | ||
|
||
func (e *CronEnvs) GetEnvList() []string { | ||
result := []string{} | ||
for k, v := range e.envMap { | ||
result = append(result, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
return result | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c5e6b86
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
related: webdevops#39