realize/realize.go

971 lines
31 KiB
Go
Raw Normal View History

2016-07-12 08:18:02 +00:00
package main
import (
2016-11-01 09:56:12 +00:00
"errors"
"fmt"
2016-08-17 23:35:37 +00:00
"os"
"time"
"github.com/fatih/color"
"github.com/tockins/interact"
"github.com/tockins/realize/server"
"github.com/tockins/realize/settings"
"github.com/tockins/realize/style"
"github.com/tockins/realize/watcher"
cli "gopkg.in/urfave/cli.v2"
2016-07-12 08:18:02 +00:00
)
2016-11-01 09:56:12 +00:00
const (
2017-04-13 13:53:58 +00:00
appVersion = "1.3"
2017-04-16 10:02:47 +00:00
config = "realize.yaml"
outputs = "outputs.log"
errs = "errors.log"
logs = "logs.log"
host = "localhost"
port = 5001
interval = 200
2016-11-01 09:56:12 +00:00
)
// Cli commands
func main() {
// Realize struct contains the general app informations
type realize struct {
settings.Settings `yaml:"settings,omitempty"`
Sync chan string `yaml:"-"`
Blueprint watcher.Blueprint `yaml:"-"`
Server server.Server `yaml:"-"`
Projects *[]watcher.Project `yaml:"projects" json:"projects"`
2016-11-01 09:56:12 +00:00
}
2016-09-01 22:17:19 +00:00
var r realize
// Before of every exec of a cli method
before := func(*cli.Context) error {
gopath := os.Getenv("GOPATH")
if gopath == "" {
2017-04-15 18:11:05 +00:00
return errors.New("$GOPATH isn't set properly")
}
2016-11-01 09:56:12 +00:00
r = realize{
Sync: make(chan string),
Settings: settings.Settings{
Config: settings.Config{
Create: true,
},
Resources: settings.Resources{
Config: config,
Outputs: outputs,
Logs: logs,
Errors: errs,
},
Server: settings.Server{
Status: false,
Open: false,
Host: host,
Port: port,
},
},
}
r.Blueprint = watcher.Blueprint{
Settings: &r.Settings,
Sync: r.Sync,
}
r.Server = server.Server{
Blueprint: &r.Blueprint,
Settings: &r.Settings,
Sync: r.Sync,
}
r.Projects = &r.Blueprint.Projects
2016-11-01 09:56:12 +00:00
// read if exist
if err := r.Read(&r); err != nil {
return err
}
// increase the file limit
if r.Config.Flimit != 0 {
if err := r.Flimit(); err != nil {
return err
}
}
return nil
2016-11-01 09:56:12 +00:00
}
app := &cli.App{
2017-04-13 13:53:58 +00:00
Name: "Realize",
Version: appVersion,
2016-07-23 22:49:19 +00:00
Authors: []*cli.Author{
2016-08-21 14:35:17 +00:00
{
2016-08-23 13:17:44 +00:00
Name: "Alessio Pracchia",
2017-04-01 17:33:21 +00:00
Email: "pracchia@hastega.it",
2016-08-23 13:17:44 +00:00
},
{
Name: "Daniele Conventi",
2017-04-01 17:33:21 +00:00
Email: "conventi@hastega.it",
2016-07-23 22:49:19 +00:00
},
},
2017-04-13 13:53:58 +00:00
Description: "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths",
2016-07-12 18:03:22 +00:00
Commands: []*cli.Command{
{
2017-04-13 13:53:58 +00:00
Name: "run",
Aliases: []string{"r"},
Description: "Run a toolchain on a project or a list of projects. If not exist a config file it creates a new one",
2016-08-21 18:15:01 +00:00
Flags: []cli.Flag{
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: "", Usage: "Project base path."},
2017-06-16 09:15:56 +00:00
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "", Usage: "Run a project by its name."},
&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."},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build."},
&cli.BoolFlag{Name: "legacy", Aliases: []string{"l"}, Value: false, Usage: "Watch by polling instead of Watch by fsnotify."},
&cli.BoolFlag{Name: "server", Aliases: []string{"s"}, Value: false, Usage: "Enable server and open into the default browser."},
&cli.BoolFlag{Name: "no-run", Aliases: []string{"nr"}, Value: false, Usage: "Disable go run"},
&cli.BoolFlag{Name: "no-install", Aliases: []string{"ni"}, Value: false, Usage: "Disable go install"},
&cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Ignore existing configurations."},
2016-08-21 18:15:01 +00:00
},
2016-07-27 09:14:32 +00:00
Action: func(p *cli.Context) error {
2017-06-16 09:15:56 +00:00
c := r
if p.String("name") != ""{
for index, project := range r.Blueprint.Projects{
if project.Name == p.String("name"){
c.Blueprint.Projects = []watcher.Project{r.Blueprint.Projects[index]}
}
}
}
if p.Bool("legacy") {
r.Config.Legacy = settings.Legacy{
Status: p.Bool("legacy"),
Interval: interval,
}
}
if p.Bool("no-config") || len(r.Blueprint.Projects) <= 0 {
if p.Bool("no-config") {
r.Config.Create = false
}
r.Blueprint.Projects = []watcher.Project{}
if err := r.Blueprint.Add(p); err != nil {
return err
}
}
2017-06-16 09:15:56 +00:00
if err := c.Server.Start(p); err != nil {
return err
}
2017-06-16 09:15:56 +00:00
if err := c.Blueprint.Run(p); err != nil {
return err
}
2017-06-16 09:15:56 +00:00
if err := r.Record(c); err != nil {
return err
2016-11-01 09:56:12 +00:00
}
return nil
2016-07-12 18:03:22 +00:00
},
Before: before,
2016-07-12 18:03:22 +00:00
},
{
2017-04-13 13:53:58 +00:00
Name: "add",
Category: "Configuration",
Aliases: []string{"a"},
Description: "Add a project to an existing config file or create a new one.",
Flags: []cli.Flag{
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: "", Usage: "Project base path."},
&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."},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build."},
&cli.BoolFlag{Name: "legacy", Aliases: []string{"l"}, Value: false, Usage: "Watch by polling instead of Watch by fsnotify."},
&cli.BoolFlag{Name: "server", Aliases: []string{"s"}, Value: false, Usage: "Enable server and open into the default browser."},
&cli.BoolFlag{Name: "no-run", Aliases: []string{"nr"}, Value: false, Usage: "Disable go run"},
&cli.BoolFlag{Name: "no-fmt", Aliases: []string{"nf"}, Value: false, Usage: "Disable go fmt."},
&cli.BoolFlag{Name: "no-install", Aliases: []string{"ni"}, Value: false, Usage: "Disable go install"},
&cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Ignore existing configurations."},
},
Action: func(p *cli.Context) error {
fmt.Println(p.String("path"))
if err := r.Blueprint.Add(p); err != nil {
return err
}
if err := r.Record(r); err != nil {
return err
}
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your project was successfully added."))
return nil
},
Before: before,
},
2016-07-23 22:49:19 +00:00
{
2017-04-13 13:53:58 +00:00
Name: "init",
Category: "Configuration",
Aliases: []string{"a"},
Description: "Define a new config file with all options step by step",
Action: func(p *cli.Context) (actErr error) {
interact.Run(&interact.Interact{
Before: func(context interact.Context) error {
context.SetErr(style.Red.Bold("INVALID INPUT"))
context.SetPrfx(color.Output, style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"))
return nil
},
Questions: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-04-16 10:02:47 +00:00
if _, err := os.Stat(settings.Dir + config); err != nil {
2017-03-19 23:19:05 +00:00
d.Skip()
}
d.SetDef(false, style.Green.Regular("(n)"))
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Would you want to overwrite the existing " + style.Magenta.Bold("Realize") + " config?",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
} else if val {
r.Settings = settings.Settings{
Config: settings.Config{
2017-04-01 17:33:21 +00:00
Create: true,
},
Resources: settings.Resources{
2017-04-01 17:33:21 +00:00
Config: config,
Outputs: outputs,
Logs: logs,
Errors: errs,
},
Server: settings.Server{
2017-04-01 17:33:21 +00:00
Status: false,
Open: false,
Host: host,
Port: port,
},
}
2017-03-19 23:19:05 +00:00
r.Blueprint.Projects = r.Blueprint.Projects[len(r.Blueprint.Projects):]
}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Would you want to customize the " + ("settings") + "?",
Resolve: func(d interact.Context) bool {
2017-03-19 23:19:05 +00:00
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetDef(0, style.Green.Regular("(os default)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[int]"),
Msg: "Max number of open files (root required)",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Int()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
r.Config.Flimit = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable legacy watch by polling",
Resolve: func(d interact.Context) bool {
2017-03-19 23:19:05 +00:00
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetDef(1, style.Green.Regular("(1s)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[seconds]"),
Msg: "Set polling interval in seconds",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Int()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-03-13 00:04:48 +00:00
r.Config.Legacy.Interval = time.Duration(val * 1000000000)
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
r.Config.Legacy.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
2017-03-19 23:19:05 +00:00
Msg: "Enable web server",
Resolve: func(d interact.Context) bool {
2017-03-19 23:19:05 +00:00
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
2017-03-19 23:19:05 +00:00
{
Before: func(d interact.Context) error {
d.SetDef(5001, style.Green.Regular("(5001)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[int]"),
2017-03-19 23:19:05 +00:00
Msg: "Server port",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Int()
if err != nil {
return d.Err()
}
r.Server.Port = int(val)
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef("localhost", style.Green.Regular("(localhost)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[string]"),
2017-03-19 23:19:05 +00:00
Msg: "Server host",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Server.Host = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
2017-03-19 23:19:05 +00:00
Msg: "Open in the current browser",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Server.Open = val
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Server.Status = val
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
_, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(true, style.Green.Regular("(y)"))
2017-04-01 17:33:21 +00:00
d.SetEnd("!")
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Would you want to " + style.Magenta.Regular("add a new project") + "? (insert '!' to stop)",
Resolve: func(d interact.Context) bool {
2017-03-19 23:19:05 +00:00
val, _ := d.Ans().Bool()
if val {
r.Blueprint.Add(p)
}
return val
},
},
Subs: []*interact.Question{
2017-03-19 23:19:05 +00:00
{
Before: func(d interact.Context) error {
d.SetDef(r.Settings.Wdir(), style.Green.Regular("("+r.Settings.Wdir()+")"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[string]"),
Msg: "Project name",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().String()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-03-19 23:19:05 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Name = val
return nil
},
},
{
Before: func(d interact.Context) error {
2017-03-19 23:19:05 +00:00
dir, _ := os.Getwd()
d.SetDef(dir, style.Green.Regular("("+dir+")"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[string]"),
Msg: "Project path",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().String()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-03-19 23:19:05 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Path = r.Settings.Path(val)
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(true, style.Green.Regular("(y)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable go fmt",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-04-16 10:02:47 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Fmt = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(true, style.Green.Regular("(y)"))
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable go vet",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Vet = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable go test",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-04-16 10:02:47 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Test = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable go generate",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-04-16 10:02:47 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Generate = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(true, style.Green.Regular("(y)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable go install",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-04-16 10:02:47 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Bin.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable go build",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-04-16 10:02:47 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Build.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(true, style.Green.Regular("(y)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable go run",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-04-16 10:02:47 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Run = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
2017-04-15 18:11:05 +00:00
Msg: "Customize watched paths",
Resolve: func(d interact.Context) bool {
2017-03-19 23:19:05 +00:00
val, _ := d.Ans().Bool()
if val {
2017-03-19 23:19:05 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Paths = r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Paths[:len(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Paths)-1]
}
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-03-19 23:19:05 +00:00
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[string]"),
Msg: "Insert a path to watch (insert '!' to stop)",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().String()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-03-19 23:19:05 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Paths = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Paths, val)
d.Reload()
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
_, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
2017-04-15 18:11:05 +00:00
Msg: "Customize ignored paths",
Resolve: func(d interact.Context) bool {
2017-03-19 23:19:05 +00:00
val, _ := d.Ans().Bool()
if val {
2017-03-19 23:19:05 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Ignore = r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Ignore[:len(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Ignore)-1]
}
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-03-19 23:19:05 +00:00
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[string]"),
Msg: "Insert a path to ignore (insert '!' to stop)",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().String()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-03-19 23:19:05 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Ignore = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Ignore, val)
d.Reload()
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
_, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
2017-03-19 23:19:05 +00:00
Msg: "Add additionals arguments",
Resolve: func(d interact.Context) bool {
2017-03-19 23:19:05 +00:00
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-03-19 23:19:05 +00:00
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[string]"),
Msg: "Insert an argument (insert '!' to stop)",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().String()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-04-16 10:02:47 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Args = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Args, val)
2017-03-19 23:19:05 +00:00
d.Reload()
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
_, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
2017-03-19 23:19:05 +00:00
Msg: "Add 'before' custom commands",
Resolve: func(d interact.Context) bool {
2017-03-19 23:19:05 +00:00
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-03-19 23:19:05 +00:00
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[string]"),
Msg: "Insert a command (insert '!' to stop)",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().String()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-06-19 10:08:51 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val, Changed: true, Startup: true})
2017-03-19 23:19:05 +00:00
d.Reload()
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
_, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
2017-03-19 23:19:05 +00:00
Msg: "Add 'after' custom commands",
Resolve: func(d interact.Context) bool {
2017-03-19 23:19:05 +00:00
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-03-19 23:19:05 +00:00
d.SetEnd("!")
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[string]"),
Msg: "Insert a command (insert '!' to stop)",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().String()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-06-19 10:08:51 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val, Changed: true, Startup: true})
2017-03-19 23:19:05 +00:00
d.Reload()
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
_, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable watcher files preview",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-03-20 00:39:25 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Preview = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable file output history",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-03-19 23:19:05 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Streams.FileOut = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable file logs history",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-03-19 23:19:05 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Streams.FileLog = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, style.Green.Regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[y/n]"),
Msg: "Enable file errors history",
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-03-19 23:19:05 +00:00
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Streams.FileErr = val
return nil
},
},
2017-06-16 09:48:25 +00:00
{
Before: func(d interact.Context) error {
d.SetDef("", style.Green.Regular("(none)"))
return nil
},
Quest: interact.Quest{
Options: style.Yellow.Regular("[string]"),
Msg: "Set an error output pattern",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].ErrorOutputPattern = val
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-03-19 23:19:05 +00:00
if val, err := d.Ans().Bool(); err != nil {
return d.Err()
} else if val {
d.Reload()
}
return nil
},
},
},
After: func(d interact.Context) error {
2017-03-19 23:19:05 +00:00
if val, _ := d.Qns().Get(0).Ans().Bool(); val {
actErr = r.Settings.Remove()
if actErr != nil {
return actErr
2017-03-19 23:19:05 +00:00
}
}
return nil
},
})
if err := r.Record(r); err != nil {
return err
}
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your configuration was successful."))
2016-11-01 09:56:12 +00:00
return nil
2016-07-23 22:49:19 +00:00
},
Before: before,
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"},
Description: "Remove a project from a realize configuration.",
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
},
2016-07-27 09:14:32 +00:00
Action: func(p *cli.Context) error {
if err := r.Blueprint.Remove(p); err != nil {
return err
}
if err := r.Record(r); err != nil {
return err
}
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your project was successfully removed."))
2016-11-01 09:56:12 +00:00
return nil
2016-07-26 17:04:13 +00:00
},
Before: before,
2016-07-26 17:04:13 +00:00
},
{
2017-04-13 13:53:58 +00:00
Name: "list",
Category: "Configuration",
Aliases: []string{"l"},
Description: "Print projects list.",
2016-07-27 09:14:32 +00:00
Action: func(p *cli.Context) error {
return r.Blueprint.List()
2016-07-27 11:42:25 +00:00
},
Before: before,
2016-07-26 17:04:13 +00:00
},
{
2017-04-13 13:53:58 +00:00
Name: "clean",
Category: "Configuration",
Aliases: []string{"c"},
Description: "Remove realize folder.",
Action: func(p *cli.Context) error {
if err := r.Settings.Remove(); err != nil {
return err
}
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Realize folder successfully removed."))
return nil
},
Before: before,
},
2016-07-12 18:03:22 +00:00
},
}
if err := app.Run(os.Args); err != nil {
fmt.Println(style.Red.Bold(err))
os.Exit(1)
}
2016-08-17 23:35:37 +00:00
}