diff --git a/main.go b/main.go index 66152c0..a4c82a7 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,9 @@ package main import ( - "os" - "gopkg.in/urfave/cli.v2" r "github.com/tockins/realize/realize" + "gopkg.in/urfave/cli.v2" + "os" ) func main() { @@ -23,7 +23,7 @@ func main() { } cli := &cli.App{ - Name: app.Name, + Name: app.Name, Version: app.Version, Authors: []*cli.Author{ &cli.Author{ @@ -34,7 +34,7 @@ func main() { Usage: app.Description, Commands: []*cli.Command{ { - Name: "run", + Name: "run", Usage: "Build and watch file changes", Action: func(p *cli.Context) error { y := r.New(p) @@ -49,8 +49,8 @@ func main() { { Name: "start", Category: "config", - Aliases: []string{"s"}, - Usage: "Create the initial config", + Aliases: []string{"s"}, + Usage: "Create the initial config", Flags: []cli.Flag{ &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App", Usage: "Project name \t"}, &cli.StringFlag{Name: "main", Aliases: []string{"m"}, Value: "main.go", Usage: "Project main file \t"}, @@ -71,8 +71,8 @@ func main() { { Name: "add", Category: "config", - Aliases: []string{"a"}, - Usage: "Add another project", + Aliases: []string{"a"}, + Usage: "Add another project", Flags: []cli.Flag{ &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App", Usage: "Project name \t"}, &cli.StringFlag{Name: "main", Aliases: []string{"m"}, Value: "main.go", Usage: "Project main file \t"}, @@ -93,8 +93,8 @@ func main() { { Name: "remove", Category: "config", - Aliases: []string{"r"}, - Usage: "Remove a project", + Aliases: []string{"r"}, + Usage: "Remove a project", Flags: []cli.Flag{ &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App"}, }, @@ -110,8 +110,8 @@ func main() { { Name: "list", Category: "config", - Aliases: []string{"l"}, - Usage: "Projects list", + Aliases: []string{"l"}, + Usage: "Projects list", Action: func(p *cli.Context) error { y := r.New(p) return handle(y.List()) @@ -125,4 +125,4 @@ func main() { } cli.Run(os.Args) -} \ No newline at end of file +} diff --git a/realize/app.go b/realize/app.go index 7a77af1..8584825 100644 --- a/realize/app.go +++ b/realize/app.go @@ -1,19 +1,19 @@ package realize import ( - "github.com/fatih/color" - "sync" "fmt" + "github.com/fatih/color" "log" + "sync" ) const ( - app_name = "Realize" - app_version = "v1.0" - app_email = "pracchia@hastega.it" + app_name = "Realize" + app_version = "v1.0" + app_email = "pracchia@hastega.it" app_description = "Run, install or build your applications on file changes. Output preview and multi project support" - app_author = "Alessio Pracchia" - app_file = "realize.config.yaml" + app_author = "Alessio Pracchia" + app_file = "realize.config.yaml" ) var wg sync.WaitGroup @@ -33,11 +33,11 @@ type App struct { func Init() *App { return &App{ - Name: app_name, - Version: app_version, + Name: app_name, + Version: app_version, Description: app_description, - Author: app_author, - Email: app_email, + Author: app_author, + Email: app_email, } } @@ -61,4 +61,3 @@ func (app *App) Information() { fmt.Println(blue(app.Name) + " - " + blue(app.Version)) fmt.Println(bluel(app.Description) + "\n") } - diff --git a/realize/config.go b/realize/config.go index 4b08a16..89a789c 100644 --- a/realize/config.go +++ b/realize/config.go @@ -1,12 +1,12 @@ package realize import ( - "os" - "gopkg.in/yaml.v2" "errors" - "gopkg.in/urfave/cli.v2" - "io/ioutil" "fmt" + "gopkg.in/urfave/cli.v2" + "gopkg.in/yaml.v2" + "io/ioutil" + "os" ) type Config struct { @@ -18,20 +18,20 @@ type Config struct { // Default value func New(params *cli.Context) *Config { return &Config{ - file: app_file, + file: app_file, Version: "1.0", Projects: []Project{ { - Name: params.String("name"), - Main: params.String("main"), - Path: params.String("base"), - Run: params.Bool("run"), + Name: params.String("name"), + Main: params.String("main"), + Path: params.String("base"), + Run: params.Bool("run"), Build: params.Bool("build"), - Bin: params.Bool("bin"), + Bin: params.Bool("bin"), Watcher: Watcher{ - Paths: watcher_paths, + Paths: watcher_paths, Ignore: watcher_ignores, - Exts: watcher_exts, + Exts: watcher_exts, }, }, }, @@ -52,8 +52,8 @@ func Duplicates(value Project, arr []Project) bool { func (h *Config) Clean() { arr := h.Projects for key, val := range arr { - if Duplicates(val, arr[key + 1:]) { - h.Projects = append(arr[:key], arr[key + 1:]...) + if Duplicates(val, arr[key+1:]) { + h.Projects = append(arr[:key], arr[key+1:]...) break } } @@ -102,14 +102,14 @@ func (h *Config) Create(params *cli.Context) error { func (h *Config) Add(params *cli.Context) error { if err := h.Read(); err == nil { new := Project{ - Name: params.String("name"), - Main: params.String("main"), - Path: params.String("base"), - Run: params.Bool("run"), + Name: params.String("name"), + Main: params.String("main"), + Path: params.String("base"), + Run: params.Bool("run"), Build: params.Bool("build"), Watcher: Watcher{ - Paths: watcher_paths, - Exts: watcher_exts, + Paths: watcher_paths, + Exts: watcher_exts, Ignore: watcher_ignores, }, } @@ -132,9 +132,9 @@ func (h *Config) Remove(params *cli.Context) error { if err := h.Read(); err == nil { for key, val := range h.Projects { if params.String("name") == val.Name { - h.Projects = append(h.Projects[:key], h.Projects[key + 1:]...) + h.Projects = append(h.Projects[:key], h.Projects[key+1:]...) err = h.Write() - if err == nil{ + if err == nil { Success("Your project was successfully removed") } return err @@ -169,5 +169,3 @@ func (h *Config) List() error { return err } } - - diff --git a/realize/project.go b/realize/project.go index 7d8f2eb..61038f5 100644 --- a/realize/project.go +++ b/realize/project.go @@ -1,40 +1,40 @@ package realize import ( - "time" - "os/exec" - "os" - "bytes" "bufio" + "bytes" "log" - "sync" + "os" + "os/exec" "strings" + "sync" + "time" ) type Project struct { reload time.Time - base string - Name string `yaml:"app_name,omitempty"` - Path string `yaml:"app_path,omitempty"` - Main string `yaml:"app_main,omitempty"` - Run bool `yaml:"app_run,omitempty"` - Bin bool `yaml:"app_bin,omitempty"` - Build bool `yaml:"app_build,omitempty"` + base string + Name string `yaml:"app_name,omitempty"` + Path string `yaml:"app_path,omitempty"` + Main string `yaml:"app_main,omitempty"` + Run bool `yaml:"app_run,omitempty"` + Bin bool `yaml:"app_bin,omitempty"` + Build bool `yaml:"app_build,omitempty"` Watcher Watcher `yaml:"app_watcher,omitempty"` } func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error { name := strings.Split(p.Path, "/") - stop := make(chan bool,1) + stop := make(chan bool, 1) var run string if len(name) == 1 { name := strings.Split(p.base, "/") run = name[len(name)-1] - }else{ + } else { run = name[len(name)-1] } - build := exec.Command(os.Getenv("GOPATH")+slash("bin")+slash(run)) + build := exec.Command(os.Getenv("GOPATH") + slash("bin") + slash(run)) build.Dir = p.base defer func() { if err := build.Process.Kill(); err != nil { @@ -58,15 +58,15 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) for in.Scan() { select { default: - log.Println(p.Name + ":", in.Text()) + log.Println(p.Name+":", in.Text()) } } close(stop) }() - for{ + for { select { - case <- channel: + case <-channel: return nil case <-stop: return nil @@ -81,11 +81,11 @@ func (p *Project) GoBuild() error { // create bin dir if _, err := os.Stat(p.base + "/bin"); err != nil { - if err = os.Mkdir(p.base + "/bin", 0777); err != nil { + if err = os.Mkdir(p.base+"/bin", 0777); err != nil { return err } } - build := exec.Command("go", "build", p.base + p.Main) + build := exec.Command("go", "build", p.base+p.Main) build.Dir = p.base + "/bin" build.Stdout = &out if err := build.Run(); err != nil { @@ -107,4 +107,3 @@ func (p *Project) GoInstall() error { } return nil } - diff --git a/realize/watcher.go b/realize/watcher.go index 2fd594b..8d24e8f 100644 --- a/realize/watcher.go +++ b/realize/watcher.go @@ -1,14 +1,14 @@ package realize import ( - "github.com/fsnotify/fsnotify" "fmt" - "path/filepath" - "os" - "strings" + "github.com/fsnotify/fsnotify" "log" - "time" + "os" + "path/filepath" + "strings" "sync" + "time" ) type Watcher struct { @@ -18,7 +18,7 @@ type Watcher struct { Paths []string `yaml:"paths,omitempty"` Ignore []string `yaml:"ignore_paths,omitempty"` Exts []string `yaml:"exts,omitempty"` - Preview bool `yaml:"preview,omitempty"` + Preview bool `yaml:"preview,omitempty"` } func (h *Config) Watch() error { @@ -41,12 +41,12 @@ func (p *Project) Watching() { var wr sync.WaitGroup var watcher *fsnotify.Watcher watcher, err := fsnotify.NewWatcher() - if(err != nil){ + if err != nil { Fail(p.Name + ": \t" + err.Error()) } - channel := make(chan bool,1) + channel := make(chan bool, 1) base, err := os.Getwd() - if(err != nil){ + if err != nil { Fail(p.Name + ": \t" + err.Error()) } @@ -63,12 +63,14 @@ func (p *Project) Watching() { } return nil } - routines := func(){ + routines := func() { channel = make(chan bool) wr.Add(1) - go p.build(); p.install(); p.run(channel, &wr); + go p.build() + p.install() + p.run(channel, &wr) } - end := func(){ + end := func() { watcher.Close() wg.Done() } @@ -105,12 +107,12 @@ func (p *Project) Watching() { select { case event := <-watcher.Events: if time.Now().Truncate(time.Second).After(p.reload) { - if event.Op & fsnotify.Chmod == fsnotify.Chmod { + if event.Op&fsnotify.Chmod == fsnotify.Chmod { continue } if _, err := os.Stat(event.Name); err == nil { i := strings.Index(event.Name, filepath.Ext(event.Name)) - log.Println(green(p.Name + ":"), event.Name[:i]) + log.Println(green(p.Name+":"), event.Name[:i]) // stop and run again close(channel) @@ -154,20 +156,20 @@ func (p *Project) build() { return } -func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { - if p.Run{ +func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { + if p.Run { if p.Bin { - runner := make(chan bool,1) + runner := make(chan bool, 1) LogSuccess(p.Name + ": Running..") go p.GoRun(channel, runner, wr) - for{ + for { select { - case <- runner: + case <-runner: LogSuccess(p.Name + ": Runned") return } } - }else{ + } else { LogFail("Set 'app_run' to true for launch run") } } @@ -177,7 +179,7 @@ func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { func (p *Project) ignore(str string) bool { for _, v := range p.Watcher.Ignore { v = slash(v) - if strings.Contains(str, p.base + v) { + if strings.Contains(str, p.base+v) { return true } } @@ -193,16 +195,16 @@ func inArray(str string, list []string) bool { return false } -func slash(str string) string{ +func slash(str string) string { if string(str[0]) != "/" { - str = "/"+str + str = "/" + str } - if string(str[len(str)-1]) == "/"{ - if(string(str) == "/"){ + if string(str[len(str)-1]) == "/" { + if string(str) == "/" { str = "" - }else { - str = str[0:len(str) - 2] + } else { + str = str[0 : len(str)-2] } } return str -} \ No newline at end of file +}