realize/realize.go

1243 lines
39 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"
2017-10-24 13:10:54 +00:00
"go/build"
"gopkg.in/urfave/cli.v2"
"log"
2017-09-18 01:05:00 +00:00
"os"
2017-10-22 16:44:44 +00:00
"path/filepath"
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-11-12 10:16:45 +00:00
version = "1.5.2"
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 {
2017-10-16 13:22:10 +00:00
Settings Settings `yaml:"settings" json:"settings"`
Server Server `yaml:"server" json:"server"`
Schema []Project `yaml:"schema" json:"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-11-12 21:42:21 +00:00
Name: "Realize",
Version: version,
2017-10-12 14:27:55 +00:00
Description: "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-11-13 21:55:11 +00:00
Aliases: []string{"s"},
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{
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"},
&cli.BoolFlag{Name: "server", Aliases: []string{"s"}, Value: false, Usage: "Enable server and open into the default browser"},
&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
},
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
}
2017-10-31 12:28:21 +00:00
if !p.Bool("no-config") && p.String("name") == "" {
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
}
2017-10-17 09:56:19 +00:00
return r.run(p)
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"},
2017-10-12 14:27:55 +00:00
Description: "Add a project to an existing config file or create a new one",
Flags: []cli.Flag{
2017-10-22 16:44:44 +00:00
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: 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"},
},
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
}
2017-10-22 16:44:44 +00:00
log.Println(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",
2017-10-23 12:32:42 +00:00
Aliases: []string{"i"},
2017-04-13 13:53:58 +00:00
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"))
2017-10-31 12:28:21 +00:00
context.SetPrfx(color.Output, yellow.regular("[")+time.Now().Format("15:04:05")+yellow.regular("]")+yellow.bold("[")+"REALIZE"+yellow.bold("]"))
return nil
},
Questions: []*interact.Question{
{
Before: func(d interact.Context) error {
2017-10-12 14:27:55 +00:00
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]"),
2017-10-12 14:27:55 +00:00
Msg: "Would you want to overwrite 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 {
2017-10-12 14:27:55 +00:00
r = new()
}
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-10-12 14:27:55 +00:00
Msg: "Would you want to customize 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]"),
2017-10-12 14:27:55 +00:00
Msg: "Set 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()
}
2017-11-12 21:42:21 +00:00
r.Settings.FileLimit = int32(val)
2017-09-03 17:59:56 +00:00
return nil
},
},
2017-10-12 14:27:55 +00:00
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
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 {
d.SetDef(100, green.regular("(100ms)"))
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[int]"),
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
},
},
2017-09-03 17:59:56 +00:00
{
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()
}
2017-10-12 14:27:55 +00:00
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-10-12 14:27:55 +00:00
Msg: "Open in current browser",
2017-03-19 23:19:05 +00:00
},
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 {
2017-10-22 21:22:45 +00:00
d.SetDef(wdir(), green.regular("("+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-10-22 16:44:44 +00:00
dir := wdir()
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()
}
2017-10-22 16:44:44 +00:00
r.Schema[len(r.Schema)-1].Path = filepath.Clean(val)
return nil
},
},
2017-09-03 17:59:56 +00:00
{
Before: func(d interact.Context) error {
2017-10-16 13:22:10 +00:00
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: "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 {
2017-10-16 13:22:10 +00:00
d.SetDef(false, green.regular("(n)"))
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 {
2017-10-16 13:22:10 +00:00
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 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
},
},
2017-11-05 21:53:31 +00:00
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
Msg: "Enable go fix",
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)"))
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
Msg: "Fix 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.Fix.Args = append(r.Schema[len(r.Schema)-1].Cmds.Fix.Args, val)
}
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema[len(r.Schema)-1].Cmds.Fix.Status = val
return nil
},
},
{
Before: func(d interact.Context) error {
d.SetDef(false, green.regular("(n)"))
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[y/n]"),
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 {
d.SetDef("", green.regular("(none)"))
return nil
},
Quest: interact.Quest{
Options: yellow.regular("[string]"),
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[len(r.Schema)-1].Cmds.Clean.Args = append(r.Schema[len(r.Schema)-1].Cmds.Clean.Args, val)
}
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema[len(r.Schema)-1].Cmds.Clean.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-10-12 14:27:55 +00:00
Msg: "Customize watching 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-10-12 14:27:55 +00:00
Msg: "Customize ignore 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]"),
2017-10-12 14:27:55 +00:00
Msg: "Print watched files on startup",
},
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 {
2017-10-12 14:27:55 +00:00
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
}
2017-10-22 16:44:44 +00:00
log.Println(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"},
2017-10-12 14:27:55 +00:00
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
}
2017-10-22 16:44:44 +00:00
log.Println(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"},
2017-10-12 14:27:55 +00:00
Description: "Remove realize folder",
Action: func(p *cli.Context) error {
2017-10-12 14:27:55 +00:00
if err := r.Settings.del(directory); err != nil {
return err
}
2017-10-22 16:44:44 +00:00
log.Println(prefix(green.bold("Realize folder successfully removed")))
return nil
},
Before: before,
},
2017-10-23 12:32:42 +00:00
{
Name: "version",
Aliases: []string{"v"},
Description: "Realize version",
Action: func(p *cli.Context) error {
log.Println(prefix(green.bold(version)))
return nil
},
Before: before,
},
2016-07-12 18:03:22 +00:00
},
}
if err := app.Run(os.Args); err != nil {
2017-10-22 16:44:44 +00:00
log.Println(prefix(red.bold(err)))
os.Exit(1)
}
2016-08-17 23:35:37 +00:00
}
2017-08-27 11:45:06 +00:00
2017-10-12 14:27:55 +00:00
// New return default realize config
func new() realize {
return realize{
sync: make(chan string),
Settings: Settings{
file: file,
Legacy: Legacy{
Interval: 100 * time.Millisecond,
},
},
Server: Server{
parent: &r,
Status: false,
Open: false,
Host: host,
Port: port,
},
}
}
2017-08-27 11:45:06 +00:00
// Prefix a given string
func prefix(s string) string {
if s != "" {
2017-10-22 16:44:44 +00:00
return fmt.Sprint(yellow.bold("["), "REALIZE", yellow.bold("]"), " : ", s)
2017-08-27 11:45:06 +00:00
}
2017-11-12 10:16:45 +00:00
return s
2017-08-27 11:45:06 +00:00
}
// Before is launched before each command
2017-11-12 10:16:45 +00:00
func before(*cli.Context) (err error) {
// custom log
log.SetFlags(0)
2017-10-12 14:27:55 +00:00
log.SetOutput(logWriter{})
2017-08-27 11:45:06 +00:00
// Before of every exec of a cli method
2017-10-24 13:10:54 +00:00
gopath := build.Default.GOPATH
2017-08-27 11:45:06 +00:00
if gopath == "" {
return errors.New("$GOPATH isn't set properly")
}
2017-11-12 10:16:45 +00:00
if err = os.Setenv("GOPATH", gopath); err != nil {
2017-10-24 13:10:54 +00:00
return err
}
2017-10-12 14:27:55 +00:00
// new realize instance
r = new()
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 {
2017-11-12 10:16:45 +00:00
if err = r.Settings.flimit(); err != nil {
2017-08-27 11:45:06 +00:00
return err
}
}
2017-11-12 10:16:45 +00:00
return
2017-08-27 11:45:06 +00:00
}
// Rewrite the layout of the log timestamp
func (w logWriter) Write(bytes []byte) (int, error) {
2017-10-22 16:44:44 +00:00
return fmt.Fprint(output, yellow.regular("["), time.Now().Format("15:04:05"), yellow.regular("]"), string(bytes))
2017-08-27 11:45:06 +00:00
}