2017-02-27 09:48:50 -03:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2017-11-12 17:39:29 -02:00
|
|
|
"context"
|
2017-06-04 16:02:04 -03:00
|
|
|
"log"
|
2017-07-01 15:05:51 -03:00
|
|
|
"os"
|
2017-11-12 17:39:29 -02:00
|
|
|
"os/signal"
|
|
|
|
|
"syscall"
|
2017-02-28 08:13:48 -08:00
|
|
|
|
2017-02-27 09:48:50 -03:00
|
|
|
"github.com/go-task/task"
|
2017-10-15 17:58:21 -02:00
|
|
|
"github.com/go-task/task/internal/args"
|
2017-03-01 20:43:27 -03:00
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
2017-02-27 09:48:50 -03:00
|
|
|
)
|
|
|
|
|
|
2017-06-14 15:28:35 -03:00
|
|
|
var (
|
|
|
|
|
version = "master"
|
|
|
|
|
)
|
|
|
|
|
|
2017-07-30 19:24:53 -03:00
|
|
|
const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [task...]
|
2017-06-04 16:02:04 -03:00
|
|
|
|
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".
|
2017-02-28 08:13:48 -08:00
|
|
|
|
|
|
|
|
'''
|
2017-02-28 08:40:17 -08:00
|
|
|
hello:
|
|
|
|
|
cmds:
|
|
|
|
|
- echo "I am going to write a file named 'output.txt' now."
|
|
|
|
|
- echo "hello" > output.txt
|
|
|
|
|
generates:
|
|
|
|
|
- output.txt
|
2017-02-28 08:13:48 -08:00
|
|
|
'''
|
2017-07-06 18:40:01 +02:00
|
|
|
|
|
|
|
|
Options:
|
2017-07-03 14:58:09 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
log.SetFlags(0)
|
2017-11-12 17:39:29 -02:00
|
|
|
log.SetOutput(os.Stderr)
|
2017-07-03 14:58:09 +02:00
|
|
|
|
|
|
|
|
pflag.Usage = func() {
|
2017-11-12 17:39:29 -02:00
|
|
|
log.Print(usage)
|
2017-03-01 20:43:27 -03:00
|
|
|
pflag.PrintDefaults()
|
2017-02-28 08:13:48 -08:00
|
|
|
}
|
2017-06-04 16:02:04 -03:00
|
|
|
|
|
|
|
|
var (
|
2017-06-14 15:28:35 -03:00
|
|
|
versionFlag bool
|
|
|
|
|
init bool
|
2017-07-15 14:09:27 -03:00
|
|
|
list bool
|
2017-12-26 21:43:52 -02:00
|
|
|
status bool
|
2017-06-14 15:28:35 -03:00
|
|
|
force bool
|
|
|
|
|
watch bool
|
2017-07-05 20:55:50 -03:00
|
|
|
verbose bool
|
2017-07-19 20:20:24 -03:00
|
|
|
silent bool
|
2017-07-30 19:24:53 -03:00
|
|
|
dir string
|
2017-06-04 16:02:04 -03:00
|
|
|
)
|
|
|
|
|
|
2017-06-14 15:28:35 -03:00
|
|
|
pflag.BoolVar(&versionFlag, "version", false, "show Task version")
|
2017-06-04 16:02:04 -03:00
|
|
|
pflag.BoolVarP(&init, "init", "i", false, "creates a new Taskfile.yml in the current folder")
|
2017-07-15 14:09:27 -03:00
|
|
|
pflag.BoolVarP(&list, "list", "l", false, "lists tasks with description of current Taskfile")
|
2017-12-26 21:43:52 -02:00
|
|
|
pflag.BoolVar(&status, "status", false, "exits with non-zero exit code if any of the given tasks is not up-to-date")
|
2017-06-04 16:02:04 -03:00
|
|
|
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")
|
2017-07-19 20:20:24 -03:00
|
|
|
pflag.BoolVarP(&silent, "silent", "s", false, "disables echoing")
|
2017-07-30 19:24:53 -03:00
|
|
|
pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution")
|
2017-03-01 20:43:27 -03:00
|
|
|
pflag.Parse()
|
2017-06-04 16:02:04 -03:00
|
|
|
|
2017-06-14 15:28:35 -03:00
|
|
|
if versionFlag {
|
|
|
|
|
log.Printf("Task version: %s\n", version)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-04 16:02:04 -03:00
|
|
|
if init {
|
2017-07-01 15:32:13 -03:00
|
|
|
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 {
|
2017-06-04 16:02:04 -03:00
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
e := task.Executor{
|
2017-07-05 20:55:50 -03:00
|
|
|
Force: force,
|
|
|
|
|
Watch: watch,
|
|
|
|
|
Verbose: verbose,
|
2017-07-19 20:20:24 -03:00
|
|
|
Silent: silent,
|
2017-07-30 19:24:53 -03:00
|
|
|
Dir: dir,
|
2017-07-01 15:05:51 -03:00
|
|
|
|
2017-11-12 17:39:29 -02:00
|
|
|
Context: getSignalContext(),
|
|
|
|
|
|
2017-07-01 15:05:51 -03:00
|
|
|
Stdin: os.Stdin,
|
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
|
Stderr: os.Stderr,
|
2017-06-04 16:02:04 -03:00
|
|
|
}
|
|
|
|
|
if err := e.ReadTaskfile(); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 14:09:27 -03:00
|
|
|
if list {
|
|
|
|
|
e.PrintTasksHelp()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 13:57:06 -03:00
|
|
|
arguments := pflag.Args()
|
|
|
|
|
if len(arguments) == 0 {
|
2017-06-04 16:02:04 -03:00
|
|
|
log.Println("task: No argument given, trying default task")
|
2017-09-07 13:57:06 -03:00
|
|
|
arguments = []string{"default"}
|
2017-06-04 16:02:04 -03:00
|
|
|
}
|
|
|
|
|
|
2017-09-07 13:57:06 -03:00
|
|
|
calls, err := args.Parse(arguments...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 21:43:52 -02:00
|
|
|
if status {
|
|
|
|
|
if err = e.Status(calls...); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 13:57:06 -03:00
|
|
|
if err := e.Run(calls...); err != nil {
|
2017-06-04 16:02:04 -03:00
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2017-02-27 09:48:50 -03:00
|
|
|
}
|
2017-11-12 17:39:29 -02: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
|
|
|
|
|
}
|