realize/realize.go

1189 lines
35 KiB
Go
Raw Normal View History

2016-07-12 08:18:02 +00:00
package main
import (
2017-11-20 15:48:06 +00:00
"github.com/tockins/interact"
"github.com/tockins/realize/realize"
"gopkg.in/urfave/cli.v2"
"log"
2017-09-18 01:05:00 +00:00
"os"
2017-11-20 15:48:06 +00:00
"path/filepath"
"strconv"
2017-11-19 23:52:31 +00:00
"strings"
2017-09-18 01:05:00 +00:00
"time"
2016-07-12 08:18:02 +00:00
)
2017-11-20 15:48:06 +00:00
var r realize.Realize
2016-11-01 09:56:12 +00:00
2017-11-19 23:52:31 +00:00
// Realize cli commands
2017-08-27 11:45:06 +00:00
func main() {
2017-12-04 21:51:23 +00:00
r.Sync = make(chan string)
app := &cli.App{
2017-11-20 15:48:06 +00:00
Name: strings.Title(realize.RPrefix),
Version: realize.RVersion,
2017-11-19 23:52:31 +00:00
Description: "Realize is the #1 Golang Task Runner which enhance your workflow by automating the most common tasks and using the best performing Golang live reloading.",
2016-07-12 18:03:22 +00:00
Commands: []*cli.Command{
{
Name: "start",
2017-11-13 21:55:11 +00:00
Aliases: []string{"s"},
2017-11-20 15:48:06 +00:00
Description: "Start " + strings.Title(realize.RPrefix) + " on a given path. If not exist a config file it creates a new one.",
2016-08-21 18:15:01 +00:00
Flags: []cli.Flag{
2017-11-12 10:16:45 +00:00
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: ".", Usage: "Project base path"},
2017-10-12 14:27:55 +00:00
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "", Usage: "Run a project by its name"},
&cli.BoolFlag{Name: "fmt", Aliases: []string{"f"}, Value: false, Usage: "Enable go fmt"},
&cli.BoolFlag{Name: "vet", Aliases: []string{"v"}, Value: false, Usage: "Enable go vet"},
&cli.BoolFlag{Name: "test", Aliases: []string{"t"}, Value: false, Usage: "Enable go test"},
&cli.BoolFlag{Name: "generate", Aliases: []string{"g"}, Value: false, Usage: "Enable go generate"},
2017-12-04 23:14:41 +00:00
&cli.BoolFlag{Name: "server", Aliases: []string{"srv"}, Value: false, Usage: "Start server"},
&cli.BoolFlag{Name: "open", Aliases: []string{"op"}, Value: false, Usage: "Open into the default browser"},
2017-10-12 14:27:55 +00:00
&cli.BoolFlag{Name: "install", Aliases: []string{"i"}, Value: false, Usage: "Enable go install"},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build"},
2017-09-17 21:37:39 +00:00
&cli.BoolFlag{Name: "run", Aliases: []string{"nr"}, Value: false, Usage: "Enable go run"},
2017-10-12 14:27:55 +00:00
&cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Ignore existing config and doesn't create a new one"},
2016-08-21 18:15:01 +00:00
},
2017-11-19 23:52:31 +00:00
Action: func(c *cli.Context) error {
2017-11-20 15:36:48 +00:00
return start(c)
2016-07-12 18:03:22 +00:00
},
},
{
2017-04-13 13:53:58 +00:00
Name: "add",
Category: "Configuration",
Aliases: []string{"a"},
2017-11-19 23:52:31 +00:00
Description: "Add a project to an existing config or to a new one.",
Flags: []cli.Flag{
2017-11-20 15:48:06 +00:00
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: realize.Wdir(), Usage: "Project base path"},
2017-10-12 14:27:55 +00:00
&cli.BoolFlag{Name: "fmt", Aliases: []string{"f"}, Value: false, Usage: "Enable go fmt"},
&cli.BoolFlag{Name: "vet", Aliases: []string{"v"}, Value: false, Usage: "Enable go vet"},
&cli.BoolFlag{Name: "test", Aliases: []string{"t"}, Value: false, Usage: "Enable go test"},
&cli.BoolFlag{Name: "generate", Aliases: []string{"g"}, Value: false, Usage: "Enable go generate"},
2017-09-17 21:37:39 +00:00
&cli.BoolFlag{Name: "install", Aliases: []string{"i"}, Value: false, Usage: "Enable go install"},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build"},
2017-10-12 14:27:55 +00:00
&cli.BoolFlag{Name: "run", Aliases: []string{"nr"}, Value: false, Usage: "Enable go run"},
},
2017-11-19 23:52:31 +00:00
Action: func(c *cli.Context) error {
2017-11-20 15:36:48 +00:00
return add(c)
},
},
2016-07-23 22:49:19 +00:00
{
2017-04-13 13:53:58 +00:00
Name: "init",
Category: "Configuration",
2017-10-23 12:32:42 +00:00
Aliases: []string{"i"},
2017-11-19 23:52:31 +00:00
Description: "Make a new config file step by step.",
Action: func(c *cli.Context) error {
2017-11-20 15:36:48 +00:00
return setup(c)
2016-07-23 22:49:19 +00:00
},
2016-07-12 18:03:22 +00:00
},
2016-07-26 17:04:13 +00:00
{
2017-04-13 13:53:58 +00:00
Name: "remove",
Category: "Configuration",
Aliases: []string{"r"},
2017-11-19 23:52:31 +00:00
Description: "Remove a project from an existing config.",
2016-07-26 17:04:13 +00:00
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: ""},
2016-07-26 17:04:13 +00:00
},
2017-11-19 23:52:31 +00:00
Action: func(c *cli.Context) error {
2017-11-20 15:36:48 +00:00
return remove(c)
2016-07-26 17:04:13 +00:00
},
},
{
2017-04-13 13:53:58 +00:00
Name: "clean",
Category: "Configuration",
Aliases: []string{"c"},
2017-11-20 15:48:06 +00:00
Description: "Remove " + strings.Title(realize.RPrefix) + " folder.",
2017-11-19 23:52:31 +00:00
Action: func(c *cli.Context) error {
2017-11-20 15:36:48 +00:00
return clean()
},
},
2017-10-23 12:32:42 +00:00
{
Name: "version",
Aliases: []string{"v"},
2017-11-20 15:48:06 +00:00
Description: "Print " + strings.Title(realize.RPrefix) + " version.",
2017-10-23 12:32:42 +00:00
Action: func(p *cli.Context) error {
2017-11-20 15:36:48 +00:00
version()
2017-10-23 12:32:42 +00:00
return nil
},
},
2016-07-12 18:03:22 +00:00
},
}
if err := app.Run(os.Args); err != nil {
2017-11-19 23:52:31 +00:00
log.Fatal(err)
os.Exit(1)
}
2016-08-17 23:35:37 +00:00
}
2017-08-27 11:45:06 +00:00
2017-11-20 15:36:48 +00:00
// Version print current version
func version() {
2017-11-20 15:48:06 +00:00
log.Println(r.Prefix(realize.Green.Bold(realize.RVersion)))
2017-10-12 14:27:55 +00:00
}
2017-12-02 17:38:17 +00:00
// Clean remove realize file
2017-11-20 15:36:48 +00:00
func clean() (err error) {
2017-12-02 17:38:17 +00:00
if err := r.Settings.Remove(realize.RFile); err != nil {
2017-11-20 15:36:48 +00:00
return err
2017-11-19 23:52:31 +00:00
}
2017-11-20 15:48:06 +00:00
log.Println(r.Prefix(realize.Green.Bold("folder successfully removed")))
2017-11-20 15:36:48 +00:00
return nil
}
// Add a project to an existing config or create a new one
func add(c *cli.Context) (err error) {
// read a config if exist
err = r.Settings.Read(&r)
if err != nil {
return err
}
projects := len(r.Schema.Projects)
// create and add a new project
r.Schema.Add(r.Schema.New(c))
if len(r.Schema.Projects) > projects {
// update config
err = r.Settings.Write(r)
if err != nil {
return err
2017-11-19 23:52:31 +00:00
}
2017-11-20 15:48:06 +00:00
log.Println(r.Prefix(realize.Green.Bold("project successfully added")))
2017-11-20 15:36:48 +00:00
} else {
2017-11-20 15:48:06 +00:00
log.Println(r.Prefix(realize.Green.Bold("project can't be added")))
2017-08-27 11:45:06 +00:00
}
2017-11-20 15:36:48 +00:00
return nil
2017-08-27 11:45:06 +00:00
}
2017-11-20 15:36:48 +00:00
// Setup a new config step by step
func setup(c *cli.Context) (err error) {
interact.Run(&interact.Interact{
Before: func(context interact.Context) error {
2017-11-20 15:48:06 +00:00
context.SetErr(realize.Red.Bold("INVALID INPUT"))
context.SetPrfx(realize.Output, realize.Yellow.Regular("[")+time.Now().Format("15:04:05")+realize.Yellow.Regular("]")+realize.Yellow.Bold("[")+strings.ToUpper(realize.RPrefix)+realize.Yellow.Bold("]"))
2017-11-20 15:36:48 +00:00
return nil
},
Questions: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-12-02 17:38:17 +00:00
if _, err := os.Stat(realize.RFile); err != nil {
2017-11-20 15:36:48 +00:00
d.Skip()
}
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
Msg: "Would you want to overwrite existing " + realize.Magenta.Regular(realize.RPrefix) + " config?",
2017-11-20 15:36:48 +00:00
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
} else if val {
2017-11-20 15:48:06 +00:00
r := realize.Realize{}
r.Server = realize.Server{Parent: &r, Status: false, Open: false, Port: realize.Port, Host: realize.Host}
2017-11-20 15:36:48 +00:00
}
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Would you want to customize settings?",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(0, realize.Green.Regular("(os default)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[int]"),
2017-11-20 15:36:48 +00:00
Msg: "Set max number of open files (root required)",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Int()
if err != nil {
return d.Err()
}
r.Settings.FileLimit = int32(val)
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Force polling watcher?",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(100, realize.Green.Regular("(100ms)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[int]"),
2017-11-20 15:36:48 +00:00
Msg: "Set polling interval",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Int()
if err != nil {
return d.Err()
}
r.Settings.Legacy.Interval = time.Duration(int(val)) * time.Millisecond
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Settings.Legacy.Force = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Enable logging files",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
2017-11-20 15:48:06 +00:00
r.Settings.Files.Errors = realize.Resource{Name: realize.FileErr, Status: val}
r.Settings.Files.Outputs = realize.Resource{Name: realize.FileOut, Status: val}
r.Settings.Files.Logs = realize.Resource{Name: realize.FileLog, Status: val}
2017-11-20 15:36:48 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Enable web server",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(realize.Port, realize.Green.Regular("("+strconv.Itoa(realize.Port)+")"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[int]"),
2017-11-20 15:36:48 +00:00
Msg: "Server port",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Int()
if err != nil {
return d.Err()
}
r.Server.Port = int(val)
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(realize.Host, realize.Green.Regular("("+realize.Host+")"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Server host",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Server.Host = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Open in current browser",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Server.Open = val
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Server.Status = val
return nil
},
},
},
Action: func(d interact.Context) interface{} {
_, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(true, realize.Green.Regular("(y)"))
2017-11-20 15:36:48 +00:00
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
Msg: "Would you want to " + realize.Magenta.Regular("add a new project") + "? (insert '!' to stop)",
2017-11-20 15:36:48 +00:00
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
if val {
r.Schema.Add(r.Schema.New(c))
}
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(realize.Wdir(), realize.Green.Regular("("+realize.Wdir()+")"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Project name",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Name = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
dir := realize.Wdir()
d.SetDef(dir, realize.Green.Regular("("+dir+")"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Project path",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Path = filepath.Clean(val)
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Enable go vet",
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef("", realize.Green.Regular("(none)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Vet additional arguments",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
if val != "" {
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Vet.Args = append(r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Vet.Args, val)
}
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Vet.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Enable go fmt",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef("", realize.Green.Regular("(none)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Fmt additional arguments",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
if val != "" {
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Fmt.Args = append(r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Fmt.Args, val)
}
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Fmt.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Enable go test",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef("", realize.Green.Regular("(none)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Test additional arguments",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
if val != "" {
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Test.Args = append(r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Test.Args, val)
}
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Test.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Enable go clean",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef("", realize.Green.Regular("(none)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Clean additional arguments",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
if val != "" {
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Clean.Args = append(r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Clean.Args, val)
}
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Clean.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Enable go generate",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef("", realize.Green.Regular("(none)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Generate additional arguments",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
if val != "" {
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Generate.Args = append(r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Generate.Args, val)
}
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Generate.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(true, realize.Green.Regular("(y)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Enable go install",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef("", realize.Green.Regular("(none)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Install additional arguments",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
if val != "" {
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Install.Args = append(r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Install.Args, val)
}
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Install.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Enable go build",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef("", realize.Green.Regular("(none)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Build additional arguments",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
if val != "" {
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Build.Args = append(r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Build.Args, val)
}
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Build.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(true, realize.Green.Regular("(y)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Enable go run",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
2017-11-20 20:13:07 +00:00
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Run.Status = val
2017-11-20 15:36:48 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Customize watching paths",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
if val {
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Paths = r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Paths[:len(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Paths)-1]
}
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Insert a path to watch (insert '!' to stop)",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Paths = append(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Paths, val)
d.Reload()
return nil
},
},
},
Action: func(d interact.Context) interface{} {
_, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Customize ignore paths",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
if val {
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore = r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore[:len(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore)-1]
}
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Insert a path to ignore (insert '!' to stop)",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore = append(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Ignore, val)
d.Reload()
return nil
},
},
},
Action: func(d interact.Context) interface{} {
_, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Add an additional argument",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Add another argument (insert '!' to stop)",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Args = append(r.Schema.Projects[len(r.Schema.Projects)-1].Args, val)
d.Reload()
return nil
},
},
},
Action: func(d interact.Context) interface{} {
_, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(none)"))
2017-11-20 15:36:48 +00:00
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Add a 'before' custom command (insert '!' to stop)",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Insert a command",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
2017-11-20 15:48:06 +00:00
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts = append(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts, realize.Command{Type: "before", Cmd: val})
2017-11-20 15:36:48 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef("", realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Launch from a specific path",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts[len(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts)-1].Path = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Tag as global command",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts[len(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts)-1].Global = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Display command output",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts[len(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts)-1].Output = val
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
if val {
d.Reload()
}
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(none)"))
2017-11-20 15:36:48 +00:00
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Add an 'after' custom commands (insert '!' to stop)",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Insert a command",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
2017-11-20 15:48:06 +00:00
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts = append(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts, realize.Command{Type: "after", Cmd: val})
2017-11-20 15:36:48 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef("", realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Launch from a specific path",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts[len(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts)-1].Path = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Tag as global command",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts[len(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts)-1].Global = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef(false, realize.Green.Regular("(n)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[y/n]"),
2017-11-20 15:36:48 +00:00
Msg: "Display command output",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts[len(r.Schema.Projects[len(r.Schema.Projects)-1].Watcher.Scripts)-1].Output = val
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
if val {
d.Reload()
}
return nil
},
},
{
Before: func(d interact.Context) error {
2017-11-20 15:48:06 +00:00
d.SetDef("", realize.Green.Regular("(none)"))
2017-11-20 15:36:48 +00:00
return nil
},
Quest: interact.Quest{
2017-11-20 15:48:06 +00:00
Options: realize.Yellow.Regular("[string]"),
2017-11-20 15:36:48 +00:00
Msg: "Set an error output pattern",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].ErrorOutputPattern = val
return nil
},
},
},
Action: func(d interact.Context) interface{} {
if val, err := d.Ans().Bool(); err != nil {
return d.Err()
} else if val {
d.Reload()
}
return nil
},
},
},
After: func(d interact.Context) error {
if val, _ := d.Qns().Get(0).Ans().Bool(); val {
2017-12-02 17:38:17 +00:00
err := r.Settings.Remove(realize.RFile)
2017-11-20 15:36:48 +00:00
if err != nil {
return err
}
}
return nil
},
})
// create config
err = r.Settings.Write(r)
if err != nil {
return err
}
2017-11-20 15:48:06 +00:00
log.Println(r.Prefix(realize.Green.Bold("Config successfully created")))
2017-11-20 15:36:48 +00:00
return nil
}
// Start realize workflow
func start(c *cli.Context) (err error) {
2017-11-20 15:48:06 +00:00
r.Server = realize.Server{Parent: &r, Status: false, Open: false, Port: realize.Port, Host: realize.Host}
2017-11-20 15:36:48 +00:00
// check no-config and read
if !c.Bool("no-config") {
// read a config if exist
2017-12-04 22:39:27 +00:00
r.Settings.Read(&r)
2017-11-20 15:36:48 +00:00
if c.String("name") != "" {
// filter by name flag if exist
2017-12-03 14:44:38 +00:00
r.Schema.Projects = r.Schema.Filter("Name", c.String("name"))
2017-11-20 15:36:48 +00:00
}
// increase file limit
if r.Settings.FileLimit != 0 {
if err = r.Settings.Flimit(); err != nil {
return err
}
}
}
2017-12-04 22:39:27 +00:00
// config and start server
if c.Bool("server") || r.Server.Status {
r.Server.Status = true
if c.Bool("open") || r.Server.Open {
r.Server.Open = true
r.Server.OpenURL()
}
err = r.Server.Start()
if err != nil {
return err
}
}
2017-11-20 15:36:48 +00:00
// check project list length
if len(r.Schema.Projects) <= 0 {
// create a new project based on given params
project := r.Schema.New(c)
// Add to projects list
r.Schema.Add(project)
// save config
if !c.Bool("no-config") {
err = r.Settings.Write(r)
if err != nil {
return err
}
}
2017-08-27 11:45:06 +00:00
}
2017-11-20 15:36:48 +00:00
// start workflow
2017-12-03 14:44:38 +00:00
return r.Start()
2017-08-27 11:45:06 +00:00
}
2017-11-20 15:36:48 +00:00
// Remove a project from an existing config
func remove(c *cli.Context) (err error) {
// read a config if exist
err = r.Settings.Read(&r)
if err != nil {
return err
}
if c.String("name") != "" {
err := r.Schema.Remove(c.String("name"))
if err != nil {
return err
}
// update config
err = r.Settings.Write(r)
if err != nil {
return err
}
2017-11-20 15:48:06 +00:00
log.Println(r.Prefix(realize.Green.Bold("project successfully removed")))
2017-11-20 15:36:48 +00:00
} else {
2017-11-20 15:48:06 +00:00
log.Println(r.Prefix(realize.Green.Bold("project name not found")))
2017-11-20 15:36:48 +00:00
}
return nil
2017-08-27 11:45:06 +00:00
}