From 09534473bb2a6ea65cc90b6f42b39b7d528c4711 Mon Sep 17 00:00:00 2001 From: alessio Date: Sat, 15 Apr 2017 10:50:49 +0200 Subject: [PATCH 1/8] #47 fixed --- watcher/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/watcher/utils.go b/watcher/utils.go index 659feb4..2e07d57 100644 --- a/watcher/utils.go +++ b/watcher/utils.go @@ -23,7 +23,7 @@ func argsParam(params *cli.Context) []string { // Duplicates check projects with same name or same combinations of main/path func duplicates(value Project, arr []Project) (Project, error) { for _, val := range arr { - if value.Path == val.Path { + if value.Path == val.Path && val.Name == value.Name { return val, errors.New("There is already a project for '" + val.Path + "'. Check your config file!") } } From ca9ec15e70df7501b72d6bdbb632f756b3fc7938 Mon Sep 17 00:00:00 2001 From: Kyoichiro Yamada Date: Thu, 13 Apr 2017 21:48:48 +0900 Subject: [PATCH 2/8] refactor: package name should NOT be abbreviated --- realize.go | 289 ++++++++++++++++++++++--------------------- server/main.go | 19 +-- settings/settings.go | 3 +- watcher/cmd.go | 5 +- watcher/exec.go | 2 +- watcher/main.go | 47 +++---- watcher/utils.go | 5 +- watcher/watcher.go | 5 +- 8 files changed, 191 insertions(+), 184 deletions(-) diff --git a/realize.go b/realize.go index e3c0444..16d3e97 100644 --- a/realize.go +++ b/realize.go @@ -3,14 +3,15 @@ package main import ( "errors" "fmt" - "github.com/fatih/color" - i "github.com/tockins/interact" - s "github.com/tockins/realize/server" - c "github.com/tockins/realize/settings" - w "github.com/tockins/realize/watcher" - "gopkg.in/urfave/cli.v2" "os" "time" + + "github.com/fatih/color" + "github.com/tockins/interact" + "github.com/tockins/realize/server" + "github.com/tockins/realize/settings" + "github.com/tockins/realize/watcher" + cli "gopkg.in/urfave/cli.v2" ) const ( @@ -30,12 +31,12 @@ var r realize // Realize struct contains the general app informations type realize struct { - c.Settings `yaml:"settings,omitempty"` - Name, Description, Author, Email, Host, Version string `yaml:"-"` - Sync chan string `yaml:"-"` - Blueprint w.Blueprint `yaml:"-"` - Server s.Server `yaml:"-"` - Projects *[]w.Project `yaml:"projects" json:"projects"` + settings.Settings `yaml:"settings,omitempty"` + Name, Description, Author, Email, Host, Version string `yaml:"-"` + Sync chan string `yaml:"-"` + Blueprint watcher.Blueprint `yaml:"-"` + Server server.Server `yaml:"-"` + Projects *[]watcher.Project `yaml:"projects" json:"projects"` } // Realize struct initialization @@ -45,17 +46,17 @@ func init() { Version: version, Description: description, Sync: make(chan string), - Settings: c.Settings{ - Config: c.Config{ + Settings: settings.Settings{ + Config: settings.Config{ Create: true, }, - Resources: c.Resources{ + Resources: settings.Resources{ Config: config, Outputs: outputs, Logs: logs, Errors: errs, }, - Server: c.Server{ + Server: settings.Server{ Status: false, Open: false, Host: host, @@ -63,11 +64,11 @@ func init() { }, }, } - r.Blueprint = w.Blueprint{ + r.Blueprint = watcher.Blueprint{ Settings: &r.Settings, Sync: r.Sync, } - r.Server = s.Server{ + r.Server = server.Server{ Blueprint: &r.Blueprint, Settings: &r.Settings, Sync: r.Sync, @@ -135,7 +136,7 @@ func main() { }, Action: func(p *cli.Context) error { if p.Bool("legacy") { - r.Config.Legacy = c.Legacy{ + r.Config.Legacy = settings.Legacy{ Status: p.Bool("legacy"), Interval: interval, } @@ -144,7 +145,7 @@ func main() { if p.Bool("no-config") { r.Config.Create = false } - r.Blueprint.Projects = []w.Project{} + r.Blueprint.Projects = []watcher.Project{} handle(r.Blueprint.Add(p)) } handle(r.Server.Start(p)) @@ -190,41 +191,41 @@ func main() { Aliases: []string{"a"}, Usage: "Define a new config file with all options step by step", Action: func(p *cli.Context) (err error) { - i.Run(&i.Interact{ - Before: func(context i.Context) error { + interact.Run(&interact.Interact{ + Before: func(context interact.Context) error { context.SetErr(r.Red.Bold("INVALID INPUT")) context.SetPrfx(color.Output, r.Yellow.Bold("[")+"REALIZE"+r.Yellow.Bold("]")) return nil }, - Questions: []*i.Question{ + Questions: []*interact.Question{ { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { if _, err := os.Stat(".realize/" + config); err != nil { d.Skip() } d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Would you want to overwrite the existing " + r.Colors.Magenta.Bold("Realize") + " config?", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() } else if val { - r.Settings = c.Settings{ - Config: c.Config{ + r.Settings = settings.Settings{ + Config: settings.Config{ Create: true, }, - Resources: c.Resources{ + Resources: settings.Resources{ Config: config, Outputs: outputs, Logs: logs, Errors: errs, }, - Server: c.Server{ + Server: settings.Server{ Status: false, Open: false, Host: host, @@ -237,29 +238,29 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Would you want to customize the " + r.Colors.Magenta.Bold("settings") + "?", - Resolve: func(d i.Context) bool { + Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() return val }, }, - Subs: []*i.Question{ + Subs: []*interact.Question{ { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(0, r.Green.Regular("(os default)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[int]"), Msg: "Max number of open files (root required)", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Int() if err != nil { return d.Err() @@ -269,29 +270,29 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable legacy watch by polling", - Resolve: func(d i.Context) bool { + Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() return val }, }, - Subs: []*i.Question{ + Subs: []*interact.Question{ { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(1, r.Green.Regular("(1s)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[seconds]"), Msg: "Set polling interval in seconds", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Int() if err != nil { return d.Err() @@ -301,7 +302,7 @@ func main() { }, }, }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -311,29 +312,29 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable web server", - Resolve: func(d i.Context) bool { + Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() return val }, }, - Subs: []*i.Question{ + Subs: []*interact.Question{ { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(5001, r.Green.Regular("(5001)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[int]"), Msg: "Server port", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Int() if err != nil { return d.Err() @@ -343,15 +344,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef("localhost", r.Green.Regular("(localhost)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[string]"), Msg: "Server host", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().String() if err != nil { return d.Err() @@ -361,15 +362,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Open in the current browser", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -379,7 +380,7 @@ func main() { }, }, }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -389,7 +390,7 @@ func main() { }, }, }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { _, err := d.Ans().Bool() if err != nil { return d.Err() @@ -398,15 +399,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(true, r.Green.Regular("(y)")) d.SetEnd("!") return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Would you want to " + r.Colors.Magenta.Regular("add a new project") + "? (insert '!' to stop)", - Resolve: func(d i.Context) bool { + Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() if val { r.Blueprint.Add(p) @@ -414,17 +415,17 @@ func main() { return val }, }, - Subs: []*i.Question{ + Subs: []*interact.Question{ { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(r.Settings.Wdir(), r.Green.Regular("("+r.Settings.Wdir()+")")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[string]"), Msg: "Project name", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().String() if err != nil { return d.Err() @@ -434,16 +435,16 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { dir, _ := os.Getwd() d.SetDef(dir, r.Green.Regular("("+dir+")")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[string]"), Msg: "Project path", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().String() if err != nil { return d.Err() @@ -453,15 +454,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(true, r.Green.Regular("(y)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable go fmt", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -471,15 +472,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable go test", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -489,15 +490,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable go generate", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -507,15 +508,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(true, r.Green.Regular("(y)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable go install", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -525,15 +526,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable go build", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -543,15 +544,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(true, r.Green.Regular("(y)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable go run", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -561,14 +562,14 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Customize the watched paths", - Resolve: func(d i.Context) bool { + Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() if val { 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] @@ -576,17 +577,17 @@ func main() { return val }, }, - Subs: []*i.Question{ + Subs: []*interact.Question{ { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetEnd("!") return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[string]"), Msg: "Insert a path to watch (insert '!' to stop)", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().String() if err != nil { return d.Err() @@ -597,7 +598,7 @@ func main() { }, }, }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { _, err := d.Ans().Bool() if err != nil { return d.Err() @@ -606,14 +607,14 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Customize the ignored paths", - Resolve: func(d i.Context) bool { + Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() if val { 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] @@ -621,17 +622,17 @@ func main() { return val }, }, - Subs: []*i.Question{ + Subs: []*interact.Question{ { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetEnd("!") return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[string]"), Msg: "Insert a path to ignore (insert '!' to stop)", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().String() if err != nil { return d.Err() @@ -642,7 +643,7 @@ func main() { }, }, }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { _, err := d.Ans().Bool() if err != nil { return d.Err() @@ -651,29 +652,29 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Add additionals arguments", - Resolve: func(d i.Context) bool { + Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() return val }, }, - Subs: []*i.Question{ + Subs: []*interact.Question{ { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetEnd("!") return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[string]"), Msg: "Insert an argument (insert '!' to stop)", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().String() if err != nil { return d.Err() @@ -684,7 +685,7 @@ func main() { }, }, }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { _, err := d.Ans().Bool() if err != nil { return d.Err() @@ -693,40 +694,40 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Add 'before' custom commands", - Resolve: func(d i.Context) bool { + Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() return val }, }, - Subs: []*i.Question{ + Subs: []*interact.Question{ { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetEnd("!") return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[string]"), Msg: "Insert a command (insert '!' to stop)", }, - Action: func(d i.Context) interface{} { + 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].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, w.Command{Type: "before", Command: val}) + 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}) d.Reload() return nil }, }, }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { _, err := d.Ans().Bool() if err != nil { return d.Err() @@ -735,40 +736,40 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Add 'after' custom commands", - Resolve: func(d i.Context) bool { + Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() return val }, }, - Subs: []*i.Question{ + Subs: []*interact.Question{ { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetEnd("!") return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[string]"), Msg: "Insert a command (insert '!' to stop)", }, - Action: func(d i.Context) interface{} { + 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].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, w.Command{Type: "after", Command: val}) + 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}) d.Reload() return nil }, }, }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { _, err := d.Ans().Bool() if err != nil { return d.Err() @@ -777,15 +778,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable watcher files preview", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -795,15 +796,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable file output history", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -813,15 +814,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable file logs history", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -831,15 +832,15 @@ func main() { }, }, { - Before: func(d i.Context) error { + Before: func(d interact.Context) error { d.SetDef(false, r.Green.Regular("(n)")) return nil }, - Quest: i.Quest{ + Quest: interact.Quest{ Options: r.Yellow.Regular("[y/n]"), Msg: "Enable file errors history", }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { val, err := d.Ans().Bool() if err != nil { return d.Err() @@ -849,7 +850,7 @@ func main() { }, }, }, - Action: func(d i.Context) interface{} { + Action: func(d interact.Context) interface{} { if val, err := d.Ans().Bool(); err != nil { return d.Err() } else if val { @@ -859,7 +860,7 @@ func main() { }, }, }, - After: func(d i.Context) error { + After: func(d interact.Context) error { if val, _ := d.Qns().Get(0).Ans().Bool(); val { err = r.Settings.Remove() if err != nil { diff --git a/server/main.go b/server/main.go index d40605b..382fb4a 100644 --- a/server/main.go +++ b/server/main.go @@ -2,21 +2,22 @@ package server import ( "encoding/json" - "github.com/labstack/echo" - "github.com/labstack/echo/middleware" - c "github.com/tockins/realize/settings" - w "github.com/tockins/realize/watcher" - "golang.org/x/net/websocket" - "gopkg.in/urfave/cli.v2" "net/http" "strconv" + + "github.com/labstack/echo" + "github.com/labstack/echo/middleware" + "github.com/tockins/realize/settings" + "github.com/tockins/realize/watcher" + "golang.org/x/net/websocket" + "gopkg.in/urfave/cli.v2" ) // Server settings type Server struct { - *c.Settings `yaml:"-"` - *w.Blueprint `yaml:"-"` - Sync chan string `yaml:"-"` + *settings.Settings `yaml:"-"` + *watcher.Blueprint `yaml:"-"` + Sync chan string `yaml:"-"` } // Render return a web pages defined in bindata diff --git a/settings/settings.go b/settings/settings.go index 733b974..098a40d 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -1,9 +1,10 @@ package settings import ( - "gopkg.in/yaml.v2" "os" "time" + + yaml "gopkg.in/yaml.v2" ) // Settings defines a group of general settings diff --git a/watcher/cmd.go b/watcher/cmd.go index 450b3d6..b168a0d 100644 --- a/watcher/cmd.go +++ b/watcher/cmd.go @@ -1,10 +1,11 @@ -package cli +package watcher import ( "errors" "fmt" - "gopkg.in/urfave/cli.v2" "strings" + + cli "gopkg.in/urfave/cli.v2" ) // Run launches the toolchain for each project diff --git a/watcher/exec.go b/watcher/exec.go index d197576..c26987d 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -1,4 +1,4 @@ -package cli +package watcher import ( "bufio" diff --git a/watcher/main.go b/watcher/main.go index ba5c367..5c3a582 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -1,10 +1,11 @@ -package cli +package watcher import ( - c "github.com/tockins/realize/settings" "log" "sync" "time" + + "github.com/tockins/realize/settings" ) var wg sync.WaitGroup @@ -21,35 +22,35 @@ type pollWatcher struct { // Log struct type logWriter struct { - c.Colors + settings.Colors } // Blueprint struct contains a projects list type Blueprint struct { - *c.Settings `yaml:"-"` - Projects []Project `yaml:"projects,omitempty" json:"projects,omitempty"` - Sync chan string `yaml:"-"` + *settings.Settings `yaml:"-"` + Projects []Project `yaml:"projects,omitempty" json:"projects,omitempty"` + Sync chan string `yaml:"-"` } // Project defines the informations of a single project type Project struct { - c.Settings `yaml:"-"` - LastChangedOn time.Time `yaml:"-" json:"-"` - base string - Name string `yaml:"name" json:"name"` - Path string `yaml:"path" json:"path"` - Fmt bool `yaml:"fmt" json:"fmt"` - Generate bool `yaml:"generate" json:"generate"` - Test bool `yaml:"test" json:"test"` - Bin bool `yaml:"bin" json:"bin"` - Build bool `yaml:"build" json:"build"` - Run bool `yaml:"run" json:"run"` - Params []string `yaml:"params,omitempty" json:"params,omitempty"` - Watcher Watcher `yaml:"watcher" json:"watcher"` - Streams Streams `yaml:"streams" json:"streams"` - Buffer Buffer `yaml:"-" json:"buffer"` - parent *Blueprint - path string + settings.Settings `yaml:"-"` + LastChangedOn time.Time `yaml:"-" json:"-"` + base string + Name string `yaml:"name" json:"name"` + Path string `yaml:"path" json:"path"` + Fmt bool `yaml:"fmt" json:"fmt"` + Generate bool `yaml:"generate" json:"generate"` + Test bool `yaml:"test" json:"test"` + Bin bool `yaml:"bin" json:"bin"` + Build bool `yaml:"build" json:"build"` + Run bool `yaml:"run" json:"run"` + Params []string `yaml:"params,omitempty" json:"params,omitempty"` + Watcher Watcher `yaml:"watcher" json:"watcher"` + Streams Streams `yaml:"streams" json:"streams"` + Buffer Buffer `yaml:"-" json:"buffer"` + parent *Blueprint + path string } // Watcher struct defines the livereload's logic diff --git a/watcher/utils.go b/watcher/utils.go index 2e07d57..b88e2b0 100644 --- a/watcher/utils.go +++ b/watcher/utils.go @@ -1,10 +1,11 @@ -package cli +package watcher import ( "errors" "fmt" - "gopkg.in/urfave/cli.v2" "time" + + cli "gopkg.in/urfave/cli.v2" ) // Argsparam parse one by one the given argumentes diff --git a/watcher/watcher.go b/watcher/watcher.go index f725c3f..b20f550 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -1,9 +1,8 @@ -package cli +package watcher import ( "errors" "fmt" - "github.com/fsnotify/fsnotify" "log" "math/big" "os" @@ -14,6 +13,8 @@ import ( "sync" "syscall" "time" + + "github.com/fsnotify/fsnotify" ) var msg string From 104e4698eb58dca3e88c47e8baeabd425a92d27c Mon Sep 17 00:00:00 2001 From: Kyoichiro Yamada Date: Thu, 13 Apr 2017 21:50:35 +0900 Subject: [PATCH 3/8] fix lints: error strings should not be capitalized or end with punctuation or a newline --- server/open.go | 7 ++++--- watcher/cmd.go | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/server/open.go b/server/open.go index 22ba233..25a436c 100644 --- a/server/open.go +++ b/server/open.go @@ -2,7 +2,7 @@ package server import ( "bytes" - "errors" + "fmt" "io" "os/exec" "runtime" @@ -22,9 +22,10 @@ func init() { // Open a url in the default browser func Open(url string) (io.Writer, error) { - open, err := cmd[runtime.GOOS] + goos := runtime.GOOS + open, err := cmd[goos] if !err { - return nil, errors.New("This operating system is not supported.") + return nil, fmt.Errorf("operating system %q is not supported", goos) } cmd := exec.Command(open, url) cmd.Stderr = &stderr diff --git a/watcher/cmd.go b/watcher/cmd.go index b168a0d..46214c7 100644 --- a/watcher/cmd.go +++ b/watcher/cmd.go @@ -81,7 +81,7 @@ func (h *Blueprint) Remove(p *cli.Context) error { return nil } } - return errors.New("No project found.") + return errors.New("no project found") } // List of all the projects @@ -144,5 +144,5 @@ func (h *Blueprint) check() error { h.Clean() return nil } - return errors.New("There are no projects. The config file is empty.") + return errors.New("there are no projects") } From e5ff238e796ee14f808aa6ad830f97b5e3d600d5 Mon Sep 17 00:00:00 2001 From: Kyoichiro Yamada Date: Thu, 13 Apr 2017 21:49:42 +0900 Subject: [PATCH 4/8] fix lints: declaration of "err" shadows declaration at realize.go:193 --- realize.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/realize.go b/realize.go index 16d3e97..c042d05 100644 --- a/realize.go +++ b/realize.go @@ -190,7 +190,7 @@ func main() { Category: "Configuration", Aliases: []string{"a"}, Usage: "Define a new config file with all options step by step", - Action: func(p *cli.Context) (err error) { + Action: func(p *cli.Context) (actErr error) { interact.Run(&interact.Interact{ Before: func(context interact.Context) error { context.SetErr(r.Red.Bold("INVALID INPUT")) @@ -862,9 +862,9 @@ func main() { }, After: func(d interact.Context) error { if val, _ := d.Qns().Get(0).Ans().Bool(); val { - err = r.Settings.Remove() - if err != nil { - return err + actErr = r.Settings.Remove() + if actErr != nil { + return actErr } } return nil From a54593d7f3852a6cda3dc8311f53ba9bbe5adcdd Mon Sep 17 00:00:00 2001 From: Kyoichiro Yamada Date: Thu, 13 Apr 2017 21:54:13 +0900 Subject: [PATCH 5/8] fix lints: declaration of "err" shadows declaration at io.go:11 --- settings/io.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/settings/io.go b/settings/io.go index c479928..e3fc2c3 100644 --- a/settings/io.go +++ b/settings/io.go @@ -9,12 +9,12 @@ import ( // Stream return a byte stream of a given file func (s Settings) Stream(file string) ([]byte, error) { _, err := os.Stat(file) - if err == nil { - content, err := ioutil.ReadFile(file) - s.Validate(err) - return content, err + if err != nil { + return nil, err } - return nil, err + content, err := ioutil.ReadFile(file) + s.Validate(err) + return content, err } // Write a file given a name and a byte stream From 9bc92338caba4f9db967d3987bbf5fd0303def53 Mon Sep 17 00:00:00 2001 From: Kyoichiro Yamada Date: Thu, 13 Apr 2017 22:49:51 +0900 Subject: [PATCH 6/8] refactoring: settings/colors.go -> style/style.go --- realize.go | 139 ++++++++++++++++++++++--------------------- settings/colors.go | 89 --------------------------- settings/settings.go | 1 - settings/utils.go | 4 +- style/style.go | 24 ++++++++ watcher/cmd.go | 49 +++++++-------- watcher/exec.go | 10 ++-- watcher/main.go | 1 - watcher/utils.go | 3 +- watcher/watcher.go | 43 ++++++------- 10 files changed, 152 insertions(+), 211 deletions(-) delete mode 100644 settings/colors.go create mode 100644 style/style.go diff --git a/realize.go b/realize.go index c042d05..7c32580 100644 --- a/realize.go +++ b/realize.go @@ -10,6 +10,7 @@ import ( "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" ) @@ -96,7 +97,7 @@ func before() error { // Handle errors func handle(err error) error { if err != nil { - fmt.Println(r.Red.Bold(err.Error())) + fmt.Println(style.Red.Bold(err.Error())) os.Exit(1) } return nil @@ -178,7 +179,7 @@ func main() { fmt.Println(p.String("path")) handle(r.Blueprint.Add(p)) handle(r.Record(r)) - fmt.Println(r.Yellow.Bold("[")+"REALIZE"+r.Yellow.Bold("]"), r.Green.Bold("Your project was successfully added.")) + fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your project was successfully added.")) return nil }, Before: func(c *cli.Context) error { @@ -193,8 +194,8 @@ func main() { Action: func(p *cli.Context) (actErr error) { interact.Run(&interact.Interact{ Before: func(context interact.Context) error { - context.SetErr(r.Red.Bold("INVALID INPUT")) - context.SetPrfx(color.Output, r.Yellow.Bold("[")+"REALIZE"+r.Yellow.Bold("]")) + context.SetErr(style.Red.Bold("INVALID INPUT")) + context.SetPrfx(color.Output, style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]")) return nil }, Questions: []*interact.Question{ @@ -203,12 +204,12 @@ func main() { if _, err := os.Stat(".realize/" + config); err != nil { d.Skip() } - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), - Msg: "Would you want to overwrite the existing " + r.Colors.Magenta.Bold("Realize") + " config?", + 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{} { val, err := d.Ans().Bool() @@ -239,12 +240,12 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), - Msg: "Would you want to customize the " + r.Colors.Magenta.Bold("settings") + "?", + Options: style.Yellow.Regular("[y/n]"), + Msg: "Would you want to customize the " + ("settings") + "?", Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() return val @@ -253,11 +254,11 @@ func main() { Subs: []*interact.Question{ { Before: func(d interact.Context) error { - d.SetDef(0, r.Green.Regular("(os default)")) + d.SetDef(0, style.Green.Regular("(os default)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[int]"), + Options: style.Yellow.Regular("[int]"), Msg: "Max number of open files (root required)", }, Action: func(d interact.Context) interface{} { @@ -271,11 +272,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable legacy watch by polling", Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() @@ -285,11 +286,11 @@ func main() { Subs: []*interact.Question{ { Before: func(d interact.Context) error { - d.SetDef(1, r.Green.Regular("(1s)")) + d.SetDef(1, style.Green.Regular("(1s)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[seconds]"), + Options: style.Yellow.Regular("[seconds]"), Msg: "Set polling interval in seconds", }, Action: func(d interact.Context) interface{} { @@ -313,11 +314,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable web server", Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() @@ -327,11 +328,11 @@ func main() { Subs: []*interact.Question{ { Before: func(d interact.Context) error { - d.SetDef(5001, r.Green.Regular("(5001)")) + d.SetDef(5001, style.Green.Regular("(5001)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[int]"), + Options: style.Yellow.Regular("[int]"), Msg: "Server port", }, Action: func(d interact.Context) interface{} { @@ -345,11 +346,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef("localhost", r.Green.Regular("(localhost)")) + d.SetDef("localhost", style.Green.Regular("(localhost)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[string]"), + Options: style.Yellow.Regular("[string]"), Msg: "Server host", }, Action: func(d interact.Context) interface{} { @@ -363,11 +364,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Open in the current browser", }, Action: func(d interact.Context) interface{} { @@ -400,13 +401,13 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(true, r.Green.Regular("(y)")) + d.SetDef(true, style.Green.Regular("(y)")) d.SetEnd("!") return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), - Msg: "Would you want to " + r.Colors.Magenta.Regular("add a new project") + "? (insert '!' to stop)", + 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 { val, _ := d.Ans().Bool() if val { @@ -418,11 +419,11 @@ func main() { Subs: []*interact.Question{ { Before: func(d interact.Context) error { - d.SetDef(r.Settings.Wdir(), r.Green.Regular("("+r.Settings.Wdir()+")")) + d.SetDef(r.Settings.Wdir(), style.Green.Regular("("+r.Settings.Wdir()+")")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[string]"), + Options: style.Yellow.Regular("[string]"), Msg: "Project name", }, Action: func(d interact.Context) interface{} { @@ -437,11 +438,11 @@ func main() { { Before: func(d interact.Context) error { dir, _ := os.Getwd() - d.SetDef(dir, r.Green.Regular("("+dir+")")) + d.SetDef(dir, style.Green.Regular("("+dir+")")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[string]"), + Options: style.Yellow.Regular("[string]"), Msg: "Project path", }, Action: func(d interact.Context) interface{} { @@ -455,11 +456,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(true, r.Green.Regular("(y)")) + d.SetDef(true, style.Green.Regular("(y)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable go fmt", }, Action: func(d interact.Context) interface{} { @@ -473,11 +474,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable go test", }, Action: func(d interact.Context) interface{} { @@ -491,11 +492,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable go generate", }, Action: func(d interact.Context) interface{} { @@ -509,11 +510,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(true, r.Green.Regular("(y)")) + d.SetDef(true, style.Green.Regular("(y)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable go install", }, Action: func(d interact.Context) interface{} { @@ -527,11 +528,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable go build", }, Action: func(d interact.Context) interface{} { @@ -545,11 +546,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(true, r.Green.Regular("(y)")) + d.SetDef(true, style.Green.Regular("(y)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable go run", }, Action: func(d interact.Context) interface{} { @@ -563,11 +564,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Customize the watched paths", Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() @@ -584,7 +585,7 @@ func main() { return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[string]"), + Options: style.Yellow.Regular("[string]"), Msg: "Insert a path to watch (insert '!' to stop)", }, Action: func(d interact.Context) interface{} { @@ -608,11 +609,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Customize the ignored paths", Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() @@ -629,7 +630,7 @@ func main() { return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[string]"), + Options: style.Yellow.Regular("[string]"), Msg: "Insert a path to ignore (insert '!' to stop)", }, Action: func(d interact.Context) interface{} { @@ -653,11 +654,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Add additionals arguments", Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() @@ -671,7 +672,7 @@ func main() { return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[string]"), + Options: style.Yellow.Regular("[string]"), Msg: "Insert an argument (insert '!' to stop)", }, Action: func(d interact.Context) interface{} { @@ -695,11 +696,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Add 'before' custom commands", Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() @@ -713,7 +714,7 @@ func main() { return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[string]"), + Options: style.Yellow.Regular("[string]"), Msg: "Insert a command (insert '!' to stop)", }, Action: func(d interact.Context) interface{} { @@ -737,11 +738,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Add 'after' custom commands", Resolve: func(d interact.Context) bool { val, _ := d.Ans().Bool() @@ -755,7 +756,7 @@ func main() { return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[string]"), + Options: style.Yellow.Regular("[string]"), Msg: "Insert a command (insert '!' to stop)", }, Action: func(d interact.Context) interface{} { @@ -779,11 +780,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable watcher files preview", }, Action: func(d interact.Context) interface{} { @@ -797,11 +798,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable file output history", }, Action: func(d interact.Context) interface{} { @@ -815,11 +816,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable file logs history", }, Action: func(d interact.Context) interface{} { @@ -833,11 +834,11 @@ func main() { }, { Before: func(d interact.Context) error { - d.SetDef(false, r.Green.Regular("(n)")) + d.SetDef(false, style.Green.Regular("(n)")) return nil }, Quest: interact.Quest{ - Options: r.Yellow.Regular("[y/n]"), + Options: style.Yellow.Regular("[y/n]"), Msg: "Enable file errors history", }, Action: func(d interact.Context) interface{} { @@ -871,7 +872,7 @@ func main() { }, }) handle(r.Record(r)) - fmt.Println(r.Yellow.Bold("[")+"REALIZE"+r.Yellow.Bold("]"), r.Green.Bold("Your configuration was successful.")) + fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your configuration was successful.")) return nil }, Before: func(c *cli.Context) error { @@ -889,7 +890,7 @@ func main() { Action: func(p *cli.Context) error { handle(r.Blueprint.Remove(p)) handle(r.Record(r)) - fmt.Println(r.Yellow.Bold("[")+"REALIZE"+r.Yellow.Bold("]"), r.Green.Bold("Your project was successfully removed.")) + fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your project was successfully removed.")) return nil }, Before: func(c *cli.Context) error { @@ -915,7 +916,7 @@ func main() { Usage: "Remove realize folder.", Action: func(p *cli.Context) error { handle(r.Settings.Remove()) - fmt.Println(r.Yellow.Bold("[")+"REALIZE"+r.Yellow.Bold("]"), r.Green.Bold("Realize folder successfully removed.")) + fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Realize folder successfully removed.")) return nil }, Before: func(c *cli.Context) error { diff --git a/settings/colors.go b/settings/colors.go deleted file mode 100644 index 7073127..0000000 --- a/settings/colors.go +++ /dev/null @@ -1,89 +0,0 @@ -package settings - -import ( - "github.com/fatih/color" -) - -// Colors allowed -type Colors struct { - Red - Blue - Yellow - Magenta - Green -} - -// Red color -type Red struct{} - -// Blue color -type Blue struct{} - -// Yellow color -type Yellow struct{} - -// Magenta color -type Magenta struct{} - -// Green color -type Green struct{} - -// Regular font in red -func (c Red) Regular(t ...interface{}) string { - r := color.New(color.FgRed).SprintFunc() - return r(t...) -} - -// Bold font in red -func (c Red) Bold(t ...interface{}) string { - r := color.New(color.FgRed, color.Bold).SprintFunc() - return r(t...) -} - -// Regular font in blue -func (c Blue) Regular(t ...interface{}) string { - r := color.New(color.FgBlue).SprintFunc() - return r(t...) -} - -// Bold font in blue -func (c Blue) Bold(t ...interface{}) string { - r := color.New(color.FgBlue, color.Bold).SprintFunc() - return r(t...) -} - -// Regular font in yellow -func (c Yellow) Regular(t ...interface{}) string { - r := color.New(color.FgYellow).SprintFunc() - return r(t...) -} - -// Bold font in red -func (c Yellow) Bold(t ...interface{}) string { - r := color.New(color.FgYellow, color.Bold).SprintFunc() - return r(t...) -} - -// Regular font in magenta -func (c Magenta) Regular(t ...interface{}) string { - r := color.New(color.FgMagenta).SprintFunc() - return r(t...) -} - -// Bold font in magenta -func (c Magenta) Bold(t ...interface{}) string { - r := color.New(color.FgMagenta, color.Bold).SprintFunc() - return r(t...) -} - -// Regular font in green -func (c Green) Regular(t ...interface{}) string { - r := color.New(color.FgGreen).SprintFunc() - return r(t...) -} - -// Bold font in red -func (c Green) Bold(t ...interface{}) string { - r := color.New(color.FgGreen, color.Bold).SprintFunc() - return r(t...) -} diff --git a/settings/settings.go b/settings/settings.go index 098a40d..a7700aa 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -9,7 +9,6 @@ import ( // Settings defines a group of general settings type Settings struct { - Colors `yaml:"-"` Config `yaml:",inline" json:"config"` Resources `yaml:"resources" json:"resources"` Server `yaml:"server,omitempty" json:"server,omitempty"` diff --git a/settings/utils.go b/settings/utils.go index f1af11d..88ee87a 100644 --- a/settings/utils.go +++ b/settings/utils.go @@ -5,6 +5,8 @@ import ( "os" "path/filepath" "strings" + + "github.com/tockins/realize/style" ) // Wdir return the current working directory @@ -25,7 +27,7 @@ func (s Settings) Validate(err error) error { // Fatal prints a fatal error with its additional messages func (s Settings) Fatal(err error, msg ...interface{}) { if len(msg) > 0 && err != nil { - log.Fatalln(s.Red.Regular(msg...), err.Error()) + log.Fatalln(style.Red.Regular(msg...), err.Error()) } else if err != nil { log.Fatalln(err.Error()) } diff --git a/style/style.go b/style/style.go new file mode 100644 index 0000000..32f8181 --- /dev/null +++ b/style/style.go @@ -0,0 +1,24 @@ +package style + +import ( + "github.com/fatih/color" +) + +type colorBase color.Attribute + +func (s colorBase) Regular(a ...interface{}) string { + return color.New(color.Attribute(s)).Sprint(a...) +} + +func (s colorBase) Bold(a ...interface{}) string { + return color.New(color.Attribute(s), color.Bold).Sprint(a...) +} + +// allowed colors +var ( + Red = colorBase(color.FgRed) + Blue = colorBase(color.FgBlue) + Yellow = colorBase(color.FgYellow) + Magenta = colorBase(color.FgMagenta) + Green = colorBase(color.FgGreen) +) diff --git a/watcher/cmd.go b/watcher/cmd.go index 46214c7..5f88012 100644 --- a/watcher/cmd.go +++ b/watcher/cmd.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/tockins/realize/style" cli "gopkg.in/urfave/cli.v2" ) @@ -89,49 +90,49 @@ func (h *Blueprint) List() error { err := h.check() if err == nil { for _, val := range h.Projects { - fmt.Println(h.Blue.Bold("[") + strings.ToUpper(val.Name) + h.Blue.Bold("]")) - name := h.Magenta.Bold("[") + strings.ToUpper(val.Name) + h.Magenta.Bold("]") + fmt.Println(style.Blue.Bold("[") + strings.ToUpper(val.Name) + style.Blue.Bold("]")) + name := style.Magenta.Bold("[") + strings.ToUpper(val.Name) + style.Magenta.Bold("]") - fmt.Println(name, h.Yellow.Regular("Base Path"), ":", h.Magenta.Regular(val.Path)) - fmt.Println(name, h.Yellow.Regular("Fmt"), ":", h.Magenta.Regular(val.Fmt)) - fmt.Println(name, h.Yellow.Regular("Generate"), ":", h.Magenta.Regular(val.Generate)) - fmt.Println(name, h.Yellow.Regular("Test"), ":", h.Magenta.Regular(val.Test)) - fmt.Println(name, h.Yellow.Regular("Install"), ":", h.Magenta.Regular(val.Bin)) - fmt.Println(name, h.Yellow.Regular("Build"), ":", h.Magenta.Regular(val.Build)) - fmt.Println(name, h.Yellow.Regular("Run"), ":", h.Magenta.Regular(val.Run)) + fmt.Println(name, style.Yellow.Regular("Base Path"), ":", style.Magenta.Regular(val.Path)) + fmt.Println(name, style.Yellow.Regular("Fmt"), ":", style.Magenta.Regular(val.Fmt)) + fmt.Println(name, style.Yellow.Regular("Generate"), ":", style.Magenta.Regular(val.Generate)) + fmt.Println(name, style.Yellow.Regular("Test"), ":", style.Magenta.Regular(val.Test)) + fmt.Println(name, style.Yellow.Regular("Install"), ":", style.Magenta.Regular(val.Bin)) + fmt.Println(name, style.Yellow.Regular("Build"), ":", style.Magenta.Regular(val.Build)) + fmt.Println(name, style.Yellow.Regular("Run"), ":", style.Magenta.Regular(val.Run)) if len(val.Params) > 0 { - fmt.Println(name, h.Yellow.Regular("Params"), ":", h.Magenta.Regular(val.Params)) + fmt.Println(name, style.Yellow.Regular("Params"), ":", style.Magenta.Regular(val.Params)) } - fmt.Println(name, h.Yellow.Regular("Watcher"), ":") - fmt.Println(name, "\t", h.Yellow.Regular("Preview"), ":", h.Magenta.Regular(val.Watcher.Preview)) + fmt.Println(name, style.Yellow.Regular("Watcher"), ":") + fmt.Println(name, "\t", style.Yellow.Regular("Preview"), ":", style.Magenta.Regular(val.Watcher.Preview)) if len(val.Watcher.Exts) > 0 { - fmt.Println(name, "\t", h.Yellow.Regular("Extensions"), ":", h.Magenta.Regular(val.Watcher.Exts)) + fmt.Println(name, "\t", style.Yellow.Regular("Extensions"), ":", style.Magenta.Regular(val.Watcher.Exts)) } if len(val.Watcher.Paths) > 0 { - fmt.Println(name, "\t", h.Yellow.Regular("Paths"), ":", h.Magenta.Regular(val.Watcher.Paths)) + fmt.Println(name, "\t", style.Yellow.Regular("Paths"), ":", style.Magenta.Regular(val.Watcher.Paths)) } if len(val.Watcher.Ignore) > 0 { - fmt.Println(name, "\t", h.Yellow.Regular("Ignored paths"), ":", h.Magenta.Regular(val.Watcher.Ignore)) + fmt.Println(name, "\t", style.Yellow.Regular("Ignored paths"), ":", style.Magenta.Regular(val.Watcher.Ignore)) } if len(val.Watcher.Scripts) > 0 { - fmt.Println(name, "\t", h.Yellow.Regular("Scripts"), ":") + fmt.Println(name, "\t", style.Yellow.Regular("Scripts"), ":") for _, v := range val.Watcher.Scripts { if v.Command != "" { - fmt.Println(name, "\t\t", h.Magenta.Regular("-"), h.Yellow.Regular("Command"), ":", h.Magenta.Regular(v.Command)) + fmt.Println(name, "\t\t", style.Magenta.Regular("-"), style.Yellow.Regular("Command"), ":", style.Magenta.Regular(v.Command)) if v.Path != "" { - fmt.Println(name, "\t\t", h.Yellow.Regular("Path"), ":", h.Magenta.Regular(v.Path)) + fmt.Println(name, "\t\t", style.Yellow.Regular("Path"), ":", style.Magenta.Regular(v.Path)) } if v.Type != "" { - fmt.Println(name, "\t\t", h.Yellow.Regular("Type"), ":", h.Magenta.Regular(v.Type)) + fmt.Println(name, "\t\t", style.Yellow.Regular("Type"), ":", style.Magenta.Regular(v.Type)) } } } } - fmt.Println(name, h.Yellow.Regular("Streams"), ":") - fmt.Println(name, "\t", h.Yellow.Regular("Cli Out"), ":", h.Magenta.Regular(val.Streams.CliOut)) - fmt.Println(name, "\t", h.Yellow.Regular("File Out"), ":", h.Magenta.Regular(val.Streams.FileOut)) - fmt.Println(name, "\t", h.Yellow.Regular("File Log"), ":", h.Magenta.Regular(val.Streams.FileLog)) - fmt.Println(name, "\t", h.Yellow.Regular("File Err"), ":", h.Magenta.Regular(val.Streams.FileErr)) + fmt.Println(name, style.Yellow.Regular("Streams"), ":") + fmt.Println(name, "\t", style.Yellow.Regular("Cli Out"), ":", style.Magenta.Regular(val.Streams.CliOut)) + fmt.Println(name, "\t", style.Yellow.Regular("File Out"), ":", style.Magenta.Regular(val.Streams.FileOut)) + fmt.Println(name, "\t", style.Yellow.Regular("File Log"), ":", style.Magenta.Regular(val.Streams.FileLog)) + fmt.Println(name, "\t", style.Yellow.Regular("File Err"), ":", style.Magenta.Regular(val.Streams.FileErr)) } return nil } diff --git a/watcher/exec.go b/watcher/exec.go index c26987d..2797c88 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -11,6 +11,8 @@ import ( "strings" "sync" "time" + + "github.com/tockins/realize/style" ) // GoRun is an implementation of the bin execution @@ -46,7 +48,7 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Failed to stop: " + err.Error()}) p.Fatal(err, "Failed to stop", ":") } - msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Regular("Ended")) + msg := fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Regular("Ended")) out := BufferOut{Time: time.Now(), Text: "Ended", Type: "Go Run"} p.print("log", out, msg, "") wr.Done() @@ -55,11 +57,11 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) stdout, err := build.StdoutPipe() stderr, err := build.StderrPipe() if err != nil { - log.Println(p.Red.Bold(err.Error())) + log.Println(style.Red.Bold(err.Error())) return err } if err := build.Start(); err != nil { - log.Println(p.Red.Bold(err.Error())) + log.Println(style.Red.Bold(err.Error())) return err } close(runner) @@ -70,7 +72,7 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) for output.Scan() { select { default: - msg := fmt.Sprintln(p.pname(p.Name, 3), ":", p.Blue.Regular(output.Text())) + msg := fmt.Sprintln(p.pname(p.Name, 3), ":", style.Blue.Regular(output.Text())) if isError { out := BufferOut{Time: time.Now(), Text: output.Text(), Type: "Go Run"} p.print("error", out, msg, "") diff --git a/watcher/main.go b/watcher/main.go index 5c3a582..2364b4e 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -22,7 +22,6 @@ type pollWatcher struct { // Log struct type logWriter struct { - settings.Colors } // Blueprint struct contains a projects list diff --git a/watcher/utils.go b/watcher/utils.go index b88e2b0..9535a2d 100644 --- a/watcher/utils.go +++ b/watcher/utils.go @@ -5,6 +5,7 @@ import ( "fmt" "time" + "github.com/tockins/realize/style" cli "gopkg.in/urfave/cli.v2" ) @@ -43,5 +44,5 @@ func inArray(str string, list []string) bool { // Rewrite the layout of the log timestamp func (w logWriter) Write(bytes []byte) (int, error) { - return fmt.Print(w.Yellow.Regular("[") + time.Now().Format("15:04:05") + w.Yellow.Regular("]") + string(bytes)) + return fmt.Print(style.Yellow.Regular("[") + time.Now().Format("15:04:05") + style.Yellow.Regular("]") + string(bytes)) } diff --git a/watcher/watcher.go b/watcher/watcher.go index b20f550..b7e16ba 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -15,6 +15,7 @@ import ( "time" "github.com/fsnotify/fsnotify" + "github.com/tockins/realize/style" ) var msg string @@ -72,7 +73,7 @@ func (p *Project) watchByPolling() { } p.LastChangedOn = time.Now().Truncate(time.Second) // repeat the initial cycle - msg = fmt.Sprintln(p.pname(p.Name, 4), ":", p.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), p.Magenta.Bold(file)) + msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file)) out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file} p.print("log", out, msg, "") @@ -89,7 +90,7 @@ func (p *Project) watchByPolling() { base := filepath.Join(p.base, dir) if _, err := os.Stat(base); err == nil { if err := filepath.Walk(base, walk); err != nil { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Regular(err.Error())) + msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Regular(err.Error())) out = BufferOut{Time: time.Now(), Text: err.Error()} p.print("error", out, msg, "") } @@ -148,7 +149,7 @@ func (p *Project) watchByNotify() { } p.LastChangedOn = time.Now().Truncate(time.Second) // repeat the initial cycle - msg = fmt.Sprintln(p.pname(p.Name, 4), ":", p.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), p.Magenta.Bold(file)) + msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file)) out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file} p.print("log", out, msg, "") @@ -161,7 +162,7 @@ func (p *Project) watchByNotify() { } } case err := <-watcher.Errors: - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Regular(err.Error())) + msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Regular(err.Error())) out = BufferOut{Time: time.Now(), Text: err.Error()} p.print("error", out, msg, "") case <-exit: @@ -207,13 +208,13 @@ func (p *Project) watch(watcher watcher) error { base := filepath.Join(p.base, dir) if _, err := os.Stat(base); err == nil { if err := filepath.Walk(base, walk); err != nil { - log.Println(p.Red.Bold(err.Error())) + log.Println(style.Red.Bold(err.Error())) } } else { return errors.New(base + " path doesn't exist") } } - msg = fmt.Sprintln(p.pname(p.Name, 1), ":", p.Blue.Bold("Watching"), p.Magenta.Bold(files), "file/s", p.Magenta.Bold(folders), "folder/s") + msg = fmt.Sprintln(p.pname(p.Name, 1), ":", style.Blue.Bold("Watching"), style.Magenta.Bold(files), "file/s", style.Magenta.Bold(folders), "folder/s") out = BufferOut{Time: time.Now(), Text: "Watching " + strconv.FormatInt(files, 10) + " files/s " + strconv.FormatInt(folders, 10) + " folder/s"} p.print("log", out, msg, "") return nil @@ -226,11 +227,11 @@ func (p *Project) install() error { log.Println(p.pname(p.Name, 1), ":", "Installing..") stream, err := p.goInstall() if err != nil { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Install"), p.Red.Regular(err.Error())) + msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Install"), style.Red.Regular(err.Error())) out = BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Install", Stream: stream} p.print("error", out, msg, stream) } else { - msg = fmt.Sprintln(p.pname(p.Name, 5), ":", p.Green.Regular("Installed")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) + msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Installed")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) out = BufferOut{Time: time.Now(), Text: "Installed after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} p.print("log", out, msg, stream) } @@ -249,7 +250,7 @@ func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { for { select { case <-runner: - msg = fmt.Sprintln(p.pname(p.Name, 5), ":", p.Green.Regular("Has been run")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) + msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Has been run")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) out = BufferOut{Time: time.Now(), Text: "Has been run after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} p.print("log", out, msg, "") return @@ -265,11 +266,11 @@ func (p *Project) build() error { log.Println(p.pname(p.Name, 1), ":", "Building..") stream, err := p.goBuild() if err != nil { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Build"), p.Red.Regular(err.Error())) + msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Build"), style.Red.Regular(err.Error())) out = BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Build", Stream: stream} p.print("error", out, msg, stream) } else { - msg = fmt.Sprintln(p.pname(p.Name, 5), ":", p.Green.Regular("Builded")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) + msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Builded")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) out = BufferOut{Time: time.Now(), Text: "Builded after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} p.print("log", out, msg, stream) } @@ -282,7 +283,7 @@ func (p *Project) build() error { func (p *Project) fmt(path string) error { if p.Fmt && strings.HasSuffix(path, ".go") { if stream, err := p.goTools(p.base, "gofmt", "-s", "-w", "-e", path); err != nil { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Fmt"), p.Red.Regular("there are some errors in"), ":", p.Magenta.Bold(path)) + msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Fmt"), style.Red.Regular("there are some errors in"), ":", style.Magenta.Bold(path)) out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Fmt", Stream: stream} p.print("error", out, msg, stream) return err @@ -295,7 +296,7 @@ func (p *Project) fmt(path string) error { func (p *Project) generate(path string) error { if p.Generate { if stream, err := p.goTools(path, "go", "generate"); err != nil { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Generate"), p.Red.Regular("there are some errors in"), ":", p.Magenta.Bold(path)) + msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Generate"), style.Red.Regular("there are some errors in"), ":", style.Magenta.Bold(path)) out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Generate", Stream: stream} p.print("error", out, msg, stream) return err @@ -308,7 +309,7 @@ func (p *Project) generate(path string) error { func (p *Project) test(path string) error { if p.Test { if stream, err := p.goTools(path, "go", "test"); err != nil { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Test"), p.Red.Regular("there are some errors in "), ":", p.Magenta.Bold(path)) + msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Test"), style.Red.Regular("there are some errors in "), ":", style.Magenta.Bold(path)) out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Test", Stream: stream} p.print("error", out, msg, stream) return err @@ -322,7 +323,7 @@ func (p *Project) cmd(flag string) { for _, cmd := range p.Watcher.Scripts { if strings.ToLower(cmd.Type) == flag { errors, logs := p.command(cmd) - msg = fmt.Sprintln(p.pname(p.Name, 5), ":", p.Green.Bold("Command"), p.Green.Bold("\"")+cmd.Command+p.Green.Bold("\"")) + msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Bold("Command"), style.Green.Bold("\"")+cmd.Command+style.Green.Bold("\"")) out = BufferOut{Time: time.Now(), Text: cmd.Command, Type: flag} if logs != "" { p.print("log", out, msg, "") @@ -336,7 +337,7 @@ func (p *Project) cmd(flag string) { p.print("log", out, "", msg) } if errors != "" { - msg = fmt.Sprintln(p.Red.Regular(errors)) + msg = fmt.Sprintln(style.Red.Regular(errors)) out = BufferOut{Time: time.Now(), Text: errors, Type: flag} p.print("error", out, "", msg) } @@ -369,19 +370,19 @@ func (p *Project) routines(channel chan bool, wr *sync.WaitGroup) { func (p *Project) pname(name string, color int) string { switch color { case 1: - name = p.Yellow.Regular("[") + strings.ToUpper(name) + p.Yellow.Regular("]") + name = style.Yellow.Regular("[") + strings.ToUpper(name) + style.Yellow.Regular("]") break case 2: - name = p.Yellow.Regular("[") + p.Red.Bold(strings.ToUpper(name)) + p.Yellow.Regular("]") + name = style.Yellow.Regular("[") + style.Red.Bold(strings.ToUpper(name)) + style.Yellow.Regular("]") break case 3: - name = p.Yellow.Regular("[") + p.Blue.Bold(strings.ToUpper(name)) + p.Yellow.Regular("]") + name = style.Yellow.Regular("[") + style.Blue.Bold(strings.ToUpper(name)) + style.Yellow.Regular("]") break case 4: - name = p.Yellow.Regular("[") + p.Magenta.Bold(strings.ToUpper(name)) + p.Yellow.Regular("]") + name = style.Yellow.Regular("[") + style.Magenta.Bold(strings.ToUpper(name)) + style.Yellow.Regular("]") break case 5: - name = p.Yellow.Regular("[") + p.Green.Bold(strings.ToUpper(name)) + p.Yellow.Regular("]") + name = style.Yellow.Regular("[") + style.Green.Bold(strings.ToUpper(name)) + style.Yellow.Regular("]") break } return name From 1060df80f62b48f737b7f728f60d3323dacc489f Mon Sep 17 00:00:00 2001 From: Kyoichiro Yamada Date: Thu, 13 Apr 2017 22:53:58 +0900 Subject: [PATCH 7/8] refactoring: arrange CLI propoerties --- realize.go | 87 +++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/realize.go b/realize.go index 7c32580..0772c57 100644 --- a/realize.go +++ b/realize.go @@ -16,37 +16,32 @@ import ( ) const ( - name = "Realize" - version = "1.3" - description = "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths" - config = "realize.yaml" - outputs = "outputs.log" - errs = "errors.log" - logs = "logs.log" - host = "localhost" - port = 5001 - interval = 200 + appVersion = "1.3" + + config = "realize.yaml" + outputs = "outputs.log" + errs = "errors.log" + logs = "logs.log" + host = "localhost" + port = 5001 + interval = 200 ) var r realize // Realize struct contains the general app informations type realize struct { - settings.Settings `yaml:"settings,omitempty"` - Name, Description, Author, Email, Host, Version string `yaml:"-"` - Sync chan string `yaml:"-"` - Blueprint watcher.Blueprint `yaml:"-"` - Server server.Server `yaml:"-"` - Projects *[]watcher.Project `yaml:"projects" json:"projects"` + settings.Settings `yaml:"settings,omitempty"` + Sync chan string `yaml:"-"` + Blueprint watcher.Blueprint `yaml:"-"` + Server server.Server `yaml:"-"` + Projects *[]watcher.Project `yaml:"projects" json:"projects"` } // Realize struct initialization func init() { r = realize{ - Name: name, - Version: version, - Description: description, - Sync: make(chan string), + Sync: make(chan string), Settings: settings.Settings{ Config: settings.Config{ Create: true, @@ -106,8 +101,8 @@ func handle(err error) error { // Cli commands func main() { app := &cli.App{ - Name: r.Name, - Version: r.Version, + Name: "Realize", + Version: appVersion, Authors: []*cli.Author{ { Name: "Alessio Pracchia", @@ -118,12 +113,12 @@ func main() { Email: "conventi@hastega.it", }, }, - Usage: r.Description, + Description: "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths", Commands: []*cli.Command{ { - Name: "run", - Aliases: []string{"r"}, - Usage: "Run a toolchain on a project or a list of projects. If not exist a config file it creates a new one.", + 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", 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."}, @@ -159,10 +154,10 @@ func main() { }, }, { - Name: "add", - Category: "Configuration", - Aliases: []string{"a"}, - Usage: "Add a project to an existing config file or create a new one.", + 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."}, @@ -187,10 +182,10 @@ func main() { }, }, { - Name: "init", - Category: "Configuration", - Aliases: []string{"a"}, - Usage: "Define a new config file with all options step by step", + 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 { @@ -880,10 +875,10 @@ func main() { }, }, { - Name: "remove", - Category: "Configuration", - Aliases: []string{"r"}, - Usage: "Remove a project from a realize configuration.", + Name: "remove", + Category: "Configuration", + Aliases: []string{"r"}, + Description: "Remove a project from a realize configuration.", Flags: []cli.Flag{ &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: ""}, }, @@ -898,10 +893,10 @@ func main() { }, }, { - Name: "list", - Category: "Configuration", - Aliases: []string{"l"}, - Usage: "Print projects list.", + Name: "list", + Category: "Configuration", + Aliases: []string{"l"}, + Description: "Print projects list.", Action: func(p *cli.Context) error { return handle(r.Blueprint.List()) }, @@ -910,10 +905,10 @@ func main() { }, }, { - Name: "clean", - Category: "Configuration", - Aliases: []string{"c"}, - Usage: "Remove realize folder.", + Name: "clean", + Category: "Configuration", + Aliases: []string{"c"}, + Description: "Remove realize folder.", Action: func(p *cli.Context) error { handle(r.Settings.Remove()) fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Realize folder successfully removed.")) From 503dcab792644567732927958b18047cfeedce50 Mon Sep 17 00:00:00 2001 From: Kyoichiro Yamada Date: Thu, 13 Apr 2017 23:09:53 +0900 Subject: [PATCH 8/8] refactoring: arrange error handing and application initializing --- realize.go | 207 +++++++++++++++++++------------------ settings/flimit.go | 9 +- settings/flimit_windows.go | 5 +- 3 files changed, 113 insertions(+), 108 deletions(-) diff --git a/realize.go b/realize.go index 0772c57..798752d 100644 --- a/realize.go +++ b/realize.go @@ -27,79 +27,70 @@ const ( interval = 200 ) -var r realize - -// 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"` -} - -// Realize struct initialization -func init() { - 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 - - // read if exist - r.Read(&r) - - // increase the file limit - if r.Config.Flimit != 0 { - r.Flimit() - } -} - -// Before of every exec of a cli method -func before() error { - gopath := os.Getenv("GOPATH") - if gopath == "" { - return handle(errors.New("$GOPATH isn't set up properly")) - } - return nil -} - -// Handle errors -func handle(err error) error { - if err != nil { - fmt.Println(style.Red.Bold(err.Error())) - os.Exit(1) - } - return nil -} - // 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"` + } + + var r realize + // Before of every exec of a cli method + before := func(*cli.Context) error { + gopath := os.Getenv("GOPATH") + if gopath == "" { + return errors.New("$GOPATH isn't set up properly") + } + + 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 + + // 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 + } + app := &cli.App{ Name: "Realize", Version: appVersion, @@ -142,16 +133,22 @@ func main() { r.Config.Create = false } r.Blueprint.Projects = []watcher.Project{} - handle(r.Blueprint.Add(p)) + if err := r.Blueprint.Add(p); err != nil { + return err + } + } + if err := r.Server.Start(p); err != nil { + return err + } + if err := r.Blueprint.Run(); err != nil { + return err + } + if err := r.Record(r); err != nil { + return err } - handle(r.Server.Start(p)) - handle(r.Blueprint.Run()) - handle(r.Record(r)) return nil }, - Before: func(c *cli.Context) error { - return before() - }, + Before: before, }, { Name: "add", @@ -170,16 +167,18 @@ func main() { &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) (err error) { + Action: func(p *cli.Context) error { fmt.Println(p.String("path")) - handle(r.Blueprint.Add(p)) - handle(r.Record(r)) + 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: func(c *cli.Context) error { - return before() - }, + Before: before, }, { Name: "init", @@ -866,13 +865,13 @@ func main() { return nil }, }) - handle(r.Record(r)) + 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.")) return nil }, - Before: func(c *cli.Context) error { - return before() - }, + Before: before, }, { Name: "remove", @@ -883,14 +882,16 @@ func main() { &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: ""}, }, Action: func(p *cli.Context) error { - handle(r.Blueprint.Remove(p)) - handle(r.Record(r)) + 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.")) return nil }, - Before: func(c *cli.Context) error { - return before() - }, + Before: before, }, { Name: "list", @@ -898,11 +899,9 @@ func main() { Aliases: []string{"l"}, Description: "Print projects list.", Action: func(p *cli.Context) error { - return handle(r.Blueprint.List()) - }, - Before: func(c *cli.Context) error { - return before() + return r.Blueprint.List() }, + Before: before, }, { Name: "clean", @@ -910,15 +909,19 @@ func main() { Aliases: []string{"c"}, Description: "Remove realize folder.", Action: func(p *cli.Context) error { - handle(r.Settings.Remove()) + 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: func(c *cli.Context) error { - return before() - }, + Before: before, }, }, } - app.Run(os.Args) + + if err := app.Run(os.Args); err != nil { + fmt.Println(style.Red.Bold(err)) + os.Exit(1) + } } diff --git a/settings/flimit.go b/settings/flimit.go index 258c92d..6ba2df9 100644 --- a/settings/flimit.go +++ b/settings/flimit.go @@ -5,12 +5,13 @@ package settings import "syscall" // Flimit defines the max number of watched files -func (s *Settings) Flimit() { +func (s *Settings) Flimit() error { var rLimit syscall.Rlimit rLimit.Max = uint64(s.Config.Flimit) rLimit.Cur = uint64(s.Config.Flimit) - err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) - if err != nil { - s.Fatal(err, "Error setting rlimit") + + if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil { + return err } + return nil } diff --git a/settings/flimit_windows.go b/settings/flimit_windows.go index 60a53eb..f74d23a 100644 --- a/settings/flimit_windows.go +++ b/settings/flimit_windows.go @@ -1,6 +1,7 @@ +// build windows package settings // Flimit defines the max number of watched files -func (s *Settings) Flimit() { - return +func (s *Settings) Flimit() error { + return nil }