methods log/print removed

This commit is contained in:
alessio 2016-08-21 00:29:32 +02:00
parent c74415dd80
commit 7bf980913a
5 changed files with 49 additions and 80 deletions

View File

@ -4,6 +4,7 @@ import (
r "github.com/tockins/realize/realize" r "github.com/tockins/realize/realize"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"os" "os"
"log"
) )
func main() { func main() {
@ -12,7 +13,7 @@ func main() {
handle := func(err error) error { handle := func(err error) error {
if err != nil { if err != nil {
r.Fail(err.Error()) log.Println(r.Red(err.Error()))
return nil return nil
} }
return nil return nil

View File

@ -3,7 +3,6 @@ package realize
import ( import (
"fmt" "fmt"
"github.com/fatih/color" "github.com/fatih/color"
"log"
"sync" "sync"
) )
@ -17,11 +16,13 @@ const (
) )
var wg sync.WaitGroup var wg sync.WaitGroup
var green = color.New(color.FgGreen, color.Bold).SprintFunc() var Green = color.New(color.FgGreen, color.Bold).SprintFunc()
var greenl = color.New(color.FgHiGreen).SprintFunc() var Greenl = color.New(color.FgGreen).SprintFunc()
var red = color.New(color.FgRed, color.Bold).SprintFunc() var Red = color.New(color.FgRed, color.Bold).SprintFunc()
var blue = color.New(color.FgBlue, color.Bold).SprintFunc() var Redl = color.New(color.FgRed).SprintFunc()
var bluel = color.New(color.FgBlue).SprintFunc() var Blue = color.New(color.FgBlue, color.Bold).SprintFunc()
var Bluel = color.New(color.FgBlue).SprintFunc()
var Magenta = color.New(color.FgMagenta).SprintFunc()
var watcherIgnores = []string{"vendor", "bin"} var watcherIgnores = []string{"vendor", "bin"}
var watcherExts = []string{".go"} var watcherExts = []string{".go"}
@ -43,43 +44,8 @@ func Init() *App {
} }
} }
// Fail is a red message, generally used for errors
func Fail(msg ...interface{}) {
color.Set(color.FgRed, color.Bold)
fmt.Println(msg...)
color.Unset()
}
// Success is a green message, generally used for feedback
func Success(msg ...interface{}) {
color.Set(color.FgGreen, color.Bold)
fmt.Println(msg...)
color.Unset()
}
// LogSuccess is a green log message, generally used for feedback
func LogSuccess(msg ...interface{}) {
color.Set(color.FgGreen, color.Bold)
log.Println(msg...)
color.Unset()
}
// LogFail is a red log message, generally used for errors
func LogFail(msg ...interface{}) {
color.Set(color.FgRed, color.Bold)
log.Println(msg...)
color.Unset()
}
// LogWatch is a blue log message used only for watcher outputs
func LogWatch(msg ...interface{}) {
color.Set(color.FgBlue, color.Bold)
log.Println(msg...)
color.Unset()
}
// Information print realize name and description // Information print realize name and description
func (app *App) Information() { func (app *App) Information() {
fmt.Println(blue(app.Name) + " - " + blue(app.Version)) fmt.Println(Blue(app.Name) + " - " + Blue(app.Version))
fmt.Println(bluel(app.Description) + "\n") fmt.Println(Bluel(app.Description) + "\n")
} }

View File

@ -42,7 +42,7 @@ func New(params *cli.Context) *Config {
func Duplicates(value Project, arr []Project) bool { func Duplicates(value Project, arr []Project) bool {
for _, val := range arr { for _, val := range arr {
if value.Path == val.Path || value.Name == val.Name { if value.Path == val.Path || value.Name == val.Name {
Fail("There is a duplicate of '"+val.Name+"'. Check your config file!") fmt.Println(Red("There is a duplicate of '"+val.Name+"'. Check your config file!"))
return true return true
} }
} }
@ -92,7 +92,7 @@ func (h *Config) Create(params *cli.Context) error {
if err != nil { if err != nil {
os.Remove(h.file) os.Remove(h.file)
} else { } else {
Success("The config file was successfully created") fmt.Println(Green("The config file was successfully created"))
} }
return err return err
} }
@ -121,7 +121,7 @@ func (h *Config) Add(params *cli.Context) error {
h.Projects = append(h.Projects, new) h.Projects = append(h.Projects, new)
err = h.Write() err = h.Write()
if err == nil { if err == nil {
Success("Your project was successfully added") fmt.Println(Green("Your project was successfully added"))
} }
} }
return err return err
@ -136,7 +136,7 @@ func (h *Config) Remove(params *cli.Context) error {
h.Projects = append(h.Projects[:key], h.Projects[key+1:]...) h.Projects = append(h.Projects[:key], h.Projects[key+1:]...)
err = h.Write() err = h.Write()
if err == nil { if err == nil {
Success("Your project was successfully removed") fmt.Println(Green("Your project was successfully removed"))
} }
return err return err
} }
@ -151,18 +151,18 @@ func (h *Config) List() error {
err := h.Read() err := h.Read()
if err == nil { if err == nil {
for _, val := range h.Projects { for _, val := range h.Projects {
fmt.Println(green("|"), green(val.Name)) fmt.Println(Green("|"), Green(val.Name))
fmt.Println(greenl("|"), "\t", green("Base Path:"), red(val.Path)) fmt.Println(Magenta("|"), "\t", Green("Base Path"), ":", Magenta(val.Path))
fmt.Println(greenl("|"), "\t", green("Run:"), red(val.Run)) fmt.Println(Magenta("|"), "\t", Green("Run"), ":", Magenta(val.Run))
fmt.Println(greenl("|"), "\t", green("Build:"), red(val.Build)) fmt.Println(Magenta("|"), "\t", Green("Build"),":", Magenta(val.Build))
fmt.Println(greenl("|"), "\t", green("Install:"), red(val.Bin)) fmt.Println(Magenta("|"), "\t", Green("Install"), ":", Magenta(val.Bin))
fmt.Println(greenl("|"), "\t", green("Watcher:")) fmt.Println(Magenta("|"), "\t", Green("Watcher"),":")
fmt.Println(greenl("|"), "\t\t", green("After:"), red(val.Watcher.After)) fmt.Println(Magenta("|"), "\t\t", Green("After"), ":", Magenta(val.Watcher.After))
fmt.Println(greenl("|"), "\t\t", green("Before:"), red(val.Watcher.Before)) fmt.Println(Magenta("|"), "\t\t", Green("Before"), ":", Magenta(val.Watcher.Before))
fmt.Println(greenl("|"), "\t\t", green("Extensions:"), red(val.Watcher.Exts)) fmt.Println(Magenta("|"), "\t\t", Green("Extensions"), ":", Magenta(val.Watcher.Exts))
fmt.Println(greenl("|"), "\t\t", green("Paths:"), red(val.Watcher.Paths)) fmt.Println(Magenta("|"), "\t\t", Green("Paths"), ":", Magenta(val.Watcher.Paths))
fmt.Println(greenl("|"), "\t\t", green("Paths ignored:"), red(val.Watcher.Ignore)) fmt.Println(Magenta("|"), "\t\t", Green("Paths ignored"), ":", Magenta(val.Watcher.Ignore))
fmt.Println(greenl("|"), "\t\t", green("Watch preview:"), red(val.Watcher.Preview)) fmt.Println(Magenta("|"), "\t\t", Green("Watch preview"), ":", Magenta(val.Watcher.Preview))
} }
return nil return nil
} }

View File

@ -42,17 +42,17 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
if err := build.Process.Kill(); err != nil { if err := build.Process.Kill(); err != nil {
log.Fatal("failed to stop: ", err) log.Fatal("failed to stop: ", err)
} }
LogFail(p.Name + ": Stopped") log.Println(Redl(p.Name),":", Red("Stopped"))
wr.Done() wr.Done()
}() }()
stdout, err := build.StdoutPipe() stdout, err := build.StdoutPipe()
if err != nil { if err != nil {
Fail(err.Error()) log.Println(Red(err.Error()))
return err return err
} }
if err := build.Start(); err != nil { if err := build.Start(); err != nil {
Fail(err.Error()) log.Println(Red(err.Error()))
return err return err
} }
close(runner) close(runner)
@ -62,7 +62,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
for in.Scan() { for in.Scan() {
select { select {
default: default:
log.Println(bluel(p.Name+" Out:"), bluel(in.Text())) log.Println(Bluel(p.Name+" Out:"), Bluel(in.Text()))
} }
} }
close(stop) close(stop)
@ -81,7 +81,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
// GoBuild an implementation of the "go build" // GoBuild an implementation of the "go build"
func (p *Project) GoBuild() error { func (p *Project) GoBuild() error {
var out bytes.Buffer var out bytes.Buffer
build := exec.Command("go", "build") build := exec.Command("go", "build")
build.Dir = p.base build.Dir = p.base
build.Stdout = &out build.Stdout = &out

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"log"
) )
// The Watcher struct defines the livereload's logic // The Watcher struct defines the livereload's logic
@ -44,12 +45,12 @@ func (p *Project) Watching() {
var watcher *fsnotify.Watcher var watcher *fsnotify.Watcher
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
Fail(p.Name + ": \t" + err.Error()) log.Println(Redl(p.Name),": \t", Red(err.Error()))
} }
channel := make(chan bool, 1) channel := make(chan bool, 1)
base, err := os.Getwd() base, err := os.Getwd()
if err != nil { if err != nil {
Fail(p.Name + ": \t" + err.Error()) log.Println(Redl(p.Name),": \t", Red(err.Error()))
} }
walk := func(path string, info os.FileInfo, err error) error { walk := func(path string, info os.FileInfo, err error) error {
@ -86,14 +87,14 @@ func (p *Project) Watching() {
base = p.base + dir base = p.base + dir
if _, err := os.Stat(base); err == nil { if _, err := os.Stat(base); err == nil {
if err := filepath.Walk(base, walk); err != nil { if err := filepath.Walk(base, walk); err != nil {
Fail(err.Error()) log.Println(Red(err.Error()))
} }
} else { } else {
Fail(p.Name + ": \t" + base + " path doesn't exist") fmt.Println(Redl(p.Name), ":\t", Red(base + " path doesn't exist"))
} }
} }
routines() routines()
fmt.Println(red("Watching: '" + p.Name + "'\n")) fmt.Println(Red("Watching: '" + p.Name + "'\n"))
p.reload = time.Now().Truncate(time.Second) p.reload = time.Now().Truncate(time.Second)
for { for {
select { select {
@ -105,7 +106,7 @@ func (p *Project) Watching() {
if _, err := os.Stat(event.Name); err == nil { if _, err := os.Stat(event.Name); err == nil {
i := strings.Index(event.Name, filepath.Ext(event.Name)) i := strings.Index(event.Name, filepath.Ext(event.Name))
if event.Name[:i] != "" { if event.Name[:i] != "" {
LogWatch(p.Name+":", "File changed ->", event.Name[:i]) log.Println(Magenta(p.Name),":", Magenta("File changed"), "->", Blue(event.Name[:i]))
// stop and run again // stop and run again
close(channel) close(channel)
@ -117,7 +118,7 @@ func (p *Project) Watching() {
} }
} }
case err := <-watcher.Errors: case err := <-watcher.Errors:
Fail(err.Error()) log.Println(Red(err.Error()))
} }
} }
} }
@ -125,21 +126,22 @@ func (p *Project) Watching() {
// Install call an implementation of the "go install" // Install call an implementation of the "go install"
func (p *Project) install(channel chan bool,wr *sync.WaitGroup) { func (p *Project) install(channel chan bool,wr *sync.WaitGroup) {
if p.Bin { if p.Bin {
LogSuccess(p.Name + ": Installing..") log.Println(Greenl(p.Name), ":", Greenl("Installing.."))
start := time.Now() start := time.Now()
if err := p.GoInstall(); err != nil { if err := p.GoInstall(); err != nil {
Fail(p.Name + ": "+err.Error()) log.Println(Redl(p.Name),":",Red(err.Error()))
wr.Done() wr.Done()
} else { } else {
LogSuccess(p.Name+":", "Installed after", time.Since(start)) log.Println(Greenl(p.Name),":", Green("Installed")+ " after", Magenta(time.Since(start)))
if p.Run { if p.Run {
runner := make(chan bool, 1) runner := make(chan bool, 1)
LogSuccess(p.Name + ": Running..") log.Println(Greenl(p.Name), ":", Greenl("Running.."))
start = time.Now()
go p.GoRun(channel, runner, wr) go p.GoRun(channel, runner, wr)
for { for {
select { select {
case <-runner: case <-runner:
LogSuccess(p.Name + ": Has been run") log.Println(Greenl(p.Name), ":", Green("Has been run") + " after", Magenta(time.Since(start)))
return return
} }
} }
@ -152,12 +154,12 @@ func (p *Project) install(channel chan bool,wr *sync.WaitGroup) {
// Build call an implementation of the "go build" // Build call an implementation of the "go build"
func (p *Project) build() { func (p *Project) build() {
if p.Build { if p.Build {
LogSuccess(p.Name + ": Building..") log.Println(Greenl(p.Name), ":", Greenl("Building.."))
start := time.Now() start := time.Now()
if err := p.GoBuild(); err != nil { if err := p.GoBuild(); err != nil {
Fail(p.Name + ": "+err.Error()) log.Println(Redl(p.Name), ":", Red(err.Error()))
} else { } else {
LogSuccess(p.Name+":", "Builded after", time.Since(start)) log.Println(Greenl(p.Name),":", Green("Builded")+ " after", Magenta(time.Since(start)))
} }
return return
} }