handling errors
This commit is contained in:
parent
c86bb98927
commit
6dfbb8cb89
@ -29,7 +29,7 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
|
|||||||
}
|
}
|
||||||
p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Ended"})
|
p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Ended"})
|
||||||
log.Println(p.pname(p.Name, 2), ":", p.Red.Regular("Ended"))
|
log.Println(p.pname(p.Name, 2), ":", p.Red.Regular("Ended"))
|
||||||
go p.sync()
|
p.sync()
|
||||||
wr.Done()
|
wr.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -54,16 +54,16 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
|
|||||||
select {
|
select {
|
||||||
default:
|
default:
|
||||||
if isError {
|
if isError {
|
||||||
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: output.Text()})
|
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: output.Text(), Type: "Go Run"})
|
||||||
} else {
|
} else {
|
||||||
p.Buffer.StdOut = append(p.Buffer.StdOut, BufferOut{Time: time.Now(), Text: output.Text()})
|
p.Buffer.StdOut = append(p.Buffer.StdOut, BufferOut{Time: time.Now(), Text: output.Text()})
|
||||||
}
|
}
|
||||||
go p.sync()
|
p.sync()
|
||||||
if p.Cli.Streams {
|
if p.Cli.Streams {
|
||||||
log.Println(p.pname(p.Name, 3), ":", p.Blue.Regular(output.Text()))
|
log.Println(p.pname(p.Name, 3), ":", p.Blue.Regular(output.Text()))
|
||||||
}
|
}
|
||||||
if p.File.Streams {
|
if p.File.Streams {
|
||||||
path := filepath.Join(p.base, p.parent.Resources.Output)
|
path := filepath.Join(p.base, p.Resources.Output)
|
||||||
f := p.Create(path)
|
f := p.Create(path)
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + output.Text() + "\r\n"); err != nil {
|
if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + output.Text() + "\r\n"); err != nil {
|
||||||
@ -102,7 +102,6 @@ func (p *Project) goBuild() (string, error) {
|
|||||||
build.Stdout = &out
|
build.Stdout = &out
|
||||||
build.Stderr = &stderr
|
build.Stderr = &stderr
|
||||||
if err := build.Run(); err != nil {
|
if err := build.Run(); err != nil {
|
||||||
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error()})
|
|
||||||
return stderr.String(), err
|
return stderr.String(), err
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
@ -117,7 +116,6 @@ func (p *Project) goInstall() (string, error) {
|
|||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin"))
|
err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error()})
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
build := exec.Command("go", "install")
|
build := exec.Command("go", "install")
|
||||||
@ -125,7 +123,6 @@ func (p *Project) goInstall() (string, error) {
|
|||||||
build.Stdout = &out
|
build.Stdout = &out
|
||||||
build.Stderr = &stderr
|
build.Stderr = &stderr
|
||||||
if err := build.Run(); err != nil {
|
if err := build.Run(); err != nil {
|
||||||
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error()})
|
|
||||||
return stderr.String(), err
|
return stderr.String(), err
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
@ -171,14 +168,21 @@ func (p *Project) goGenerate(path string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cmds exec a list of defined commands
|
// Cmds exec a list of defined commands
|
||||||
func (p *Project) cmds(cmds []string) (errors []error) {
|
func (p *Project) cmds(cmds []string) (errors []string) {
|
||||||
|
defer func() {
|
||||||
|
p.sync()
|
||||||
|
}()
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
|
var out bytes.Buffer
|
||||||
|
var stderr bytes.Buffer
|
||||||
cmd := strings.Replace(strings.Replace(cmd, "'", "", -1), "\"", "", -1)
|
cmd := strings.Replace(strings.Replace(cmd, "'", "", -1), "\"", "", -1)
|
||||||
c := strings.Split(cmd, " ")
|
c := strings.Split(cmd, " ")
|
||||||
build := exec.Command(c[0], c[1:]...)
|
build := exec.Command(c[0], c[1:]...)
|
||||||
build.Dir = p.base
|
build.Dir = p.base
|
||||||
|
build.Stdout = &out
|
||||||
|
build.Stderr = &stderr
|
||||||
if err := build.Run(); err != nil {
|
if err := build.Run(); err != nil {
|
||||||
errors = append(errors, err)
|
errors = append(errors, stderr.String())
|
||||||
return errors
|
return errors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,8 +72,12 @@ type Buffer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type BufferOut struct {
|
type BufferOut struct {
|
||||||
Time time.Time
|
Time time.Time
|
||||||
Text string
|
Text string
|
||||||
|
Path string
|
||||||
|
Type string
|
||||||
|
Stream string
|
||||||
|
Errors []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the application
|
// Initialize the application
|
||||||
|
@ -61,7 +61,7 @@ func (p *Project) watching() {
|
|||||||
go func() {
|
go func() {
|
||||||
p.parent.Sync <- "sync"
|
p.parent.Sync <- "sync"
|
||||||
}()
|
}()
|
||||||
fmt.Println(p.pname(p.Name, 4), p.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), p.Magenta.Bold(file))
|
log.Println(p.pname(p.Name, 4), ":", p.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), p.Magenta.Bold(file))
|
||||||
// stop and run again
|
// stop and run again
|
||||||
if p.Run {
|
if p.Run {
|
||||||
close(channel)
|
close(channel)
|
||||||
@ -93,12 +93,15 @@ func (p *Project) install(channel chan bool, wr *sync.WaitGroup) {
|
|||||||
log.Println(p.pname(p.Name, 1), ":", "Installing..")
|
log.Println(p.pname(p.Name, 1), ":", "Installing..")
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
if stream, err := p.goInstall(); err != nil {
|
if stream, err := p.goInstall(); err != nil {
|
||||||
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Install", Stream: stream})
|
msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Install"), p.Red.Regular(err.Error()))
|
||||||
log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Install"), p.Red.Regular(err.Error()))
|
out := BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Install", Stream: stream}
|
||||||
fmt.Println(stream)
|
p.print("error", out, msg, stream)
|
||||||
wr.Done()
|
wr.Done()
|
||||||
} else {
|
} else {
|
||||||
log.Println(p.pname(p.Name, 5), ":", p.Green.Regular("Installed")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
msg := fmt.Sprintln(p.pname(p.Name, 5), ":", p.Green.Regular("Installed")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
||||||
|
out := BufferOut{Time: time.Now(), Text: "Installed"}
|
||||||
|
p.print("log", out, msg, stream)
|
||||||
|
|
||||||
if p.Run {
|
if p.Run {
|
||||||
runner := make(chan bool, 1)
|
runner := make(chan bool, 1)
|
||||||
log.Println(p.pname(p.Name, 1), ":", "Running..")
|
log.Println(p.pname(p.Name, 1), ":", "Running..")
|
||||||
@ -107,7 +110,9 @@ func (p *Project) install(channel chan bool, wr *sync.WaitGroup) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-runner:
|
case <-runner:
|
||||||
log.Println(p.pname(p.Name, 5), ":", p.Green.Regular("Has been run")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
msg := fmt.Sprintln(p.pname(p.Name, 5), ":", p.Green.Regular("Has been run")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
||||||
|
out := BufferOut{Time: time.Now(), Text: "Has been run"}
|
||||||
|
p.print("log", out, msg, stream)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,11 +131,13 @@ func (p *Project) build() {
|
|||||||
log.Println(p.pname(p.Name, 1), ":", "Building..")
|
log.Println(p.pname(p.Name, 1), ":", "Building..")
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
if stream, err := p.goBuild(); err != nil {
|
if stream, err := p.goBuild(); err != nil {
|
||||||
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Build", Stream: stream})
|
msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Build"), p.Red.Regular(err.Error()))
|
||||||
log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Build"), p.Red.Regular(err.Error()))
|
out := BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Build", Stream: stream}
|
||||||
fmt.Println(stream)
|
p.print("error", out, msg, stream)
|
||||||
} else {
|
} else {
|
||||||
log.Println(p.pname(p.Name, 5), ":", p.Green.Regular("Builded")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
msg := fmt.Sprintln(p.pname(p.Name, 5), ":", p.Green.Regular("Builded")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
||||||
|
out := BufferOut{Time: time.Now(), Text: "Builded"}
|
||||||
|
p.print("log", out, msg, stream)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -143,9 +150,9 @@ func (p *Project) fmt(path string) error {
|
|||||||
}()
|
}()
|
||||||
if p.Fmt {
|
if p.Fmt {
|
||||||
if stream, err := p.goFmt(path); err != nil {
|
if stream, err := p.goFmt(path); err != nil {
|
||||||
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Fmt", Stream: stream})
|
msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Fmt"), p.Red.Regular("there are some errors in"), ":", p.Magenta.Bold(path))
|
||||||
log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Fmt"), p.Red.Regular("there are some errors in"), ":", p.Magenta.Bold(path))
|
out := BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Fmt", Stream: stream}
|
||||||
fmt.Println(stream)
|
p.print("error", out, msg, stream)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,9 +166,9 @@ func (p *Project) generate(path string) error {
|
|||||||
}()
|
}()
|
||||||
if p.Generate {
|
if p.Generate {
|
||||||
if stream, err := p.goGenerate(path); err != nil {
|
if stream, err := p.goGenerate(path); err != nil {
|
||||||
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Generate", Stream: stream})
|
msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Generate"), p.Red.Regular("there are some errors in"), ":", p.Magenta.Bold(path))
|
||||||
log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Generate"), p.Red.Regular("there are some errors in"), ":", p.Magenta.Bold(path))
|
out := BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Generate", Stream: stream}
|
||||||
fmt.Println(stream)
|
p.print("error", out, msg, stream)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -175,8 +182,9 @@ func (p *Project) cmd(exit chan bool) {
|
|||||||
cast := func(commands []string) {
|
cast := func(commands []string) {
|
||||||
if errs := p.cmds(commands); errs != nil {
|
if errs := p.cmds(commands); errs != nil {
|
||||||
for _, err := range errs {
|
for _, err := range errs {
|
||||||
|
msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold(err))
|
||||||
log.Println(p.pname(p.Name, 2), p.Red.Bold(err))
|
out := BufferOut{Time: time.Now(), Text: err, Type: "After/Before"}
|
||||||
|
p.print("error", out, msg, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -205,9 +213,9 @@ func (p *Project) test(path string) error {
|
|||||||
}()
|
}()
|
||||||
if p.Test {
|
if p.Test {
|
||||||
if stream, err := p.goTest(path); err != nil {
|
if stream, err := p.goTest(path); err != nil {
|
||||||
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Test", Stream: stream})
|
msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Test"), p.Red.Regular("there are some errors in "), ":", p.Magenta.Bold(path))
|
||||||
log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Test"), p.Red.Regular("there are some errors in "), ":", p.Magenta.Bold(path))
|
out := BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Test", Stream: stream}
|
||||||
fmt.Println(stream)
|
p.print("error", out, msg, stream)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -222,7 +230,7 @@ func (p *Project) walks(watcher *fsnotify.Watcher) error {
|
|||||||
if !p.ignore(path) {
|
if !p.ignore(path) {
|
||||||
if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.HasPrefix(path, ".")) && !strings.Contains(path, "/.") || (inArray(filepath.Ext(path), p.Watcher.Exts)) {
|
if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.HasPrefix(path, ".")) && !strings.Contains(path, "/.") || (inArray(filepath.Ext(path), p.Watcher.Exts)) {
|
||||||
if p.Watcher.Preview {
|
if p.Watcher.Preview {
|
||||||
fmt.Println(p.pname(p.Name, 1), ":", path)
|
log.Println(p.pname(p.Name, 1), ":", path)
|
||||||
}
|
}
|
||||||
if err = watcher.Add(path); err != nil {
|
if err = watcher.Add(path); err != nil {
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
@ -258,7 +266,7 @@ func (p *Project) walks(watcher *fsnotify.Watcher) error {
|
|||||||
return errors.New(base + " path doesn't exist")
|
return errors.New(base + " path doesn't exist")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println(p.pname(p.Name, 1), p.Red.Bold("Watching"), p.Magenta.Bold(files), "file/s", p.Magenta.Bold(folders), "folder/s")
|
log.Println(p.pname(p.Name, 1), ":", p.Blue.Bold("Watching"), p.Magenta.Bold(files), "file/s", p.Magenta.Bold(folders), "folder/s")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,3 +309,43 @@ func (p *Project) pname(name string, color int) string {
|
|||||||
}
|
}
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Project) print(t string, o BufferOut, msg string, stream string) {
|
||||||
|
switch t {
|
||||||
|
case "out":
|
||||||
|
p.Buffer.StdOut = append(p.Buffer.StdOut, o)
|
||||||
|
if p.File.Streams {
|
||||||
|
path := filepath.Join(p.base, p.Resources.Output)
|
||||||
|
f := p.Create(path)
|
||||||
|
t := time.Now()
|
||||||
|
if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + o.Text + "\r\n"); err != nil {
|
||||||
|
p.Fatal("", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "log":
|
||||||
|
p.Buffer.StdLog = append(p.Buffer.StdLog, o)
|
||||||
|
if p.File.Logs {
|
||||||
|
path := filepath.Join(p.base, p.Resources.Log)
|
||||||
|
f := p.Create(path)
|
||||||
|
t := time.Now()
|
||||||
|
if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + o.Text + "\r\n"); err != nil {
|
||||||
|
p.Fatal("", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "error":
|
||||||
|
p.Buffer.StdErr = append(p.Buffer.StdErr, o)
|
||||||
|
if p.File.Errors {
|
||||||
|
path := filepath.Join(p.base, p.Resources.Log)
|
||||||
|
f := p.Create(path)
|
||||||
|
t := time.Now()
|
||||||
|
if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + o.Text + "\r\n"); err != nil {
|
||||||
|
p.Fatal("", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
log.Print(msg)
|
||||||
|
if stream != "" {
|
||||||
|
fmt.Println(stream)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user