Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new flag 'shell' which outputs shell commands from a task #1135

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -87,6 +88,7 @@ func run() error {
color bool
interval time.Duration
global bool
shell bool
)

pflag.BoolVar(&versionFlag, "version", false, "Show Task version.")
Expand Down Expand Up @@ -115,6 +117,7 @@ func run() error {
pflag.IntVarP(&concurrency, "concurrency", "C", 0, "Limit number tasks to run concurrently.")
pflag.DurationVarP(&interval, "interval", "I", 0, "Interval to watch for changes.")
pflag.BoolVarP(&global, "global", "g", false, "Runs global Taskfile, from $HOME/Taskfile.{yml,yaml}.")
pflag.BoolVarP(&shell, "shell", "", false, "Dump raw shell script")
pflag.Parse()

if versionFlag {
Expand Down Expand Up @@ -235,6 +238,12 @@ func run() error {
return err
}

if shell {
e.Shell = true
// disable all other logs except purely shell output
e.Logger.Stdout = io.Discard
}

if e.Taskfile.Version.Compare(taskfile.V3) >= 0 {
calls, globals = args.ParseV3(tasksAndVars...)
} else {
Expand Down
5 changes: 5 additions & 0 deletions precondition.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ var ErrPreconditionFailed = errors.New("task: precondition not met")

func (e *Executor) areTaskPreconditionsMet(ctx context.Context, t *taskfile.Task) (bool, error) {
for _, p := range t.Preconditions {
// log precondition command if shell flag is passed
if e.Shell {
e.Logger.FOutf(e.Stdout, logger.Default, "%s\n", p.Sh)
return true, nil
}
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: p.Sh,
Dir: t.Dir,
Expand Down
6 changes: 6 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Executor struct {
Verbose bool
Silent bool
Dry bool
Shell bool
Summary bool
Parallel bool
Color bool
Expand Down Expand Up @@ -278,6 +279,11 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
return nil
}

if e.Shell {
e.Logger.FOutf(e.Stdout, logger.Default, "%s\n", cmd.Cmd)
return nil
}

if e.Verbose || (!cmd.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) {
e.Logger.Errf(logger.Green, "task: [%s] %s", t.Name(), cmd.Cmd)
}
Expand Down
24 changes: 24 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1892,3 +1892,27 @@ func TestSplitArgs(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "3\n", buff.String())
}

func TestShell(t *testing.T) {
// reuse testdata/dry since 'shell' flag has similar behavior
const dir = "testdata/dry"

file := filepathext.SmartJoin(dir, "file.txt")
_ = os.Remove(file)

var buff bytes.Buffer

e := task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: io.Discard,
Shell: true,
}
require.NoError(t, e.Setup())
require.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"}))

assert.Equal(t, "touch file.txt", strings.TrimSpace(buff.String()))
if _, err := os.Stat(file); err == nil {
t.Errorf("File should not exist %s", file)
}
}