mirror of
https://github.com/go-task/task.git
synced 2025-06-25 00:47:04 +02:00
refactor: better usage of bytes.Buffer type
This commit is contained in:
6
task.go
6
task.go
@ -284,12 +284,12 @@ func (e *Executor) runCommand(ctx context.Context, call Call, i int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
buff := bytes.NewBuffer(nil)
|
var stdout bytes.Buffer
|
||||||
opts.Stdout = buff
|
opts.Stdout = &stdout
|
||||||
if err = execext.RunCommand(opts); err != nil {
|
if err = execext.RunCommand(opts); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
os.Setenv(t.Set, strings.TrimSpace(buff.String()))
|
os.Setenv(t.Set, strings.TrimSpace(stdout.String()))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -32,20 +32,18 @@ func (e *Executor) handleDynamicVariableContent(value string) (string, error) {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
buff := bytes.NewBuffer(nil)
|
var stdout bytes.Buffer
|
||||||
|
|
||||||
opts := &execext.RunCommandOptions{
|
opts := &execext.RunCommandOptions{
|
||||||
Command: strings.TrimPrefix(value, "$"),
|
Command: strings.TrimPrefix(value, "$"),
|
||||||
Dir: e.Dir,
|
Dir: e.Dir,
|
||||||
Stdout: buff,
|
Stdout: &stdout,
|
||||||
Stderr: e.Stderr,
|
Stderr: e.Stderr,
|
||||||
}
|
}
|
||||||
if err := execext.RunCommand(opts); err != nil {
|
if err := execext.RunCommand(opts); err != nil {
|
||||||
return "", &dynamicVarError{cause: err, cmd: opts.Command}
|
return "", &dynamicVarError{cause: err, cmd: opts.Command}
|
||||||
}
|
}
|
||||||
|
|
||||||
result := buff.String()
|
result := strings.TrimSuffix(stdout.String(), "\n")
|
||||||
result = strings.TrimSuffix(result, "\n")
|
|
||||||
if strings.ContainsRune(result, '\n') {
|
if strings.ContainsRune(result, '\n') {
|
||||||
return "", ErrMultilineResultCmd
|
return "", ErrMultilineResultCmd
|
||||||
}
|
}
|
||||||
@ -140,8 +138,8 @@ func (e *Executor) ReplaceVariables(initial string, call Call) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
b := bytes.NewBuffer(nil)
|
var b bytes.Buffer
|
||||||
if err = templ.Execute(b, vars); err != nil {
|
if err = templ.Execute(&b, vars); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return b.String(), nil
|
return b.String(), nil
|
||||||
|
Reference in New Issue
Block a user