From 227871d17159b5ca5d3168147166220ea08cfd35 Mon Sep 17 00:00:00 2001 From: asoseil Date: Sun, 22 Oct 2017 18:44:44 +0200 Subject: [PATCH 01/12] #100 name flag fixed --- cmd.go | 10 +++++++--- realize.go | 36 +++++++++++++++++++++++------------- settings.go | 16 ---------------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/cmd.go b/cmd.go index 774263e..83250a3 100644 --- a/cmd.go +++ b/cmd.go @@ -62,8 +62,8 @@ func (r *realize) check() error { // Add a new project func (r *realize) add(p *cli.Context) error { project := Project{ - Name: r.Settings.name(p.String("name"), p.String("path")), - Path: r.Settings.path(p.String("path")), + Name: filepath.Base(filepath.Clean(p.String("path"))), + Path: filepath.Clean(p.String("path")), Cmds: Cmds{ Vet: Cmd{ Status: p.Bool("vet"), @@ -101,6 +101,7 @@ func (r *realize) add(p *cli.Context) error { // Run launches the toolchain for each project func (r *realize) run(p *cli.Context) error { + var match bool err := r.check() if err == nil { // loop projects @@ -214,10 +215,13 @@ func (r *realize) run(p *cli.Context) error { } else { r.Schema[k].base = filepath.Join(wd, elm.path) } + match = true go r.Schema[k].watch() } + if !match { + return errors.New("there is no project with the given name") + } wg.Wait() - return nil } return err } diff --git a/realize.go b/realize.go index 4bd957f..552f5c1 100644 --- a/realize.go +++ b/realize.go @@ -8,6 +8,7 @@ import ( "gopkg.in/urfave/cli.v2" "log" "os" + "path/filepath" "strconv" "time" ) @@ -52,7 +53,7 @@ func main() { Aliases: []string{"r"}, Description: "Start a toolchain on a project or a list of projects. If not exist a config file it creates a new one", Flags: []cli.Flag{ - &cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: "", Usage: "Project base path"}, + &cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: wdir(), Usage: "Project base path"}, &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"}, @@ -68,7 +69,7 @@ func main() { if err := r.insert(p); err != nil { return err } - if !p.Bool("no-config") { + if !p.Bool("no-config") && p.String("name") == ""{ if err := r.Settings.record(r); err != nil { return err } @@ -86,7 +87,7 @@ func main() { 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.StringFlag{Name: "path", Aliases: []string{"p"}, Value: wdir(), Usage: "Project base path"}, &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"}, @@ -102,7 +103,7 @@ func main() { if err := r.Settings.record(r); err != nil { return err } - fmt.Fprintln(output, prefix(green.bold("Your project was successfully added"))) + log.Println(prefix(green.bold("Your project was successfully added"))) return nil }, Before: before, @@ -116,7 +117,7 @@ func main() { interact.Run(&interact.Interact{ Before: func(context interact.Context) error { context.SetErr(red.bold("INVALID INPUT")) - context.SetPrfx(color.Output, yellow.bold("[")+"REALIZE"+yellow.bold("]")) + context.SetPrfx(color.Output, yellow.regular("[") + time.Now().Format("15:04:05") + yellow.regular("]") + yellow.bold("[")+"REALIZE"+yellow.bold("]")) return nil }, Questions: []*interact.Question{ @@ -361,7 +362,7 @@ func main() { }, { Before: func(d interact.Context) error { - dir, _ := os.Getwd() + dir := wdir() d.SetDef(dir, green.regular("("+dir+")")) return nil }, @@ -374,7 +375,7 @@ func main() { if err != nil { return d.Err() } - r.Schema[len(r.Schema)-1].Path = r.Settings.path(val) + r.Schema[len(r.Schema)-1].Path = filepath.Clean(val) return nil }, }, @@ -1045,7 +1046,7 @@ func main() { if err := r.Settings.record(r); err != nil { return err } - fmt.Fprintln(output, prefix(green.bold(" Your configuration was successful"))) + log.Println(prefix(green.bold("Your configuration was successful"))) return nil }, Before: before, @@ -1065,7 +1066,7 @@ func main() { if err := r.Settings.record(r); err != nil { return err } - fmt.Fprintln(output, prefix(green.bold("Your project was successfully removed"))) + log.Println(prefix(green.bold("Your project was successfully removed"))) return nil }, Before: before, @@ -1079,7 +1080,7 @@ func main() { if err := r.Settings.del(directory); err != nil { return err } - fmt.Fprintln(output, prefix(green.bold("Realize folder successfully removed"))) + log.Println(prefix(green.bold("Realize folder successfully removed"))) return nil }, Before: before, @@ -1087,7 +1088,7 @@ func main() { }, } if err := app.Run(os.Args); err != nil { - fmt.Fprintln(output, prefix(red.bold(err))) + log.Println(prefix(red.bold(err))) os.Exit(1) } } @@ -1115,7 +1116,7 @@ func new() realize { // Prefix a given string func prefix(s string) string { if s != "" { - return fmt.Sprint(yellow.bold("["), "REALIZE", yellow.bold("]"), s) + return fmt.Sprint(yellow.bold("["), "REALIZE", yellow.bold("]"), " : ", s) } return "" } @@ -1143,7 +1144,16 @@ func before(*cli.Context) error { return nil } +// Wdir return current working directory +func wdir() string { + dir, err := os.Getwd() + if err != nil { + log.Fatal(prefix(err.Error())) + } + return dir +} + // Rewrite the layout of the log timestamp func (w logWriter) Write(bytes []byte) (int, error) { - return fmt.Fprint(output, yellow.regular("["), time.Now().Format("15:04:05"), yellow.regular("]")+string(bytes)) + return fmt.Fprint(output, yellow.regular("["), time.Now().Format("15:04:05"), yellow.regular("]"), string(bytes)) } diff --git a/settings.go b/settings.go index 8fe1cbe..2301e22 100644 --- a/settings.go +++ b/settings.go @@ -7,7 +7,6 @@ import ( "math/rand" "os" "path/filepath" - "strings" "syscall" "time" ) @@ -102,11 +101,6 @@ func (s *Settings) del(d string) error { return err } -// Path cleaner -func (s Settings) path(path string) string { - return strings.Replace(filepath.Clean(path), "\\", "/", -1) -} - // Validate checks a fatal error func (s Settings) validate(err error) error { if err != nil { @@ -171,16 +165,6 @@ func (s Settings) write(name string, data []byte) error { return s.validate(err) } -// Name return the project name or the path of the working dir -func (s Settings) name(name string, path string) string { - if name == "" && path == "" { - return s.wdir() - } else if path != "/" { - return filepath.Base(path) - } - return name -} - // Create a new file and return its pointer func (s Settings) create(path string, name string) *os.File { var file string From b972d0df043df7eae59106b1af5d6236bcdc763b Mon Sep 17 00:00:00 2001 From: asoseil Date: Sun, 22 Oct 2017 18:58:56 +0200 Subject: [PATCH 02/12] workflow fixed --- watcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/watcher.go b/watcher.go index 287f100..53de11b 100644 --- a/watcher.go +++ b/watcher.go @@ -407,7 +407,7 @@ func (p *Project) routines(stop <-chan bool, watcher FileWatcher, path string) { p.init = true } // prevent errors using realize without config with only run flag - if !p.Cmds.Install.Status && !p.Cmds.Build.Status { + if p.Cmds.Run && !p.Cmds.Install.Status && !p.Cmds.Build.Status { p.Cmds.Install.Status = true } if !done { From d00b437295dfdabcc17db6004c7a153f2ecf2a51 Mon Sep 17 00:00:00 2001 From: asoseil Date: Sun, 22 Oct 2017 19:22:07 +0200 Subject: [PATCH 03/12] wdir method moved to utils --- settings.go | 7 ------- utils.go | 10 ++++++++++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/settings.go b/settings.go index 2301e22..7b1af07 100644 --- a/settings.go +++ b/settings.go @@ -76,13 +76,6 @@ func random(n int) string { return string(b) } -// Wdir return the current working directory -func (s Settings) wdir() string { - dir, err := os.Getwd() - s.validate(err) - return filepath.Base(dir) -} - // Flimit defines the max number of watched files func (s *Settings) flimit() error { var rLimit syscall.Rlimit diff --git a/utils.go b/utils.go index 4025f79..79c94d8 100644 --- a/utils.go +++ b/utils.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "strings" + "log" ) // getEnvPath returns the first path found in env or empty string @@ -83,3 +84,12 @@ func replace(a []string, b string) []string { } return a } + +// Wdir return current working directory +func wdir() string { + dir, err := os.Getwd() + if err != nil { + log.Fatal(prefix(err.Error())) + } + return dir +} From d02dc91443a564cc8b29d0c7e3f276e9614fe242 Mon Sep 17 00:00:00 2001 From: asoseil Date: Sun, 22 Oct 2017 20:50:51 +0200 Subject: [PATCH 04/12] workflow improved --- exec.go | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/exec.go b/exec.go index 190e505..1397ecc 100644 --- a/exec.go +++ b/exec.go @@ -19,15 +19,8 @@ func (p *Project) goCompile(stop <-chan bool, method []string, args []string) (s var out bytes.Buffer var stderr bytes.Buffer done := make(chan error) - err := os.Setenv("GOBIN", filepath.Join(getEnvPath("GOPATH"), "bin")) - if err != nil { - return "", err - } args = append(method, args...) cmd := exec.Command(args[0], args[1:]...) - if _, err := os.Stat(filepath.Join(p.base, p.path)); err == nil { - p.path = filepath.Join(p.base, p.path) - } cmd.Dir = p.path cmd.Stdout = &out cmd.Stderr = &stderr @@ -53,6 +46,8 @@ func (p *Project) goCompile(stop <-chan bool, method []string, args []string) (s func (p *Project) goRun(stop <-chan bool, runner chan bool) { var build *exec.Cmd var args []string + + // custom error pattern isErrorText := func(string) bool { return false } @@ -67,20 +62,22 @@ func (p *Project) goRun(stop <-chan bool, runner chan bool) { } } + // add additional arguments for _, arg := range p.Args { a := strings.FieldsFunc(arg, func(i rune) bool { return i == '"' || i == '=' || i == '\'' }) args = append(args, a...) - } - if _, err := os.Stat(filepath.Join(getEnvPath("GOBIN"), filepath.Base(p.base))); err == nil { - build = exec.Command(filepath.Join(getEnvPath("GOBIN"), filepath.Base(p.base)), args...) - } else if _, err := os.Stat(filepath.Join(getEnvPath("GOBIN"), filepath.Base(p.base)) + extWindows); err == nil { - build = exec.Command(filepath.Join(getEnvPath("GOBIN"), filepath.Base(p.base))+extWindows, args...) + gobin := os.Getenv("GOBIN") + path := filepath.Join(gobin, p.name) + if _, err := os.Stat(path); err == nil { + build = exec.Command(path, args...) + } else if _, err := os.Stat(path + extWindows); err == nil { + build = exec.Command(path+extWindows, args...) } else { - path := filepath.Join(p.base, filepath.Base(p.base)) + path := filepath.Join(p.path, p.name) if _, err = os.Stat(path); err == nil { build = exec.Command(path, args...) } else if _, err = os.Stat(path + extWindows); err == nil { @@ -100,6 +97,7 @@ func (p *Project) goRun(stop <-chan bool, runner chan bool) { p.stamp("log", out, msg, "") }() + // scan project stream stdout, err := build.StdoutPipe() stderr, err := build.StderrPipe() if err != nil { @@ -149,12 +147,13 @@ func (p *Project) command(stop <-chan bool, cmd Command) (string, string) { done := make(chan error) args := strings.Split(strings.Replace(strings.Replace(cmd.Command, "'", "", -1), "\"", "", -1), " ") exec := exec.Command(args[0], args[1:]...) - exec.Dir = p.base + exec.Dir = p.path + // make cmd path if cmd.Path != "" { - if strings.Contains(cmd.Path, p.base) { + if strings.Contains(cmd.Path, p.path) { exec.Dir = cmd.Path } else { - exec.Dir = filepath.Join(p.base, cmd.Path) + exec.Dir = filepath.Join(p.path, cmd.Path) } } exec.Stdout = &stdout @@ -187,7 +186,7 @@ func (p *Project) goTool(wg *sync.WaitGroup, stop <-chan bool, result chan<- too if strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "") { if strings.HasSuffix(path, ".go") { tool.options = append(tool.options, path) - path = p.base + path = p.path } if s := ext(path); s == "" || s == "go" { var out, stderr bytes.Buffer From bfff06d38a36b8da10707eb60e1226c263ace198 Mon Sep 17 00:00:00 2001 From: asoseil Date: Sun, 22 Oct 2017 23:22:45 +0200 Subject: [PATCH 05/12] ignore hidden dir support --- cmd.go | 258 ++++++++++++++++++++++++++--------------------------- exec.go | 1 + realize.go | 11 +-- utils.go | 10 --- watcher.go | 13 +-- 5 files changed, 137 insertions(+), 156 deletions(-) diff --git a/cmd.go b/cmd.go index 83250a3..958a69b 100644 --- a/cmd.go +++ b/cmd.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "time" + "reflect" ) // Tool options customizable, should be moved in Cmd @@ -50,15 +51,6 @@ func (r *realize) clean() { } } -// Check whether there is a project -func (r *realize) check() error { - if len(r.Schema) > 0 { - r.clean() - return nil - } - return errors.New("there are no projects") -} - // Add a new project func (r *realize) add(p *cli.Context) error { project := Project{ @@ -88,7 +80,7 @@ func (r *realize) add(p *cli.Context) error { Args: params(p), Watcher: Watch{ Paths: []string{"/"}, - Ignore: []string{"vendor"}, + Ignore: []string{".git",".realize","vendor"}, Exts: []string{"go"}, }, } @@ -102,127 +94,133 @@ func (r *realize) add(p *cli.Context) error { // Run launches the toolchain for each project func (r *realize) run(p *cli.Context) error { var match bool - err := r.check() - if err == nil { - // loop projects - if p.String("name") != "" { - wg.Add(1) - } else { - wg.Add(len(r.Schema)) - } - for k, elm := range r.Schema { - // command start using name flag - if p.String("name") != "" && r.Schema[k].Name != p.String("name") { - continue - } - //fields := reflect.Indirect(reflect.ValueOf(&r.Schema[k].Cmds)) - //// Loop struct Cmds fields - //for i := 0; i < fields.NumField(); i++ { - // field := fields.Type().Field(i).Name - // if fields.FieldByName(field).Type().Name() == "Cmd" { - // v := fields.FieldByName(field) - // // Loop struct Cmd - // for i := 0; i < v.NumField(); i++ { - // f := v.Field(i) - // if f.IsValid() { - // if f.CanSet() { - // switch f.Kind() { - // case reflect.Bool: - // case reflect.String: - // case reflect.Slice: - // } - // } - // } - // } - // } - //} - if elm.Cmds.Fmt.Status { - if len(elm.Cmds.Fmt.Args) == 0 { - elm.Cmds.Fmt.Args = []string{"-s", "-w", "-e", "./"} - } - r.Schema[k].tools = append(r.Schema[k].tools, tool{ - status: elm.Cmds.Fmt.Status, - cmd: replace([]string{"gofmt"}, r.Schema[k].Cmds.Fmt.Method), - options: split([]string{}, elm.Cmds.Fmt.Args), - name: "Fmt", - }) - } - if elm.Cmds.Generate.Status { - r.Schema[k].tools = append(r.Schema[k].tools, tool{ - status: elm.Cmds.Generate.Status, - cmd: replace([]string{"go", "generate"}, r.Schema[k].Cmds.Generate.Method), - options: split([]string{}, elm.Cmds.Generate.Args), - name: "Generate", - dir: true, - }) - } - if elm.Cmds.Test.Status { - r.Schema[k].tools = append(r.Schema[k].tools, tool{ - status: elm.Cmds.Test.Status, - cmd: replace([]string{"go", "test"}, r.Schema[k].Cmds.Test.Method), - options: split([]string{}, elm.Cmds.Test.Args), - name: "Test", - dir: true, - }) - } - if elm.Cmds.Vet.Status { - r.Schema[k].tools = append(r.Schema[k].tools, tool{ - status: elm.Cmds.Vet.Status, - cmd: replace([]string{"go", "vet"}, r.Schema[k].Cmds.Vet.Method), - options: split([]string{}, elm.Cmds.Vet.Args), - name: "Vet", - dir: true, - }) - } - // default settings - r.Schema[k].Cmds.Install = Cmd{ - Status: elm.Cmds.Install.Status, - Args: append([]string{}, elm.Cmds.Install.Args...), - method: replace([]string{"go", "install"}, r.Schema[k].Cmds.Install.Method), - name: "Install", - startTxt: "Installing...", - endTxt: "Installed", - } - r.Schema[k].Cmds.Build = Cmd{ - Status: elm.Cmds.Build.Status, - Args: append([]string{}, elm.Cmds.Build.Args...), - method: replace([]string{"go", "build"}, r.Schema[k].Cmds.Build.Method), - name: "Build", - startTxt: "Building...", - endTxt: "Built", - } - - r.Schema[k].parent = r - r.Schema[k].path = r.Schema[k].Path - - // env variables - for key, item := range r.Schema[k].Environment { - if err := os.Setenv(key, item); err != nil { - r.Schema[k].Buffer.StdErr = append(r.Schema[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""}) - } - } - - // base path of the project - wd, err := os.Getwd() - if err != nil { - return err - } - if elm.path == "." || elm.path == "/" { - r.Schema[k].base = wd - r.Schema[k].path = elm.wdir() - } else if filepath.IsAbs(elm.path) { - r.Schema[k].base = elm.path - } else { - r.Schema[k].base = filepath.Join(wd, elm.path) - } - match = true - go r.Schema[k].watch() - } - if !match { - return errors.New("there is no project with the given name") - } - wg.Wait() + // check projects and remove duplicates + if len(r.Schema) > 0 { + r.clean() + }else{ + return errors.New("there are no projects") } + // set gobin + err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin")) + if err != nil { + return err + } + // loop projects + if p.String("name") != "" { + wg.Add(1) + } else { + wg.Add(len(r.Schema)) + } + for k, elm := range r.Schema { + // command start using name flag + if p.String("name") != "" && r.Schema[k].Name != p.String("name") { + continue + } + // validate project path, if invalid get wdir or clean current + if !filepath.IsAbs(elm.path){ + r.Schema[k].path = wdir() + }else{ + r.Schema[k].path = filepath.Clean(elm.path) + } + // env variables + for key, item := range r.Schema[k].Environment { + if err := os.Setenv(key, item); err != nil { + r.Schema[k].Buffer.StdErr = append(r.Schema[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""}) + } + } + // get basepath name + r.Schema[k].name = filepath.Base(r.Schema[k].path) + + fields := reflect.Indirect(reflect.ValueOf(&r.Schema[k].Cmds)) + // Loop struct Cmds fields + for i := 0; i < fields.NumField(); i++ { + field := fields.Type().Field(i).Name + if fields.FieldByName(field).Type().Name() == "Cmd" { + v := fields.FieldByName(field) + // Loop struct Cmd + for i := 0; i < v.NumField(); i++ { + //f := v.Type().Field(i).Name + //fmt.Println(f) + //if f.IsValid() { + // if f.CanSet() { + // fmt.Println(f.) + // //switch f.Kind() { + // //case reflect.Bool: + // //case reflect.String: + // //case reflect.Slice: + // //} + // } + //} + } + } + } + + if elm.Cmds.Fmt.Status { + if len(elm.Cmds.Fmt.Args) == 0 { + elm.Cmds.Fmt.Args = []string{"-s", "-w", "-e", "./"} + } + r.Schema[k].tools = append(r.Schema[k].tools, tool{ + status: elm.Cmds.Fmt.Status, + cmd: replace([]string{"gofmt"}, r.Schema[k].Cmds.Fmt.Method), + options: split([]string{}, elm.Cmds.Fmt.Args), + name: "Fmt", + }) + } + if elm.Cmds.Generate.Status { + r.Schema[k].tools = append(r.Schema[k].tools, tool{ + status: elm.Cmds.Generate.Status, + cmd: replace([]string{"go", "generate"}, r.Schema[k].Cmds.Generate.Method), + options: split([]string{}, elm.Cmds.Generate.Args), + name: "Generate", + dir: true, + }) + } + if elm.Cmds.Test.Status { + r.Schema[k].tools = append(r.Schema[k].tools, tool{ + status: elm.Cmds.Test.Status, + cmd: replace([]string{"go", "test"}, r.Schema[k].Cmds.Test.Method), + options: split([]string{}, elm.Cmds.Test.Args), + name: "Test", + dir: true, + }) + } + if elm.Cmds.Vet.Status { + r.Schema[k].tools = append(r.Schema[k].tools, tool{ + status: elm.Cmds.Vet.Status, + cmd: replace([]string{"go", "vet"}, r.Schema[k].Cmds.Vet.Method), + options: split([]string{}, elm.Cmds.Vet.Args), + name: "Vet", + dir: true, + }) + } + // default settings + r.Schema[k].Cmds.Install = Cmd{ + Status: elm.Cmds.Install.Status, + Args: append([]string{}, elm.Cmds.Install.Args...), + method: replace([]string{"go", "install"}, r.Schema[k].Cmds.Install.Method), + name: "Install", + startTxt: "Installing...", + endTxt: "Installed", + } + r.Schema[k].Cmds.Build = Cmd{ + Status: elm.Cmds.Build.Status, + Args: append([]string{}, elm.Cmds.Build.Args...), + method: replace([]string{"go", "build"}, r.Schema[k].Cmds.Build.Method), + name: "Build", + startTxt: "Building...", + endTxt: "Built", + } + + r.Schema[k].parent = r + r.Schema[k].path = r.Schema[k].Path + + match = true + go r.Schema[k].watch() + } + if !match { + return errors.New("there is no project with the given name") + } + wg.Wait() return err } diff --git a/exec.go b/exec.go index 1397ecc..1e3d88e 100644 --- a/exec.go +++ b/exec.go @@ -70,6 +70,7 @@ func (p *Project) goRun(stop <-chan bool, runner chan bool) { args = append(args, a...) } + gobin := os.Getenv("GOBIN") path := filepath.Join(gobin, p.name) if _, err := os.Stat(path); err == nil { diff --git a/realize.go b/realize.go index 552f5c1..f12df99 100644 --- a/realize.go +++ b/realize.go @@ -344,7 +344,7 @@ func main() { Subs: []*interact.Question{ { Before: func(d interact.Context) error { - d.SetDef(r.Settings.wdir(), green.regular("("+r.Settings.wdir()+")")) + d.SetDef(wdir(), green.regular("("+wdir()+")")) return nil }, Quest: interact.Quest{ @@ -1144,15 +1144,6 @@ func before(*cli.Context) error { return nil } -// Wdir return current working directory -func wdir() string { - dir, err := os.Getwd() - if err != nil { - log.Fatal(prefix(err.Error())) - } - return dir -} - // Rewrite the layout of the log timestamp func (w logWriter) Write(bytes []byte) (int, error) { return fmt.Fprint(output, yellow.regular("["), time.Now().Format("15:04:05"), yellow.regular("]"), string(bytes)) diff --git a/utils.go b/utils.go index 79c94d8..2783d26 100644 --- a/utils.go +++ b/utils.go @@ -4,20 +4,10 @@ import ( "errors" "gopkg.in/urfave/cli.v2" "os" - "path/filepath" "strings" "log" ) -// getEnvPath returns the first path found in env or empty string -func getEnvPath(env string) string { - path := filepath.SplitList(os.Getenv(env)) - if len(path) == 0 { - return "" - } - return path[0] -} - // Array check if a string is in given array func array(str string, list []string) bool { for _, v := range list { diff --git a/watcher.go b/watcher.go index 53de11b..dd161ea 100644 --- a/watcher.go +++ b/watcher.go @@ -55,7 +55,7 @@ type Project struct { watcher FileWatcher init bool files, folders int64 - base, path, lastFile string + name, path, lastFile string tools []tool paths []string lastTime time.Time @@ -98,7 +98,7 @@ func (p *Project) watch() { p.cmd(stop, "before", true) // indexing files and dirs for _, dir := range p.Watcher.Paths { - base := filepath.Join(p.base, dir) + base := filepath.Join(p.path, dir) if _, err := os.Stat(base); err == nil { if err := filepath.Walk(base, p.walk); err == nil { p.tool(stop, base) @@ -312,7 +312,8 @@ func (p *Project) changed(event fsnotify.Event, stop chan bool) { // Watch the files tree of a project func (p *Project) walk(path string, info os.FileInfo, err error) error { for _, v := range p.Watcher.Ignore { - if strings.Contains(path, filepath.Join(p.base, v)) { + s := append([]string{p.path},strings.Split(v,string(os.PathSeparator))...) + if strings.Contains(path, filepath.Join(s...)) { return nil } } @@ -338,7 +339,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { case "out": p.Buffer.StdOut = append(p.Buffer.StdOut, o) if p.Files.Outputs.Status { - f := p.create(p.base, p.Files.Outputs.Name) + f := p.create(p.path, p.Files.Outputs.Name) t := time.Now() s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"} if _, err := f.WriteString(strings.Join(s, " ")); err != nil { @@ -348,7 +349,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { case "log": p.Buffer.StdLog = append(p.Buffer.StdLog, o) if p.Files.Logs.Status { - f := p.create(p.base, p.Files.Logs.Name) + f := p.create(p.path, p.Files.Logs.Name) t := time.Now() s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"} if stream != "" { @@ -361,7 +362,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { case "error": p.Buffer.StdErr = append(p.Buffer.StdErr, o) if p.Files.Errors.Status { - f := p.create(p.base, p.Files.Errors.Name) + f := p.create(p.path, p.Files.Errors.Name) t := time.Now() s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Type, o.Text, o.Path, "\r\n"} if stream != "" { From 968726968f042d5ab57b529d788cc216b606b065 Mon Sep 17 00:00:00 2001 From: asoseil Date: Sun, 22 Oct 2017 23:54:02 +0200 Subject: [PATCH 06/12] tests updated --- cmd_test.go | 24 ++++++------------------ realize_test.go | 3 +-- settings_test.go | 40 +++------------------------------------- utils_test.go | 13 ++++++++----- watcher_test.go | 2 +- 5 files changed, 19 insertions(+), 63 deletions(-) diff --git a/cmd_test.go b/cmd_test.go index 32d4682..6684754 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -8,6 +8,7 @@ import ( "reflect" "testing" "time" + "path/filepath" ) type loggerT struct{} @@ -39,19 +40,6 @@ func TestRealize_Clean(t *testing.T) { } -func TestRealize_Check(t *testing.T) { - r := realize{} - err := r.check() - if err == nil { - t.Error("There is no project, error expected") - } - r.Schema = append(r.Schema, Project{Name: "test0"}) - err = r.check() - if err != nil { - t.Error("There is a project, error unexpected", err) - } -} - func TestRealize_Add(t *testing.T) { r := realize{} // add all flags, test with expected @@ -63,13 +51,13 @@ func TestRealize_Add(t *testing.T) { set.Bool("run", false, "") set.Bool("build", false, "") set.Bool("generate", false, "") - set.String("path", "", "") + set.String("path", wdir(), "") c := cli.NewContext(nil, set, nil) - set.Parse([]string{"--path=test_path", "--fmt", "--install", "--run", "--build", "--generate", "--test", "--vet"}) + set.Parse([]string{"--fmt", "--install", "--run", "--build", "--generate", "--test", "--vet"}) r.add(c) expected := Project{ - Name: "test_path", - Path: "test_path", + Name: filepath.Base(wdir()), + Path: wdir(), Cmds: Cmds{ Fmt: Cmd{ Status: true, @@ -93,7 +81,7 @@ func TestRealize_Add(t *testing.T) { }, Watcher: Watch{ Paths: []string{"/"}, - Ignore: []string{"vendor"}, + Ignore: []string{".git",".realize","vendor"}, Exts: []string{"go"}, }, } diff --git a/realize_test.go b/realize_test.go index f97ab25..f22439b 100644 --- a/realize_test.go +++ b/realize_test.go @@ -9,7 +9,7 @@ import ( func TestPrefix(t *testing.T) { input := random(10) - value := fmt.Sprint(yellow.bold("[")+"REALIZE"+yellow.bold("]"), input) + value := fmt.Sprint(yellow.bold("["), "REALIZE", yellow.bold("]"), " : ", input) result := prefix(input) if result == "" { t.Fatal("Expected a string") @@ -28,7 +28,6 @@ func TestBefore(t *testing.T) { func TestNew(t *testing.T) { r := new() - t.Log(reflect.TypeOf(r).String()) if reflect.TypeOf(r).String() != "main.realize" { t.Error("Expected a realize struct") } diff --git a/settings_test.go b/settings_test.go index b9092b5..cfe04e2 100644 --- a/settings_test.go +++ b/settings_test.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "os" "path/filepath" - "strings" "testing" ) @@ -100,18 +99,6 @@ func TestSettings_Record(t *testing.T) { s.del(filepath.Join(directory, s.file)) } -func TestSettings_Wdir(t *testing.T) { - s := Settings{} - expected, err := os.Getwd() - if err != nil { - t.Error(err) - } - result := s.wdir() - if result != filepath.Base(expected) { - t.Error("Expected", filepath.Base(expected), "instead", result) - } -} - func TestSettings_Validate(t *testing.T) { s := Settings{} input := errors.New("") @@ -121,28 +108,7 @@ func TestSettings_Validate(t *testing.T) { } } -func TestSettings_Name(t *testing.T) { +func TestSettings_Fatal(t *testing.T){ s := Settings{} - name := random(8) - path := random(5) - dir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - result := s.name(name, path) - if result != dir && result != filepath.Base(path) { - t.Fatal("Expected", dir, "or", filepath.Base(path), "instead", result) - } - -} - -func TestSettings_Path(t *testing.T) { - s := Settings{} - path := random(5) - expected := strings.Replace(filepath.Clean(path), "\\", "/", -1) - result := s.path(path) - if result != expected { - t.Fatal("Expected", expected, "instead", result) - } - -} + s.fatal(nil,"test") +} \ No newline at end of file diff --git a/utils_test.go b/utils_test.go index 728794a..84f6162 100644 --- a/utils_test.go +++ b/utils_test.go @@ -53,10 +53,13 @@ func TestInArray(t *testing.T) { } } -func TestGetEnvPath(t *testing.T) { - expected := filepath.SplitList(os.Getenv("GOPATH"))[0] - result := getEnvPath("GOPATH") - if expected != result { - t.Fatal("Expected", expected, "instead", result) +func TestWdir(t *testing.T) { + expected, err := os.Getwd() + if err != nil { + t.Error(err) + } + result := wdir() + if result != expected { + t.Error("Expected", filepath.Base(expected), "instead", result) } } diff --git a/watcher_test.go b/watcher_test.go index 394d1b2..223711f 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -29,7 +29,7 @@ func TestWalk(t *testing.T) { Ignore: []string{"vendor"}, Exts: []string{"go"}, }, - base: "/go/project", + Path: "/go/project", watcher: &fileWatcherMock{}, init: true, } From 725783831f34446b3f7702326e530e986f5eb2b8 Mon Sep 17 00:00:00 2001 From: asoseil Date: Mon, 23 Oct 2017 00:23:24 +0200 Subject: [PATCH 07/12] run fixed when a build isn't found --- exec.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/exec.go b/exec.go index 1e3d88e..ab27fef 100644 --- a/exec.go +++ b/exec.go @@ -12,6 +12,7 @@ import ( "strings" "sync" "time" + "github.com/pkg/errors" ) // GoCompile is used for compile a project @@ -84,10 +85,11 @@ func (p *Project) goRun(stop <-chan bool, runner chan bool) { } else if _, err = os.Stat(path + extWindows); err == nil { build = exec.Command(path+extWindows, args...) } else { - p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Can't run a not compiled project"}) - p.fatal(nil, "Can't run a not compiled project", ":") + p.err(errors.New("Build not found")) + return } } + defer func() { if err := build.Process.Kill(); err != nil { p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Failed to stop: " + err.Error()}) From 61d9efe90f378a386202d8ec7775e76f8ef79d5d Mon Sep 17 00:00:00 2001 From: asoseil Date: Mon, 23 Oct 2017 00:35:45 +0200 Subject: [PATCH 08/12] double path field fixed --- cmd.go | 12 ++++++------ exec.go | 12 ++++++------ watcher.go | 12 ++++++------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cmd.go b/cmd.go index 958a69b..414eeaf 100644 --- a/cmd.go +++ b/cmd.go @@ -7,6 +7,7 @@ import ( "path/filepath" "time" "reflect" + "fmt" ) // Tool options customizable, should be moved in Cmd @@ -117,10 +118,11 @@ func (r *realize) run(p *cli.Context) error { continue } // validate project path, if invalid get wdir or clean current - if !filepath.IsAbs(elm.path){ - r.Schema[k].path = wdir() + if !filepath.IsAbs(elm.Path){ + r.Schema[k].Path = wdir() }else{ - r.Schema[k].path = filepath.Clean(elm.path) + r.Schema[k].Path = filepath.Clean(elm.Path) + fmt.Println(r.Schema[k].Path ) } // env variables for key, item := range r.Schema[k].Environment { @@ -129,7 +131,7 @@ func (r *realize) run(p *cli.Context) error { } } // get basepath name - r.Schema[k].name = filepath.Base(r.Schema[k].path) + r.Schema[k].name = filepath.Base(r.Schema[k].Path) fields := reflect.Indirect(reflect.ValueOf(&r.Schema[k].Cmds)) // Loop struct Cmds fields @@ -210,9 +212,7 @@ func (r *realize) run(p *cli.Context) error { startTxt: "Building...", endTxt: "Built", } - r.Schema[k].parent = r - r.Schema[k].path = r.Schema[k].Path match = true go r.Schema[k].watch() diff --git a/exec.go b/exec.go index ab27fef..fb67cf7 100644 --- a/exec.go +++ b/exec.go @@ -22,7 +22,7 @@ func (p *Project) goCompile(stop <-chan bool, method []string, args []string) (s done := make(chan error) args = append(method, args...) cmd := exec.Command(args[0], args[1:]...) - cmd.Dir = p.path + cmd.Dir = p.Path cmd.Stdout = &out cmd.Stderr = &stderr // Start command @@ -79,7 +79,7 @@ func (p *Project) goRun(stop <-chan bool, runner chan bool) { } else if _, err := os.Stat(path + extWindows); err == nil { build = exec.Command(path+extWindows, args...) } else { - path := filepath.Join(p.path, p.name) + path := filepath.Join(p.Path, p.name) if _, err = os.Stat(path); err == nil { build = exec.Command(path, args...) } else if _, err = os.Stat(path + extWindows); err == nil { @@ -150,13 +150,13 @@ func (p *Project) command(stop <-chan bool, cmd Command) (string, string) { done := make(chan error) args := strings.Split(strings.Replace(strings.Replace(cmd.Command, "'", "", -1), "\"", "", -1), " ") exec := exec.Command(args[0], args[1:]...) - exec.Dir = p.path + exec.Dir = p.Path // make cmd path if cmd.Path != "" { - if strings.Contains(cmd.Path, p.path) { + if strings.Contains(cmd.Path, p.Path) { exec.Dir = cmd.Path } else { - exec.Dir = filepath.Join(p.path, cmd.Path) + exec.Dir = filepath.Join(p.Path, cmd.Path) } } exec.Stdout = &stdout @@ -189,7 +189,7 @@ func (p *Project) goTool(wg *sync.WaitGroup, stop <-chan bool, result chan<- too if strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "") { if strings.HasSuffix(path, ".go") { tool.options = append(tool.options, path) - path = p.path + path = p.Path } if s := ext(path); s == "" || s == "go" { var out, stderr bytes.Buffer diff --git a/watcher.go b/watcher.go index dd161ea..e2dc395 100644 --- a/watcher.go +++ b/watcher.go @@ -55,7 +55,7 @@ type Project struct { watcher FileWatcher init bool files, folders int64 - name, path, lastFile string + name, lastFile string tools []tool paths []string lastTime time.Time @@ -98,7 +98,7 @@ func (p *Project) watch() { p.cmd(stop, "before", true) // indexing files and dirs for _, dir := range p.Watcher.Paths { - base := filepath.Join(p.path, dir) + base := filepath.Join(p.Path, dir) if _, err := os.Stat(base); err == nil { if err := filepath.Walk(base, p.walk); err == nil { p.tool(stop, base) @@ -312,7 +312,7 @@ func (p *Project) changed(event fsnotify.Event, stop chan bool) { // Watch the files tree of a project func (p *Project) walk(path string, info os.FileInfo, err error) error { for _, v := range p.Watcher.Ignore { - s := append([]string{p.path},strings.Split(v,string(os.PathSeparator))...) + s := append([]string{p.Path},strings.Split(v,string(os.PathSeparator))...) if strings.Contains(path, filepath.Join(s...)) { return nil } @@ -339,7 +339,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { case "out": p.Buffer.StdOut = append(p.Buffer.StdOut, o) if p.Files.Outputs.Status { - f := p.create(p.path, p.Files.Outputs.Name) + f := p.create(p.Path, p.Files.Outputs.Name) t := time.Now() s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"} if _, err := f.WriteString(strings.Join(s, " ")); err != nil { @@ -349,7 +349,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { case "log": p.Buffer.StdLog = append(p.Buffer.StdLog, o) if p.Files.Logs.Status { - f := p.create(p.path, p.Files.Logs.Name) + f := p.create(p.Path, p.Files.Logs.Name) t := time.Now() s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"} if stream != "" { @@ -362,7 +362,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { case "error": p.Buffer.StdErr = append(p.Buffer.StdErr, o) if p.Files.Errors.Status { - f := p.create(p.path, p.Files.Errors.Name) + f := p.create(p.Path, p.Files.Errors.Name) t := time.Now() s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Type, o.Text, o.Path, "\r\n"} if stream != "" { From fd19cc5faf3967979b5b9fa975577b8e6381ba9c Mon Sep 17 00:00:00 2001 From: asoseil Date: Mon, 23 Oct 2017 14:32:42 +0200 Subject: [PATCH 09/12] version command --- realize.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/realize.go b/realize.go index f12df99..45a7722 100644 --- a/realize.go +++ b/realize.go @@ -14,7 +14,7 @@ import ( ) const ( - version = "1.5.0" + version = "1.5.1" ) // New realize instance @@ -111,7 +111,7 @@ func main() { { Name: "init", Category: "Configuration", - Aliases: []string{"a"}, + Aliases: []string{"i"}, Description: "Define a new config file with all options step by step", Action: func(p *cli.Context) (actErr error) { interact.Run(&interact.Interact{ @@ -1085,6 +1085,16 @@ func main() { }, Before: before, }, + { + Name: "version", + Aliases: []string{"v"}, + Description: "Realize version", + Action: func(p *cli.Context) error { + log.Println(prefix(green.bold(version))) + return nil + }, + Before: before, + }, }, } if err := app.Run(os.Args); err != nil { From b543439be978b95847283219419762931b865192 Mon Sep 17 00:00:00 2001 From: asoseil Date: Mon, 23 Oct 2017 21:51:17 +0200 Subject: [PATCH 10/12] fmt removed --- cmd.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd.go b/cmd.go index 414eeaf..691e52a 100644 --- a/cmd.go +++ b/cmd.go @@ -122,7 +122,6 @@ func (r *realize) run(p *cli.Context) error { r.Schema[k].Path = wdir() }else{ r.Schema[k].Path = filepath.Clean(elm.Path) - fmt.Println(r.Schema[k].Path ) } // env variables for key, item := range r.Schema[k].Environment { From b06a6f310ff35525d5f4b947cb35964615e1e2c0 Mon Sep 17 00:00:00 2001 From: asoseil Date: Mon, 23 Oct 2017 22:35:37 +0200 Subject: [PATCH 11/12] import fixed --- cmd.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd.go b/cmd.go index 691e52a..15bcacb 100644 --- a/cmd.go +++ b/cmd.go @@ -7,7 +7,6 @@ import ( "path/filepath" "time" "reflect" - "fmt" ) // Tool options customizable, should be moved in Cmd From 65a386eae78fc9559fbf3c6104079b46217e3eb8 Mon Sep 17 00:00:00 2001 From: mingrammer Date: Tue, 24 Oct 2017 22:10:54 +0900 Subject: [PATCH 12/12] Supports using the default GOPATH --- realize.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/realize.go b/realize.go index 4bd957f..1544f43 100644 --- a/realize.go +++ b/realize.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/fatih/color" "github.com/tockins/interact" + "go/build" "gopkg.in/urfave/cli.v2" "log" "os" @@ -1126,10 +1127,13 @@ func before(*cli.Context) error { log.SetFlags(0) log.SetOutput(logWriter{}) // Before of every exec of a cli method - gopath := os.Getenv("GOPATH") + gopath := build.Default.GOPATH if gopath == "" { return errors.New("$GOPATH isn't set properly") } + if err := os.Setenv("GOPATH", gopath); err != nil { + return err + } // new realize instance r = new() // read if exist