Files
task/cmd/task/task.go

152 lines
3.2 KiB
Go
Raw Normal View History

2017-02-27 09:48:50 -03:00
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
2017-02-27 09:48:50 -03:00
"github.com/go-task/task"
"github.com/go-task/task/internal/args"
"github.com/spf13/pflag"
2017-02-27 09:48:50 -03:00
)
2017-06-14 15:28:35 -03:00
var (
version = "master"
)
2018-07-29 22:03:22 +01:00
const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [--dry] [task...]
2017-07-03 14:58:09 +02:00
Runs the specified task(s). Falls back to the "default" task if no task name
was specified, or lists all tasks if an unknown task name was specified.
Example: 'task hello' with the following 'Taskfile.yml' file will generate an
'output.txt' file with the content "hello".
'''
hello:
cmds:
- echo "I am going to write a file named 'output.txt' now."
- echo "hello" > output.txt
generates:
- output.txt
'''
2017-07-06 18:40:01 +02:00
Options:
2017-07-03 14:58:09 +02:00
`
func main() {
log.SetFlags(0)
log.SetOutput(os.Stderr)
2017-07-03 14:58:09 +02:00
pflag.Usage = func() {
log.Print(usage)
pflag.PrintDefaults()
}
var (
2017-06-14 15:28:35 -03:00
versionFlag bool
init bool
list bool
status bool
2017-06-14 15:28:35 -03:00
force bool
watch bool
2017-07-05 20:55:50 -03:00
verbose bool
silent bool
2018-08-05 11:28:02 -03:00
dry bool
2017-07-30 19:24:53 -03:00
dir string
)
2017-06-14 15:28:35 -03:00
pflag.BoolVar(&versionFlag, "version", false, "show Task version")
pflag.BoolVarP(&init, "init", "i", false, "creates a new Taskfile.yml in the current folder")
pflag.BoolVarP(&list, "list", "l", false, "lists tasks with description of current Taskfile")
pflag.BoolVar(&status, "status", false, "exits with non-zero exit code if any of the given tasks is not up-to-date")
pflag.BoolVarP(&force, "force", "f", false, "forces execution even when the task is up-to-date")
pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task")
2017-07-05 20:55:50 -03:00
pflag.BoolVarP(&verbose, "verbose", "v", false, "enables verbose mode")
pflag.BoolVarP(&silent, "silent", "s", false, "disables echoing")
2018-08-05 11:28:02 -03:00
pflag.BoolVar(&dry, "dry", false, "compiles and prints tasks in the order that they would be run, without executing them")
2017-07-30 19:24:53 -03:00
pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution")
pflag.Parse()
2017-06-14 15:28:35 -03:00
if versionFlag {
log.Printf("Task version: %s\n", version)
return
}
if init {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
2017-07-30 19:29:49 -03:00
if err := task.InitTaskfile(os.Stdout, wd); err != nil {
log.Fatal(err)
}
return
}
ctx := context.Background()
if !watch {
ctx = getSignalContext()
}
e := task.Executor{
2017-07-05 20:55:50 -03:00
Force: force,
Watch: watch,
Verbose: verbose,
Silent: silent,
2017-07-30 19:24:53 -03:00
Dir: dir,
2018-08-05 11:28:02 -03:00
Dry: dry,
Context: ctx,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
2018-03-11 14:39:40 -03:00
if err := e.Setup(); err != nil {
log.Fatal(err)
}
if list {
e.PrintTasksHelp()
return
}
arguments := pflag.Args()
if len(arguments) == 0 {
log.Println("task: No argument given, trying default task")
arguments = []string{"default"}
}
calls, err := args.Parse(arguments...)
if err != nil {
log.Fatal(err)
}
if status {
if err = e.Status(calls...); err != nil {
log.Fatal(err)
}
return
}
if err := e.Run(calls...); err != nil {
log.Fatal(err)
}
2017-02-27 09:48:50 -03:00
}
func getSignalContext() context.Context {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go func() {
sig := <-ch
log.Printf("task: signal received: %s", sig)
cancel()
}()
return ctx
}