minor bugs fixed

This commit is contained in:
asoseil 2017-11-22 15:31:34 +01:00
parent c286bd230d
commit 9642f4a2b6
5 changed files with 56 additions and 65 deletions

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -1 +0,0 @@
package realize

View File

@ -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