diff --git a/app/main.go b/app/main.go index 4d97301..2d5f5e7 100644 --- a/app/main.go +++ b/app/main.go @@ -25,6 +25,7 @@ var R Realize // Realize struct contains the general app informations type Realize struct { + c.Utils Name, Description, Author, Email, Host string Version string Limit uint64 @@ -49,6 +50,7 @@ func init() { Sync: make(chan string), } R.Blueprint = w.Blueprint{ + Utils: R.Utils, Files: R.Files, Sync: R.Sync, } @@ -84,7 +86,7 @@ func (r *Realize) BlueS(s string) string { } func (r *Realize) Dir() string { - return c.Wdir() + return r.Wdir() } func (r *Realize) Serve(p *cli.Context) { diff --git a/cli/cmd.go b/cli/cmd.go index 27e277e..dbf5842 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -55,7 +55,6 @@ func (h *Blueprint) Fast(params *cli.Context) error { // Add a new project func (h *Blueprint) Add(params *cli.Context) error { p := Project{ - Name: nameFlag(params), Path: filepath.Clean(params.String("path")), Build: params.Bool("build"), Bin: boolFlag(params.Bool("no-bin")), @@ -73,6 +72,7 @@ func (h *Blueprint) Add(params *cli.Context) error { }, }, } + p.Name = p.nameFlag(params) if _, err := duplicates(p, h.Projects); err != nil { return err } diff --git a/cli/main.go b/cli/main.go index b0e4b83..9fc6106 100644 --- a/cli/main.go +++ b/cli/main.go @@ -2,6 +2,7 @@ package cli import ( "github.com/fatih/color" + c "github.com/tockins/realize/config" "log" "sync" "time" @@ -24,6 +25,7 @@ type logWriter struct{} // Projects struct contains a projects list type Blueprint struct { + c.Utils Projects []Project `yaml:"projects,omitempty"` Files map[string]string `yaml:"-"` Sync chan string `yaml:"-"` @@ -31,6 +33,7 @@ type Blueprint struct { // Project defines the informations of a single project type Project struct { + c.Utils LastChangedOn time.Time `yaml:"-"` base string Name string `yaml:"app_name,omitempty"` diff --git a/cli/utils.go b/cli/utils.go index 2586b61..520748b 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -12,15 +12,6 @@ import ( "time" ) -// WorkingDir returns the name last element of the working directory path -func WorkingDir() string { - dir, err := os.Getwd() - if err != nil { - log.Fatal(Red(err)) - } - return filepath.Base(dir) -} - // Read a file given a name and return its byte stream func read(file string) ([]byte, error) { _, err := os.Stat(file) @@ -67,10 +58,10 @@ func argsParam(params *cli.Context) []string { } // NameParam check the project name presence. If empty takes the working directory name -func nameFlag(params *cli.Context) string { +func (p *Project) nameFlag(params *cli.Context) string { var name string if params.String("name") == "" && params.String("path") == "" { - return WorkingDir() + return p.Wdir() } else if params.String("path") != "/" { name = filepath.Base(params.String("path")) } else { diff --git a/cli/watcher.go b/cli/watcher.go index beb0ef1..8bc88db 100644 --- a/cli/watcher.go +++ b/cli/watcher.go @@ -223,7 +223,7 @@ func (p *Project) walks(watcher *fsnotify.Watcher) error { if p.Path == "." || p.Path == "/" { p.base = wd - p.Path = WorkingDir() + p.Path = p.Wdir() } else if filepath.IsAbs(p.Path) { p.base = p.Path } else { diff --git a/config/utils.go b/config/utils.go index 730cf17..e77bde0 100644 --- a/config/utils.go +++ b/config/utils.go @@ -8,13 +8,13 @@ import ( type Utils struct{} -func Wdir() string { +func (u *Utils) Wdir() string { dir, err := os.Getwd() - Validate(err) + u.Validate(err) return filepath.Base(dir) } -func Validate(err error) error { +func (u *Utils) Validate(err error) error { if err != nil { log.Fatal(err) }