This commit is contained in:
alessio 2016-08-21 15:38:00 +02:00
parent a5eb8b0251
commit 09873ae083
6 changed files with 43 additions and 43 deletions

View File

@ -3,9 +3,9 @@ package realize
import (
"fmt"
"github.com/fatih/color"
"log"
"sync"
"time"
"log"
)
const (
@ -32,14 +32,14 @@ var watcherIgnores = []string{"vendor", "bin"}
var watcherExts = []string{".go"}
var watcherPaths = []string{"/"}
type logWriter struct {}
type logWriter struct{}
// App struct contains the informations about realize
type App struct {
Name, Version, Description, Author, Email string
}
func init(){
func init() {
log.SetFlags(0)
log.SetOutput(new(logWriter))
}
@ -62,5 +62,5 @@ func (app *App) Information() {
}
func (writer logWriter) Write(bytes []byte) (int, error) {
return fmt.Print(YellowS("[")+time.Now().UTC().Format("15:04:05") +YellowS("]")+string(bytes))
return fmt.Print(YellowS("[") + time.Now().UTC().Format("15:04:05") + YellowS("]") + string(bytes))
}

View File

@ -1,14 +1,14 @@
package realize
import (
"testing"
"reflect"
"testing"
)
func TestInit(t *testing.T) {
actual := Init()
expected := &App{Name:AppName,Version:AppVersion,Description:AppDescription,Author:AppAuthor,Email:AppEmail}
if !reflect.DeepEqual(actual,expected) {
expected := &App{Name: AppName, Version: AppVersion, Description: AppDescription, Author: AppAuthor, Email: AppEmail}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Test failed, expected: '%s', got: '%s'", expected, actual)
}
}

View File

@ -18,18 +18,18 @@ type Config struct {
}
// nameParam check the project name presence. If empty takes the working directory name
func nameParam(params *cli.Context) string{
func nameParam(params *cli.Context) string {
var name string
if params.String("name") == "" {
name = params.String("base")
}else{
} else {
name = params.String("name")
}
return name
}
func boolParam(b bool) bool{
if b{
func boolParam(b bool) bool {
if b {
return false
}
return true
@ -61,7 +61,7 @@ func New(params *cli.Context) *Config {
func Duplicates(value Project, arr []Project) error {
for _, val := range arr {
if value.Path == val.Path || value.Name == val.Name {
return errors.New("There is a duplicate of '"+val.Name+"'. Check your config file!")
return errors.New("There is a duplicate of '" + val.Name + "'. Check your config file!")
}
}
return nil
@ -134,7 +134,7 @@ func (h *Config) Add(params *cli.Context) error {
return err
}
err = h.Create()
if err == nil{
if err == nil {
fmt.Println(Green("The config file was successfully created"))
}
return err
@ -167,9 +167,9 @@ func (h *Config) List() error {
fmt.Println(Blue("|"), Blue(strings.ToUpper(val.Name)))
fmt.Println(MagentaS("|"), "\t", Yellow("Base Path"), ":", MagentaS(val.Path))
fmt.Println(MagentaS("|"), "\t", Yellow("Run"), ":", MagentaS(val.Run))
fmt.Println(MagentaS("|"), "\t", Yellow("Build"),":", MagentaS(val.Build))
fmt.Println(MagentaS("|"), "\t", Yellow("Build"), ":", MagentaS(val.Build))
fmt.Println(MagentaS("|"), "\t", Yellow("Install"), ":", MagentaS(val.Bin))
fmt.Println(MagentaS("|"), "\t", Yellow("Watcher"),":")
fmt.Println(MagentaS("|"), "\t", Yellow("Watcher"), ":")
fmt.Println(MagentaS("|"), "\t\t", Yellow("After"), ":", MagentaS(val.Watcher.After))
fmt.Println(MagentaS("|"), "\t\t", Yellow("Before"), ":", MagentaS(val.Watcher.Before))
fmt.Println(MagentaS("|"), "\t\t", Yellow("Extensions"), ":", MagentaS(val.Watcher.Exts))

View File

@ -9,8 +9,8 @@ var context *cli.Context
func TestNew(t *testing.T) {
actual := New(context)
expected := &Config{file:AppFile,Version: AppVersion}
expected := &Config{file: AppFile, Version: AppVersion}
if actual == expected {
t.Errorf("Test failed, expected: '%s', got: '%s'",expected, actual)
t.Errorf("Test failed, expected: '%s', got: '%s'", expected, actual)
}
}
}

View File

@ -32,7 +32,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
if len(name) == 1 {
name := strings.Split(p.base, "/")
run = name[len(name)-1]
}else {
} else {
run = name[len(name)-1]
}
build := exec.Command(slash(os.Getenv("GOBIN")) + slash(run))
@ -41,7 +41,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
if err := build.Process.Kill(); err != nil {
log.Fatal("failed to stop: ", err)
}
log.Println(pname(p.Name,2),":", RedS("Stopped"))
log.Println(pname(p.Name, 2), ":", RedS("Stopped"))
wr.Done()
}()
@ -61,7 +61,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
for in.Scan() {
select {
default:
log.Println(pname(p.Name,3),":",BlueS(in.Text()))
log.Println(pname(p.Name, 3), ":", BlueS(in.Text()))
}
}
close(stop)
@ -96,7 +96,7 @@ func (p *Project) GoInstall() error {
base, _ := os.Getwd()
path := base + p.Path
err := os.Setenv("GOBIN",slash(os.Getenv("GOPATH")) + slash("bin"))
err := os.Setenv("GOBIN", slash(os.Getenv("GOPATH"))+slash("bin"))
if err != nil {
return err
}
@ -108,4 +108,4 @@ func (p *Project) GoInstall() error {
return err
}
return nil
}
}

View File

@ -3,13 +3,13 @@ package realize
import (
"fmt"
"github.com/fsnotify/fsnotify"
"log"
"math/big"
"os"
"path/filepath"
"strings"
"sync"
"time"
"log"
"math/big"
)
// The Watcher struct defines the livereload's logic
@ -46,12 +46,12 @@ func (p *Project) Watching() {
var watcher *fsnotify.Watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Println(strings.ToUpper(pname(p.Name, 1)),": \t", Red(err.Error()))
log.Println(strings.ToUpper(pname(p.Name, 1)), ": \t", Red(err.Error()))
}
channel := make(chan bool, 1)
base, err := os.Getwd()
if err != nil {
log.Println(pname(p.Name, 1),": \t", Red(err.Error()))
log.Println(pname(p.Name, 1), ": \t", Red(err.Error()))
}
walk := func(path string, info os.FileInfo, err error) error {
@ -71,7 +71,7 @@ func (p *Project) Watching() {
channel = make(chan bool)
wr.Add(1)
go p.build()
go p.install(channel,&wr)
go p.install(channel, &wr)
}
end := func() {
watcher.Close()
@ -91,7 +91,7 @@ func (p *Project) Watching() {
log.Println(Red(err.Error()))
}
} else {
fmt.Println(pname(p.Name, 1), ":\t", Red(base + " path doesn't exist"))
fmt.Println(pname(p.Name, 1), ":\t", Red(base+" path doesn't exist"))
}
}
routines()
@ -107,7 +107,7 @@ func (p *Project) Watching() {
if _, err := os.Stat(event.Name); err == nil {
i := strings.Index(event.Name, filepath.Ext(event.Name))
if event.Name[:i] != "" {
log.Println(pname(p.Name, 4),":", Magenta(event.Name[:i]))
log.Println(pname(p.Name, 4), ":", Magenta(event.Name[:i]))
// stop and run again
close(channel)
@ -125,24 +125,24 @@ func (p *Project) Watching() {
}
// 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 {
log.Println(pname(p.Name, 1), ":","Installing..")
log.Println(pname(p.Name, 1), ":", "Installing..")
start := time.Now()
if err := p.GoInstall(); err != nil {
log.Println(pname(p.Name, 1),":",Red(err.Error()))
log.Println(pname(p.Name, 1), ":", Red(err.Error()))
wr.Done()
} else {
log.Println(pname(p.Name, 5),":", Green("Installed")+ " after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
log.Println(pname(p.Name, 5), ":", Green("Installed")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
if p.Run {
runner := make(chan bool, 1)
log.Println(pname(p.Name, 1), ":","Running..")
log.Println(pname(p.Name, 1), ":", "Running..")
start = time.Now()
go p.GoRun(channel, runner, wr)
for {
select {
case <-runner:
log.Println(pname(p.Name, 5), ":", Green("Has been run") + " after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
log.Println(pname(p.Name, 5), ":", Green("Has been run")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
return
}
}
@ -160,7 +160,7 @@ func (p *Project) build() {
if err := p.GoBuild(); err != nil {
log.Println(pname(p.Name, 1), ":", Red(err.Error()))
} else {
log.Println(pname(p.Name, 5),":", Green("Builded")+ " after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
log.Println(pname(p.Name, 5), ":", Green("Builded")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
}
return
}
@ -206,22 +206,22 @@ func slash(str string) string {
}
// defines the colors scheme for the project name
func pname(name string, color int) string{
func pname(name string, color int) string {
switch color {
case 1:
name = Yellow("[")+strings.ToUpper(name)+Yellow("]")
name = Yellow("[") + strings.ToUpper(name) + Yellow("]")
break
case 2:
name = Yellow("[")+Red(strings.ToUpper(name))+Yellow("]")
name = Yellow("[") + Red(strings.ToUpper(name)) + Yellow("]")
break
case 3:
name = Yellow("[")+Blue(strings.ToUpper(name))+Yellow("]")
name = Yellow("[") + Blue(strings.ToUpper(name)) + Yellow("]")
break
case 4:
name = Yellow("[")+Magenta(strings.ToUpper(name))+Yellow("]")
name = Yellow("[") + Magenta(strings.ToUpper(name)) + Yellow("]")
break
case 5:
name = Yellow("[")+Green(strings.ToUpper(name))+Yellow("]")
name = Yellow("[") + Green(strings.ToUpper(name)) + Yellow("]")
break
}
return name