From 199d5d95a78e6e39a8265e4d1656cc8c185b6963 Mon Sep 17 00:00:00 2001 From: alessio Date: Fri, 2 Sep 2016 00:17:19 +0200 Subject: [PATCH] code organization --- realize/blueprint.go => cli/cmd.go | 40 +++-------------- {realize => cli}/exec.go | 4 +- cli/main.go | 70 ++++++++++++++++++++++++++++++ {realize => cli}/utils.go | 4 +- {realize => cli}/watcher.go | 14 +----- main.go => realize.go | 21 ++++++++- realize/realize.go | 55 ----------------------- 7 files changed, 99 insertions(+), 109 deletions(-) rename realize/blueprint.go => cli/cmd.go (82%) rename {realize => cli}/exec.go (97%) create mode 100644 cli/main.go rename {realize => cli}/utils.go (98%) rename {realize => cli}/watcher.go (91%) rename main.go => realize.go (89%) delete mode 100644 realize/realize.go diff --git a/realize/blueprint.go b/cli/cmd.go similarity index 82% rename from realize/blueprint.go rename to cli/cmd.go index 3e18d94..4e40409 100644 --- a/realize/blueprint.go +++ b/cli/cmd.go @@ -1,4 +1,4 @@ -package realize +package cli import ( "errors" @@ -10,38 +10,8 @@ import ( "path/filepath" "strings" "syscall" - "time" ) -// App struct contains the informations about realize -type Realize struct { - Name, Description, Author, Email string - Version string - Limit uint64 - Blueprint Blueprint -} - -// Projects struct contains a projects list -type Blueprint struct { - Projects []Project `yaml:"Projects,omitempty"` - files map[string]string -} - -// Project defines the informations of a single project -type Project struct { - reload time.Time - base string - Name string `yaml:"app_name,omitempty"` - Path string `yaml:"app_path,omitempty"` - Run bool `yaml:"app_run,omitempty"` - Bin bool `yaml:"app_bin,omitempty"` - Build bool `yaml:"app_build,omitempty"` - Fmt bool `yaml:"app_fmt,omitempty"` - Test bool `yaml:"app_test,omitempty"` - Params []string `yaml:"app_params,omitempty"` - Watcher Watcher `yaml:"app_watcher,omitempty"` -} - // Wdir returns the name last element of the working directory path func (r *Realize) Wdir() string { dir, err := os.Getwd() @@ -51,8 +21,8 @@ func (r *Realize) Wdir() string { return filepath.Base(dir) } -// Limit defines the max number of watched files -func (r *Realize) limit() { +// Flimit defines the max number of watched files +func (r *Realize) Increases() { // increases the files limit var rLimit syscall.Rlimit rLimit.Max = r.Limit @@ -138,7 +108,7 @@ func (h *Blueprint) Clean() { // Read, Check and remove duplicates from the config file func (h *Blueprint) Read() error { - content, err := read(h.files["config"]) + content, err := read(h.Files["config"]) if err == nil { err = yaml.Unmarshal(content, h) if err == nil { @@ -159,7 +129,7 @@ func (h *Blueprint) Create() error { if err != nil { return err } - return write(h.files["config"], y) + return write(h.Files["config"], y) } // Inserts a new project in the list diff --git a/realize/exec.go b/cli/exec.go similarity index 97% rename from realize/exec.go rename to cli/exec.go index aded846..0d9cd22 100644 --- a/realize/exec.go +++ b/cli/exec.go @@ -1,4 +1,4 @@ -package realize +package cli import ( "bufio" @@ -55,7 +55,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) log.Println(pname(p.Name, 3), ":", BlueS(in.Text())) } if p.Watcher.Output["file"] { - path := filepath.Join(p.base, App.Blueprint.files["output"]) + path := filepath.Join(p.base, App.Blueprint.Files["output"]) f := create(path) t := time.Now() if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + in.Text() + "\r\n"); err != nil { diff --git a/cli/main.go b/cli/main.go new file mode 100644 index 0000000..1239fe5 --- /dev/null +++ b/cli/main.go @@ -0,0 +1,70 @@ +package cli + +import ( + "github.com/fatih/color" + "log" + "sync" + "time" +) + +var App Realize + +var wg sync.WaitGroup + +// Green, Red Bold, Red, Blue, Blue Bold, Yellow, Yellow Bold, Magenta, Magenta Bold colors +var Green, Red, RedS, Blue, BlueS, Yellow, YellowS, Magenta, MagentaS = color.New(color.FgGreen, color.Bold).SprintFunc(), + color.New(color.FgRed, color.Bold).SprintFunc(), + color.New(color.FgRed).SprintFunc(), + color.New(color.FgBlue, color.Bold).SprintFunc(), + color.New(color.FgBlue).SprintFunc(), + color.New(color.FgYellow, color.Bold).SprintFunc(), + color.New(color.FgYellow).SprintFunc(), + color.New(color.FgMagenta, color.Bold).SprintFunc(), + color.New(color.FgMagenta).SprintFunc() + +// Realize struct contains the general app informations +type Realize struct { + Name, Description, Author, Email string + Version string + Limit uint64 + Blueprint Blueprint +} + +// Projects struct contains a projects list +type Blueprint struct { + Projects []Project `yaml:"Projects,omitempty"` + Files map[string]string `yaml:"-"` +} + +// Project defines the informations of a single project +type Project struct { + reload time.Time + base string + Name string `yaml:"app_name,omitempty"` + Path string `yaml:"app_path,omitempty"` + Run bool `yaml:"app_run,omitempty"` + Bin bool `yaml:"app_bin,omitempty"` + Build bool `yaml:"app_build,omitempty"` + Fmt bool `yaml:"app_fmt,omitempty"` + Test bool `yaml:"app_test,omitempty"` + Params []string `yaml:"app_params,omitempty"` + Watcher Watcher `yaml:"app_watcher,omitempty"` +} + +// Watcher struct defines the livereload's logic +type Watcher struct { + // different before and after on re-run? + Before []string `yaml:"before,omitempty"` + After []string `yaml:"after,omitempty"` + Paths []string `yaml:"paths,omitempty"` + Ignore []string `yaml:"ignore_paths,omitempty"` + Exts []string `yaml:"exts,omitempty"` + Preview bool `yaml:"preview,omitempty"` + Output map[string]bool `yaml:"output,omitempty"` +} + +// Initialize the application +func init() { + log.SetFlags(0) + log.SetOutput(new(logWriter)) +} diff --git a/realize/utils.go b/cli/utils.go similarity index 98% rename from realize/utils.go rename to cli/utils.go index b0a7ea3..4c7fb3b 100644 --- a/realize/utils.go +++ b/cli/utils.go @@ -1,4 +1,4 @@ -package realize +package cli import ( "errors" @@ -30,7 +30,7 @@ func read(file string) ([]byte, error) { func write(name string, data []byte) error { err := ioutil.WriteFile(name, data, 0655) if err != nil { - log.Fatal(err) + log.Fatal(Red(err)) return err } return nil diff --git a/realize/watcher.go b/cli/watcher.go similarity index 91% rename from realize/watcher.go rename to cli/watcher.go index 6e8df4f..db96820 100644 --- a/realize/watcher.go +++ b/cli/watcher.go @@ -1,4 +1,4 @@ -package realize +package cli import ( "errors" @@ -13,18 +13,6 @@ import ( "time" ) -// The Watcher struct defines the livereload's logic -type Watcher struct { - // different before and after on re-run? - Before []string `yaml:"before,omitempty"` - After []string `yaml:"after,omitempty"` - Paths []string `yaml:"paths,omitempty"` - Ignore []string `yaml:"ignore_paths,omitempty"` - Exts []string `yaml:"exts,omitempty"` - Preview bool `yaml:"preview,omitempty"` - Output map[string]bool `yaml:"output,omitempty"` -} - // Watching method is the main core. It manages the livereload and the watching func (p *Project) watching() { diff --git a/main.go b/realize.go similarity index 89% rename from main.go rename to realize.go index e6c027d..cc9a85a 100644 --- a/main.go +++ b/realize.go @@ -1,14 +1,31 @@ package main import ( - "fmt" - r "github.com/tockins/realize/realize" + r "github.com/tockins/realize/cli" //_ "github.com/tockins/realize/server" + "fmt" "gopkg.in/urfave/cli.v2" "log" "os" ) +func init() { + App := r.Realize{ + Name: "Realize", + Version: "1.0", + Description: "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths", + Limit: 10000, + Blueprint: r.Blueprint{ + Files: map[string]string{ + "config": "r.config.yaml", + "output": "r.output.log", + }, + }, + } + App.Increases() + r.App = App +} + func main() { app := r.App diff --git a/realize/realize.go b/realize/realize.go deleted file mode 100644 index 879fb9c..0000000 --- a/realize/realize.go +++ /dev/null @@ -1,55 +0,0 @@ -package realize - -import ( - "github.com/fatih/color" - "log" - "sync" -) - -var App Realize - -var wg sync.WaitGroup - -// Green color bold -var Green = color.New(color.FgGreen, color.Bold).SprintFunc() - -// Red color bold -var Red = color.New(color.FgRed, color.Bold).SprintFunc() - -// RedS color used for errors -var RedS = color.New(color.FgRed).SprintFunc() - -// Blue color bold used for project output -var Blue = color.New(color.FgBlue, color.Bold).SprintFunc() - -// BlueS color -var BlueS = color.New(color.FgBlue).SprintFunc() - -// Yellow color bold -var Yellow = color.New(color.FgYellow, color.Bold).SprintFunc() - -// YellowS color -var YellowS = color.New(color.FgYellow).SprintFunc() - -// MagentaS color -var MagentaS = color.New(color.FgMagenta).SprintFunc() - -// Magenta color bold -var Magenta = color.New(color.FgMagenta, color.Bold).SprintFunc() - -// Initialize the application -func init() { - App = Realize{ - Name: "Realize", - Version: "1.0", - Description: "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths", - Limit: 10000, - } - App.Blueprint.files = map[string]string{ - "config": "r.config.yaml", - "output": "r.output.log", - } - App.limit() - log.SetFlags(0) - log.SetOutput(new(logWriter)) -}