realize/realize.go

1113 lines
34 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"
"github.com/fatih/color"
"github.com/tockins/interact"
"gopkg.in/urfave/cli.v2"
"log"
2017-09-18 01:05:00 +00:00
"os"
2017-09-03 17:59:56 +00:00
"strconv"
2017-09-18 01:05:00 +00:00
"time"
2016-07-12 08:18:02 +00:00
)
2016-11-01 09:56:12 +00:00
const (
2017-09-03 17:59:56 +00:00
version = "1.4.1"
2016-11-01 09:56:12 +00:00
)
// New realize instance
var r realize
// Log struct
type logWriter struct{}
2017-08-27 11:45:06 +00:00
// Realize struct contains the general app informations
type realize struct {
Settings Settings `yaml:"settings"`
Server Server `yaml:"server"`
Schema []Project `yaml:"schema"`
sync chan string
2017-08-27 11:45:06 +00:00
}
2016-11-01 09:56:12 +00:00
2017-08-27 11:45:06 +00:00
// Cli commands
func main() {
app := &cli.App{
2017-04-13 13:53:58 +00:00
Name: "Realize",
2017-08-27 11:45:06 +00:00
Version: version,
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{
{
Name: "start",
2017-04-13 13:53:58 +00:00
Aliases: []string{"r"},
Description: "Start 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."},
2017-09-17 21:37:39 +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."},
&cli.BoolFlag{Name: "server", Aliases: []string{"s"}, Value: false, Usage: "Enable server and open into the default browser."},
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."},
&cli.BoolFlag{Name: "run", Aliases: []string{"nr"}, Value: false, Usage: "Enable go run"},
&cli.BoolFlag{Name: "config", Aliases: []string{"nc"}, Value: false, Usage: "Use a config if exist or save a new one"},
2016-08-21 18:15:01 +00:00
},
2016-07-27 09:14:32 +00:00
Action: func(p *cli.Context) error {
if err := r.insert(p); err != nil {
2017-08-27 11:45:06 +00:00
return err
}
if p.Bool("config") {
if err := r.Settings.record(r); err != nil {
2017-09-17 22:15:21 +00:00
return err
}
}
if err := r.Server.start(p); err != nil {
return err
}
if err := r.run(p); 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: "server", Aliases: []string{"s"}, Value: false, Usage: "Enable server and open into the default browser."},
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"},
&cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: false, Usage: "Enable go run"},
},
Action: func(p *cli.Context) error {
if err := r.add(p); err != nil {
return err
}
if err := r.Settings.record(r); err != nil {
return err
}
fmt.Fprintln(output, prefix(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(red.bold("INVALID INPUT"))
context.SetPrfx(color.Output, yellow.bold("[")+"REALIZE"+yellow.bold("]"))
return nil
},
Questions: []*interact.Question{
{
Before: func(d interact.Context) error {
if _, err := os.Stat(Directory + "/" + File); err != nil {
2017-03-19 23:19:05 +00:00
d.Skip()
}
d.SetDef(false, green.regular("(n)"))
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
Msg: "Would you want to overwrite the existing " + 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{
file: File,
2017-04-01 17:33:21 +00:00
}
r.Server = Server{
Status: false,
Open: false,
Host: host,
Port: port,
}
r.Schema = r.Schema[len(r.Schema):]
}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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, green.regular("(os default)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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.Settings.FileLimit = val
2017-09-03 17:59:56 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
2017-09-03 17:59:56 +00:00
Msg: "Enable logging files",
},
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.Settings.Files.Errors = Resource{Name: FileErr, Status: val}
r.Settings.Files.Outputs = Resource{Name: FileOut, Status: val}
r.Settings.Files.Logs = Resource{Name: FileLog, Status: val}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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(port, green.regular("("+strconv.Itoa(port)+")"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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(host, green.regular("("+host+")"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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, green.regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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, 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: yellow.regular("[y/n]"),
Msg: "Would you want to " + 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.add(p)
2017-03-19 23:19:05 +00:00
}
return val
},
},
Subs: []*interact.Question{
2017-03-19 23:19:05 +00:00
{
Before: func(d interact.Context) error {
d.SetDef(r.Settings.wdir(), green.regular("("+r.Settings.wdir()+")"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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()
}
r.Schema[len(r.Schema)-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, green.regular("("+dir+")"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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()
}
r.Schema[len(r.Schema)-1].Path = r.Settings.path(val)
return nil
},
},
2017-09-03 17:59:56 +00:00
{
Before: func(d interact.Context) error {
d.SetDef(true, green.regular("(y)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
2017-09-03 17:59:56 +00:00
Msg: "Enable go vet",
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetDef("", green.regular("(none)"))
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
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[len(r.Schema)-1].Cmds.Vet.Args = append(r.Schema[len(r.Schema)-1].Cmds.Vet.Args, 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 {
2017-03-19 23:19:05 +00:00
return d.Err()
}
r.Schema[len(r.Schema)-1].Cmds.Vet.Status = val
2017-04-16 10:02:47 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(true, green.regular("(y)"))
2017-04-16 10:02:47 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
2017-09-03 17:59:56 +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 {
d.SetDef("", green.regular("(none)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Cmds.Fmt.Args = append(r.Schema[len(r.Schema)-1].Cmds.Fmt.Args, val)
2017-09-03 17:59:56 +00:00
}
return nil
},
},
2017-04-16 10:02:47 +00:00
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema[len(r.Schema)-1].Cmds.Fmt.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(true, green.regular("(y)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
Msg: "Enable go test",
2017-09-03 17:59:56 +00:00
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetDef("", green.regular("(none)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Cmds.Test.Args = append(r.Schema[len(r.Schema)-1].Cmds.Test.Args, val)
2017-09-03 17:59:56 +00:00
}
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.Schema[len(r.Schema)-1].Cmds.Test.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
Msg: "Enable go generate",
2017-09-03 17:59:56 +00:00
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetDef("", green.regular("(none)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Cmds.Generate.Args = append(r.Schema[len(r.Schema)-1].Cmds.Generate.Args, val)
2017-09-03 17:59:56 +00:00
}
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.Schema[len(r.Schema)-1].Cmds.Generate.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(true, green.regular("(y)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
Msg: "Enable go install",
2017-09-03 17:59:56 +00:00
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetDef("", green.regular("(none)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Cmds.Install.Args = append(r.Schema[len(r.Schema)-1].Cmds.Install.Args, val)
2017-09-03 17:59:56 +00:00
}
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.Schema[len(r.Schema)-1].Cmds.Install.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
Msg: "Enable go build",
2017-09-03 17:59:56 +00:00
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetDef("", green.regular("(none)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Cmds.Build.Args = append(r.Schema[len(r.Schema)-1].Cmds.Build.Args, val)
2017-09-03 17:59:56 +00:00
}
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.Schema[len(r.Schema)-1].Cmds.Build.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(true, green.regular("(y)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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()
}
r.Schema[len(r.Schema)-1].Cmds.Run = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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 {
r.Schema[len(r.Schema)-1].Watcher.Paths = r.Schema[len(r.Schema)-1].Watcher.Paths[:len(r.Schema[len(r.Schema)-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: 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()
}
r.Schema[len(r.Schema)-1].Watcher.Paths = append(r.Schema[len(r.Schema)-1].Watcher.Paths, 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, green.regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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 {
r.Schema[len(r.Schema)-1].Watcher.Ignore = r.Schema[len(r.Schema)-1].Watcher.Ignore[:len(r.Schema[len(r.Schema)-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: 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()
}
r.Schema[len(r.Schema)-1].Watcher.Ignore = append(r.Schema[len(r.Schema)-1].Watcher.Ignore, 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, green.regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
2017-09-03 17:59:56 +00:00
Msg: "Add an additional argument",
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: yellow.regular("[string]"),
2017-09-03 17:59:56 +00:00
Msg: "Add another 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()
}
r.Schema[len(r.Schema)-1].Args = append(r.Schema[len(r.Schema)-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, green.regular("(none)"))
2017-09-03 17:59:56 +00:00
d.SetEnd("!")
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
2017-09-03 17:59:56 +00:00
Msg: "Add a 'before' custom command (insert '!' to stop)",
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
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
2017-09-03 17:59:56 +00:00
Msg: "Insert a command",
},
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()
}
r.Schema[len(r.Schema)-1].Watcher.Scripts = append(r.Schema[len(r.Schema)-1].Watcher.Scripts, Command{Type: "before", Command: val})
2017-09-03 17:59:56 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef("", green.regular("(n)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Watcher.Scripts[len(r.Schema[len(r.Schema)-1].Watcher.Scripts)-1].Path = val
2017-09-03 17:59:56 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Watcher.Scripts[len(r.Schema[len(r.Schema)-1].Watcher.Scripts)-1].Global = val
2017-09-03 17:59:56 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Watcher.Scripts[len(r.Schema[len(r.Schema)-1].Watcher.Scripts)-1].Output = val
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-09-03 17:59:56 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-09-03 17:59:56 +00:00
if val {
d.Reload()
}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(none)"))
2017-09-03 17:59:56 +00:00
d.SetEnd("!")
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
2017-09-03 17:59:56 +00:00
Msg: "Add an 'after' custom commands (insert '!' to stop)",
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
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
2017-09-03 17:59:56 +00:00
Msg: "Insert a command",
},
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()
}
r.Schema[len(r.Schema)-1].Watcher.Scripts = append(r.Schema[len(r.Schema)-1].Watcher.Scripts, Command{Type: "after", Command: val})
2017-09-03 17:59:56 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef("", green.regular("(n)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Watcher.Scripts[len(r.Schema[len(r.Schema)-1].Watcher.Scripts)-1].Path = val
2017-09-03 17:59:56 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Watcher.Scripts[len(r.Schema[len(r.Schema)-1].Watcher.Scripts)-1].Global = val
2017-09-03 17:59:56 +00:00
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-09-03 17:59:56 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
2017-09-03 17:59:56 +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[len(r.Schema)-1].Watcher.Scripts[len(r.Schema[len(r.Schema)-1].Watcher.Scripts)-1].Output = val
return nil
},
},
},
Action: func(d interact.Context) interface{} {
2017-09-03 17:59:56 +00:00
val, err := d.Ans().Bool()
if err != nil {
2017-03-19 23:19:05 +00:00
return d.Err()
}
2017-09-03 17:59:56 +00:00
if val {
d.Reload()
}
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
2017-03-19 23:19:05 +00:00
return nil
},
Quest: interact.Quest{
Options: 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()
}
r.Schema[len(r.Schema)-1].Watcher.Preview = val
return nil
},
},
2017-06-16 09:48:25 +00:00
{
Before: func(d interact.Context) error {
d.SetDef("", green.regular("(none)"))
2017-06-16 09:48:25 +00:00
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
2017-06-16 09:48:25 +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[len(r.Schema)-1].ErrorOutputPattern = val
2017-06-16 09:48:25 +00:00
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.del(Directory)
if actErr != nil {
return actErr
2017-03-19 23:19:05 +00:00
}
}
return nil
},
})
if err := r.Settings.record(r); err != nil {
return err
}
fmt.Fprintln(output, prefix(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.remove(p); err != nil {
return err
}
if err := r.Settings.record(r); err != nil {
return err
}
fmt.Fprintln(output, prefix(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: "clean",
Category: "Configuration",
Aliases: []string{"c"},
Description: "Remove realize folder.",
Action: func(p *cli.Context) error {
if err := r.Settings.del(Directory); err != nil {
return err
}
fmt.Fprintln(output, prefix(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 {
print(red.bold(err))
os.Exit(1)
}
2016-08-17 23:35:37 +00:00
}
2017-08-27 11:45:06 +00:00
// Prefix a given string
func prefix(s string) string {
if s != "" {
return fmt.Sprint(yellow.bold("["), "REALIZE", yellow.bold("]"), s)
2017-08-27 11:45:06 +00:00
}
return ""
}
// Before is launched before each command
func before(*cli.Context) error {
// custom log
log.SetFlags(0)
log.SetOutput(new(logWriter))
2017-08-27 11:45:06 +00:00
// Before of every exec of a cli method
gopath := os.Getenv("GOPATH")
if gopath == "" {
return errors.New("$GOPATH isn't set properly")
}
r = realize{
sync: make(chan string),
Settings: Settings{
file: File,
Legacy: Legacy{
Interval: 100 * time.Millisecond,
2017-08-27 11:45:06 +00:00
},
},
Server: Server{
parent: &r,
Status: false,
Open: false,
Host: host,
Port: port,
},
2017-08-27 11:45:06 +00:00
}
// read if exist
r.Settings.read(&r)
2017-08-27 11:45:06 +00:00
// increase the file limit
if r.Settings.FileLimit != 0 {
if err := r.Settings.flimit(); err != nil {
2017-08-27 11:45:06 +00:00
return err
}
}
return nil
}
// Rewrite the layout of the log timestamp
func (w logWriter) Write(bytes []byte) (int, error) {
return fmt.Fprint(output, yellow.regular("["), time.Now().Format("15:04:05"), yellow.regular("]")+string(bytes))
2017-08-27 11:45:06 +00:00
}