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

@ -1,12 +1,12 @@
package realize package realize
import ( import (
"bytes"
"log"
"os" "os"
"strings"
"testing" "testing"
"time" "time"
"strings"
"log"
"bytes"
) )
func TestRealize_Stop(t *testing.T) { func TestRealize_Stop(t *testing.T) {

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"` 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 // Project info
type Project struct { type Project struct {
parent *Realize parent *Realize
@ -75,6 +84,7 @@ type BufferOut struct {
Errors []string `json:"errors"` Errors []string `json:"errors"`
} }
// Project interface
type ProjectI interface { type ProjectI interface {
Setup() Setup()
Watch(chan os.Signal) Watch(chan os.Signal)
@ -96,6 +106,43 @@ func (p *Project) Setup() {
p.Tools.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 // Watch a project
func (p *Project) Watch(exit chan os.Signal) { func (p *Project) Watch(exit chan os.Signal) {
var err error var err error