From e131e37349290dbe55a34013701cc3730f50eddd Mon Sep 17 00:00:00 2001 From: alessio Date: Fri, 21 Oct 2016 17:30:12 +0200 Subject: [PATCH] read,write file moved to config folder --- app/main.go | 8 ++++---- cli/cmd.go | 19 ++++++++++++++++--- cli/exec.go | 6 +++--- cli/main.go | 4 ++-- cli/utils.go | 49 ------------------------------------------------ cli/watcher.go | 2 +- config/config.go | 18 ++++++++++++++++++ config/io.go | 30 +++++++++++++++++++++++++++++ config/utils.go | 17 +++++++++++------ 9 files changed, 85 insertions(+), 68 deletions(-) create mode 100644 config/config.go create mode 100644 config/io.go diff --git a/app/main.go b/app/main.go index 2d5f5e7..fff8e75 100644 --- a/app/main.go +++ b/app/main.go @@ -25,7 +25,7 @@ var R Realize // Realize struct contains the general app informations type Realize struct { - c.Utils + c.Config Name, Description, Author, Email, Host string Version string Limit uint64 @@ -50,9 +50,9 @@ func init() { Sync: make(chan string), } R.Blueprint = w.Blueprint{ - Utils: R.Utils, - Files: R.Files, - Sync: R.Sync, + Config: R.Config, + Files: R.Files, + Sync: R.Sync, } R.Server = s.Server{ Blueprint: &R.Blueprint, diff --git a/cli/cmd.go b/cli/cmd.go index dbf5842..aaa555e 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -55,6 +55,7 @@ func (h *Blueprint) Fast(params *cli.Context) error { // Add a new project func (h *Blueprint) Add(params *cli.Context) error { p := Project{ + Name: h.name(params), Path: filepath.Clean(params.String("path")), Build: params.Bool("build"), Bin: boolFlag(params.Bool("no-bin")), @@ -72,7 +73,6 @@ func (h *Blueprint) Add(params *cli.Context) error { }, }, } - p.Name = p.nameFlag(params) if _, err := duplicates(p, h.Projects); err != nil { return err } @@ -93,7 +93,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 := h.Stream(h.Files["config"]) if err == nil { err = yaml.Unmarshal(content, h) if err == nil { @@ -114,7 +114,7 @@ func (h *Blueprint) Create() error { if err != nil { return err } - return write(h.Files["config"], y) + return h.Write(h.Files["config"], y) } // Inserts a new project in the list @@ -178,3 +178,16 @@ func (h *Blueprint) List() error { } return err } + +// NameParam check the project name presence. If empty takes the working directory name +func (p *Blueprint) name(params *cli.Context) string { + var name string + if params.String("name") == "" && params.String("path") == "" { + return p.Wdir() + } else if params.String("path") != "/" { + name = filepath.Base(params.String("path")) + } else { + name = params.String("name") + } + return name +} diff --git a/cli/exec.go b/cli/exec.go index 5c9ca6c..d86f40d 100644 --- a/cli/exec.go +++ b/cli/exec.go @@ -30,7 +30,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) defer func() { if err := build.Process.Kill(); err != nil { p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Failed to stop: " + err.Error()}) - log.Fatal(Red("Failed to stop: "), Red(err)) + p.Fatal("Failed to stop:", err) } p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Ended"}) log.Println(pname(p.Name, 2), ":", RedS("Ended")) @@ -70,10 +70,10 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } if p.Watcher.Output["file"] { path := filepath.Join(p.base, p.parent.Files["output"]) - f := create(path) + f := p.Create(path) t := time.Now() if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + output.Text() + "\r\n"); err != nil { - log.Fatal(err) + p.Fatal("", err) } } } diff --git a/cli/main.go b/cli/main.go index 9fc6106..944a2a4 100644 --- a/cli/main.go +++ b/cli/main.go @@ -25,7 +25,7 @@ type logWriter struct{} // Projects struct contains a projects list type Blueprint struct { - c.Utils + c.Config Projects []Project `yaml:"projects,omitempty"` Files map[string]string `yaml:"-"` Sync chan string `yaml:"-"` @@ -33,7 +33,7 @@ type Blueprint struct { // Project defines the informations of a single project type Project struct { - c.Utils + c.Config LastChangedOn time.Time `yaml:"-"` base string Name string `yaml:"app_name,omitempty"` diff --git a/cli/utils.go b/cli/utils.go index 520748b..a466284 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -4,46 +4,10 @@ import ( "errors" "fmt" "gopkg.in/urfave/cli.v2" - "io/ioutil" - "log" - "os" - "path/filepath" "strings" "time" ) -// Read a file given a name and return its byte stream -func read(file string) ([]byte, error) { - _, err := os.Stat(file) - if err == nil { - content, err := ioutil.ReadFile(file) - if err != nil { - return nil, err - } - return content, err - } - return nil, err -} - -// Write a file given a name and a byte stream -func write(name string, data []byte) error { - err := ioutil.WriteFile(name, data, 0655) - if err != nil { - log.Fatal(Red(err)) - return err - } - return nil -} - -// Create a new file and return its pointer -func create(file string) *os.File { - out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0655) - if err != nil { - log.Fatal(err) - } - return out -} - // argsParam parse one by one the given argumentes func argsParam(params *cli.Context) []string { argsN := params.NArg() @@ -57,19 +21,6 @@ func argsParam(params *cli.Context) []string { return nil } -// NameParam check the project name presence. If empty takes the working directory name -func (p *Project) nameFlag(params *cli.Context) string { - var name string - if params.String("name") == "" && params.String("path") == "" { - return p.Wdir() - } else if params.String("path") != "/" { - name = filepath.Base(params.String("path")) - } else { - name = params.String("name") - } - return name -} - // BoolParam is used to check the presence of a bool flag func boolFlag(b bool) bool { if b { diff --git a/cli/watcher.go b/cli/watcher.go index 8bc88db..71736a4 100644 --- a/cli/watcher.go +++ b/cli/watcher.go @@ -78,7 +78,7 @@ func (p *Project) watching() { err := p.fmt(event.Name[:i] + ext) if err != nil { - log.Fatal(Red(err)) + p.Fatal("", err) } else { go p.routines(channel, &wr) p.LastChangedOn = time.Now().Truncate(time.Second) diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..1f38cea --- /dev/null +++ b/config/config.go @@ -0,0 +1,18 @@ +package config + +import ( + "github.com/fatih/color" +) + +type Config struct{} + +var Red, Blue, Yellow, Magenta = color.New(color.FgRed).SprintFunc(), + color.New(color.FgBlue).SprintFunc(), + color.New(color.FgYellow).SprintFunc(), + color.New(color.FgMagenta).SprintFunc() + +var GreenB, RedB, BlueB, YellowB, MagentaB = color.New(color.FgGreen, color.Bold).SprintFunc(), + color.New(color.FgRed, color.Bold).SprintFunc(), + color.New(color.FgBlue, color.Bold).SprintFunc(), + color.New(color.FgYellow, color.Bold).SprintFunc(), + color.New(color.FgMagenta, color.Bold).SprintFunc() diff --git a/config/io.go b/config/io.go new file mode 100644 index 0000000..80bc0e1 --- /dev/null +++ b/config/io.go @@ -0,0 +1,30 @@ +package config + +import ( + "io/ioutil" + "os" +) + +// Scan return a byte stream of a given file +func (c *Config) Stream(file string) ([]byte, error) { + _, err := os.Stat(file) + if err == nil { + content, err := ioutil.ReadFile(file) + c.Validate(err) + return content, err + } + return nil, err +} + +// Write a file given a name and a byte stream +func (c *Config) Write(name string, data []byte) error { + err := ioutil.WriteFile(name, data, 0655) + return c.Validate(err) +} + +// Create a new file and return its pointer +func (c *Config) Create(file string) *os.File { + out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0655) + c.Validate(err) + return out +} diff --git a/config/utils.go b/config/utils.go index e77bde0..8fcd207 100644 --- a/config/utils.go +++ b/config/utils.go @@ -6,17 +6,22 @@ import ( "path/filepath" ) -type Utils struct{} - -func (u *Utils) Wdir() string { +func (c *Config) Wdir() string { dir, err := os.Getwd() - u.Validate(err) + c.Validate(err) return filepath.Base(dir) } -func (u *Utils) Validate(err error) error { +func (c *Config) Validate(err error) error { if err != nil { - log.Fatal(err) + log.Fatal(Red(err)) } return nil } + +func (c *Config) Fatal(msg string, err error){ + if(msg != "") { + log.Fatal(Red(msg), err.Error()) + } + log.Fatal(err.Error()) +}