diff --git a/main.go b/main.go index c18b57c..2a529c1 100644 --- a/main.go +++ b/main.go @@ -69,12 +69,6 @@ func main() { }, Action: func(p *cli.Context) error { y := r.New(p) - if p.NArg() > 0 { - y.Projects[0].Params = make([]string, p.NArg() - 1) - for i := 1; i < p.NArg(); i++ { - y.Projects[0].Params[i - 1] = p.Args().Get(i) - } - } return handle(y.Fast(p)) }, Before: func(c *cli.Context) error { diff --git a/realize/config.go b/realize/config.go index 47b0125..5af7c4a 100644 --- a/realize/config.go +++ b/realize/config.go @@ -19,8 +19,45 @@ type Config struct { Projects []Project } +// New method puts the cli params in the struct +func New(params *cli.Context) *Config { + return &Config{ + file: AppFile, + Version: AppVersion, + Projects: []Project{ + { + Name: nameFlag(params), + Path: filepath.Clean(params.String("path")), + Build: params.Bool("build"), + Bin: boolFlag(params.Bool("no-bin")), + Run: boolFlag(params.Bool("no-run")), + Fmt: boolFlag(params.Bool("no-fmt")), + Params: argsParam(params), + Watcher: Watcher{ + Paths: watcherPaths, + Ignore: watcherIgnores, + Exts: watcherExts, + }, + }, + }, + } +} + +// argsParam parse one by one the given argumentes +func argsParam(params *cli.Context) []string { + argsN := params.NArg() + if argsN > 0 { + var args []string + for i := 0; i <= argsN-1; i++ { + args = append(args, params.Args().Get(i)) + } + return args + } + return nil +} + // NameParam check the project name presence. If empty takes the working directory name -func nameParam(params *cli.Context) string { +func nameFlag(params *cli.Context) string { var name string if params.String("name") == "" && params.String("path") == "" { return WorkingDir() @@ -33,7 +70,7 @@ func nameParam(params *cli.Context) string { } // BoolParam is used to check the presence of a bool flag -func boolParam(b bool) bool { +func boolFlag(b bool) bool { if b { return false } @@ -49,29 +86,6 @@ func WorkingDir() string { return filepath.Base(dir) } -// New method puts the cli params in the struct -func New(params *cli.Context) *Config { - return &Config{ - file: AppFile, - Version: AppVersion, - Projects: []Project{ - { - Name: nameParam(params), - Path: filepath.Clean(params.String("path")), - Build: params.Bool("build"), - Bin: boolParam(params.Bool("no-bin")), - Run: boolParam(params.Bool("no-run")), - Fmt: boolParam(params.Bool("no-fmt")), - Watcher: Watcher{ - Paths: watcherPaths, - Ignore: watcherIgnores, - Exts: watcherExts, - }, - }, - }, - } -} - // Duplicates check projects with same name or same combinations of main/path func Duplicates(value Project, arr []Project) (error, Project) { for _, val := range arr { @@ -127,12 +141,13 @@ func (h *Config) Add(params *cli.Context) error { err := h.Read() if err == nil { new := Project{ - Name: nameParam(params), - Path: filepath.Clean(params.String("path")), - Build: params.Bool("build"), - Bin: boolParam(params.Bool("no-bin")), - Run: boolParam(params.Bool("no-run")), - Fmt: boolParam(params.Bool("no-fmt")), + Name: nameFlag(params), + Path: filepath.Clean(params.String("path")), + Build: params.Bool("build"), + Bin: boolFlag(params.Bool("no-bin")), + Run: boolFlag(params.Bool("no-run")), + Fmt: boolFlag(params.Bool("no-fmt")), + Params: argsParam(params), Watcher: Watcher{ Paths: watcherPaths, Exts: watcherExts, diff --git a/realize/project.go b/realize/project.go index 18fa13e..43461c2 100644 --- a/realize/project.go +++ b/realize/project.go @@ -16,14 +16,14 @@ import ( 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"` - Params []string `yaml:"app_params,omitempty"` - Watcher Watcher `yaml:"app_watcher,omitempty"` + 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"` + Params []string `yaml:"app_params,omitempty"` + Watcher Watcher `yaml:"app_watcher,omitempty"` } // GoRun is an implementation of the bin execution @@ -33,7 +33,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) var build *exec.Cmd if len(p.Params) != 0 { build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.Path)), p.Params...) - } else{ + } else { build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.Path))) } build.Dir = p.base