mirror of
https://github.com/go-task/task.git
synced 2025-08-10 22:42:19 +02:00
Small refactor of variables replacing
This commit is contained in:
29
task.go
29
task.go
@@ -109,18 +109,13 @@ func RunTask(ctx context.Context, name string) error {
|
||||
}
|
||||
|
||||
func (t *Task) runDeps(ctx context.Context) error {
|
||||
vars, err := t.handleVariables()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
for _, d := range t.Deps {
|
||||
dep := d
|
||||
|
||||
g.Go(func() error {
|
||||
dep, err := ReplaceVariables(dep, vars)
|
||||
dep, err := t.ReplaceVariables(dep)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -132,7 +127,7 @@ func (t *Task) runDeps(ctx context.Context) error {
|
||||
})
|
||||
}
|
||||
|
||||
if err = g.Wait(); err != nil {
|
||||
if err := g.Wait(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -157,11 +152,7 @@ func (t *Task) isUpToDate() bool {
|
||||
}
|
||||
|
||||
func (t *Task) runCommand(ctx context.Context, i int) error {
|
||||
vars, err := t.handleVariables()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := ReplaceVariables(t.Cmds[i], vars)
|
||||
c, err := t.ReplaceVariables(t.Cmds[i])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -174,12 +165,12 @@ func (t *Task) runCommand(ctx context.Context, i int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
dir, err := ReplaceVariables(t.Dir, vars)
|
||||
dir, err := t.ReplaceVariables(t.Dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
envs, err := t.getEnviron(vars)
|
||||
envs, err := t.getEnviron()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -209,7 +200,7 @@ func (t *Task) runCommand(ctx context.Context, i int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Task) getEnviron(vars map[string]string) ([]string, error) {
|
||||
func (t *Task) getEnviron() ([]string, error) {
|
||||
if t.Env == nil {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -217,15 +208,11 @@ func (t *Task) getEnviron(vars map[string]string) ([]string, error) {
|
||||
envs := os.Environ()
|
||||
|
||||
for k, v := range t.Env {
|
||||
replacedValue, err := ReplaceVariables(v, vars)
|
||||
env, err := t.ReplaceVariables(fmt.Sprintf("%s=%s", k, v))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
replacedKey, err := ReplaceVariables(k, vars)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
envs = append(envs, fmt.Sprintf("%s=%s", replacedKey, replacedValue))
|
||||
envs = append(envs, env)
|
||||
}
|
||||
return envs, nil
|
||||
}
|
||||
|
@@ -25,15 +25,10 @@ var (
|
||||
ErrMultilineResultCmd = errors.New("Got multiline result from command")
|
||||
)
|
||||
|
||||
var varCmds = make(map[string]string)
|
||||
|
||||
func handleDynamicVariableContent(value string) (string, error) {
|
||||
if !strings.HasPrefix(value, "$") {
|
||||
return value, nil
|
||||
}
|
||||
if result, ok := varCmds[value]; ok {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
buff := bytes.NewBuffer(nil)
|
||||
|
||||
@@ -53,11 +48,10 @@ func handleDynamicVariableContent(value string) (string, error) {
|
||||
}
|
||||
|
||||
result = strings.TrimSpace(result)
|
||||
varCmds[value] = result
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (t *Task) handleVariables() (map[string]string, error) {
|
||||
func (t *Task) getVariables() (map[string]string, error) {
|
||||
localVariables := make(map[string]string)
|
||||
for key, value := range t.Vars {
|
||||
val, err := handleDynamicVariableContent(value)
|
||||
@@ -106,13 +100,19 @@ func init() {
|
||||
}
|
||||
|
||||
// ReplaceVariables writes vars into initial string
|
||||
func ReplaceVariables(initial string, vars map[string]string) (string, error) {
|
||||
t, err := template.New("").Funcs(templateFuncs).Parse(initial)
|
||||
func (t *Task) ReplaceVariables(initial string) (string, error) {
|
||||
vars, err := t.getVariables()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
templ, err := template.New("").Funcs(templateFuncs).Parse(initial)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
b := bytes.NewBuffer(nil)
|
||||
if err = t.Execute(b, vars); err != nil {
|
||||
if err = templ.Execute(b, vars); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return b.String(), nil
|
||||
|
Reference in New Issue
Block a user