diff --git a/realize/cli.go b/realize/cli.go index 1ecd51f..8f7c5ea 100644 --- a/realize/cli.go +++ b/realize/cli.go @@ -80,5 +80,5 @@ func (w LogWriter) Write(bytes []byte) (int, error) { if len(bytes) > 0 { return fmt.Fprint(Output, Yellow.Regular("["), time.Now().Format("15:04:05"), Yellow.Regular("]"), string(bytes)) } - return 0,nil + return 0, nil } diff --git a/realize/cli_test.go b/realize/cli_test.go index 4b700ce..7f4b498 100644 --- a/realize/cli_test.go +++ b/realize/cli_test.go @@ -1,12 +1,12 @@ package realize import ( + "bytes" + "log" "os" + "strings" "testing" "time" - "strings" - "log" - "bytes" ) func TestRealize_Stop(t *testing.T) { @@ -21,7 +21,7 @@ func TestRealize_Stop(t *testing.T) { func TestRealize_Start(t *testing.T) { r := Realize{} - go func(){ + go func() { time.Sleep(100) close(r.exit) _, ok := <-r.exit @@ -36,7 +36,7 @@ func TestRealize_Prefix(t *testing.T) { r := Realize{} input := "test" result := r.Prefix(input) - if len(result) <= 0 && !strings.Contains(result,input){ + if len(result) <= 0 && !strings.Contains(result, input) { t.Error("Unexpected error") } } @@ -47,7 +47,7 @@ func TestSettings_Read(t *testing.T) { w := LogWriter{} input := "" int, err := w.Write([]byte(input)) - if err != nil || int > 0{ - t.Error("Unexpected error", err, "string lenght should be 0 instead",int) + if err != nil || int > 0 { + t.Error("Unexpected error", err, "string lenght should be 0 instead", int) } -} \ No newline at end of file +} diff --git a/realize/commands.go b/realize/commands.go deleted file mode 100644 index 5b76dcb..0000000 --- a/realize/commands.go +++ /dev/null @@ -1,55 +0,0 @@ -package realize - -import ( - "bytes" - "github.com/go-siris/siris/core/errors" - "os/exec" - "path/filepath" - "strings" -) - -// Command options -type Command struct { - Type string `yaml:"type" json:"type"` - Cmd string `yaml:"command" json:"command"` - Path string `yaml:"path,omitempty" json:"path,omitempty"` - Global bool `yaml:"global,omitempty" json:"global,omitempty"` - Output bool `yaml:"output,omitempty" json:"output,omitempty"` -} - -// Exec an additional command from a defined path if specified -func (c *Command) Exec(base string, stop <-chan bool) (response Response) { - var stdout bytes.Buffer - var stderr bytes.Buffer - done := make(chan error) - args := strings.Split(strings.Replace(strings.Replace(c.Cmd, "'", "", -1), "\"", "", -1), " ") - ex := exec.Command(args[0], args[1:]...) - ex.Dir = base - // make cmd path - if c.Path != "" { - if strings.Contains(c.Path, base) { - ex.Dir = c.Path - } else { - ex.Dir = filepath.Join(base, c.Path) - } - } - ex.Stdout = &stdout - ex.Stderr = &stderr - // Start command - ex.Start() - go func() { done <- ex.Wait() }() - // Wait a result - select { - case <-stop: - // Stop running command - ex.Process.Kill() - case err := <-done: - // Command completed - response.Name = c.Cmd - response.Out = stdout.String() - if err != nil { - response.Err = errors.New(stderr.String()) - } - } - return -} diff --git a/realize/commands_test.go b/realize/commands_test.go deleted file mode 100644 index 4b7b005..0000000 --- a/realize/commands_test.go +++ /dev/null @@ -1 +0,0 @@ -package realize diff --git a/realize/projects.go b/realize/projects.go index 7ffb1b2..521ce17 100644 --- a/realize/projects.go +++ b/realize/projects.go @@ -30,6 +30,15 @@ type Watch struct { Scripts []Command `yaml:"scripts,omitempty" json:"scripts,omitempty"` } +// Command fields +type Command struct { + Type string `yaml:"type" json:"type"` + Cmd string `yaml:"command" json:"command"` + Path string `yaml:"path,omitempty" json:"path,omitempty"` + Global bool `yaml:"global,omitempty" json:"global,omitempty"` + Output bool `yaml:"output,omitempty" json:"output,omitempty"` +} + // Project info type Project struct { parent *Realize @@ -75,6 +84,7 @@ type BufferOut struct { Errors []string `json:"errors"` } +// Project interface type ProjectI interface { Setup() Watch(chan os.Signal) @@ -96,6 +106,43 @@ func (p *Project) Setup() { p.Tools.Setup() } +// Exec an additional command from a defined path if specified +func (c *Command) Exec(base string, stop <-chan bool) (response Response) { + var stdout bytes.Buffer + var stderr bytes.Buffer + done := make(chan error) + args := strings.Split(strings.Replace(strings.Replace(c.Cmd, "'", "", -1), "\"", "", -1), " ") + ex := exec.Command(args[0], args[1:]...) + ex.Dir = base + // make cmd path + if c.Path != "" { + if strings.Contains(c.Path, base) { + ex.Dir = c.Path + } else { + ex.Dir = filepath.Join(base, c.Path) + } + } + ex.Stdout = &stdout + ex.Stderr = &stderr + // Start command + ex.Start() + go func() { done <- ex.Wait() }() + // Wait a result + select { + case <-stop: + // Stop running command + ex.Process.Kill() + case err := <-done: + // Command completed + response.Name = c.Cmd + response.Out = stdout.String() + if err != nil { + response.Err = errors.New(stderr.String()) + } + } + return +} + // Watch a project func (p *Project) Watch(exit chan os.Signal) { var err error