diff --git a/realize/config.go b/realize/config.go deleted file mode 100644 index 726a0ff..0000000 --- a/realize/config.go +++ /dev/null @@ -1,219 +0,0 @@ -package realize - -import ( - "errors" - "fmt" - "gopkg.in/urfave/cli.v2" - "gopkg.in/yaml.v2" - "io/ioutil" - "log" - "os" - "path/filepath" - "strings" -) - -// Config struct contains the general informations about a project -type Config struct { - file string - Version string `yaml:"version,omitempty"` - 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")), - Test: params.Bool("test"), - 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 nameFlag(params *cli.Context) string { - var name string - if params.String("name") == "" && params.String("path") == "" { - return WorkingDir() - } 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 { - return false - } - return true -} - -// WorkingDir returns the last element of the working dir path -func WorkingDir() string { - dir, err := os.Getwd() - if err != nil { - log.Fatal(Red(err)) - } - return filepath.Base(dir) -} - -// Duplicates check projects with same name or same combinations of main/path -func Duplicates(value Project, arr []Project) (Project, error) { - for _, val := range arr { - if value.Path == val.Path || value.Name == val.Name { - return val, errors.New("There is a duplicate of '" + val.Name + "'. Check your config file!") - } - } - return Project{}, nil -} - -// Clean duplicate projects -func (h *Config) Clean() { - arr := h.Projects - for key, val := range arr { - if _, err := Duplicates(val, arr[key+1:]); err != nil { - h.Projects = append(arr[:key], arr[key+1:]...) - break - } - } -} - -// Read, Check and remove duplicates from the config file -func (h *Config) Read() error { - _, err := os.Stat(h.file) - if err == nil { - file, err := ioutil.ReadFile(h.file) - if err == nil { - if len(h.Projects) > 0 { - err = yaml.Unmarshal(file, h) - if err == nil { - h.Clean() - } - return err - } - return errors.New("There are no projects") - } - return h.Create() - } - return err -} - -// Create and unmarshal yaml config file -func (h *Config) Create() error { - y, err := yaml.Marshal(h) - if err != nil { - return err - } - return ioutil.WriteFile(h.file, y, 0655) -} - -// Add another project -func (h *Config) Add(params *cli.Context) error { - err := h.Read() - if err == nil { - new := 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")), - Test: params.Bool("test"), - Params: argsParam(params), - Watcher: Watcher{ - Paths: watcherPaths, - Exts: watcherExts, - Ignore: watcherIgnores, - }, - } - if _, err := Duplicates(new, h.Projects); err != nil { - return err - } - h.Projects = append(h.Projects, new) - err = h.Create() - if err == nil { - fmt.Println(Green("Your project was successfully added")) - } - return err - } - err = h.Create() - if err == nil { - fmt.Println(Green("The config file was successfully created")) - } - return err -} - -// Remove a project in list -func (h *Config) Remove(params *cli.Context) error { - err := h.Read() - if err == nil { - for key, val := range h.Projects { - if params.String("name") == val.Name { - h.Projects = append(h.Projects[:key], h.Projects[key+1:]...) - err = h.Create() - if err == nil { - fmt.Println(Green("Your project was successfully removed")) - } - return err - } - } - return errors.New("No project found") - } - return err -} - -// List of projects -func (h *Config) List() error { - err := h.Read() - if err == nil { - for _, val := range h.Projects { - 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("Install"), ":", MagentaS(val.Bin)) - fmt.Println(MagentaS("|"), "\t", Yellow("Fmt"), ":", MagentaS(val.Fmt)) - fmt.Println(MagentaS("|"), "\t", Yellow("Test"), ":", MagentaS(val.Test)) - fmt.Println(MagentaS("|"), "\t", Yellow("Params"), ":", MagentaS(val.Params)) - 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)) - fmt.Println(MagentaS("|"), "\t\t", Yellow("Paths"), ":", MagentaS(val.Watcher.Paths)) - fmt.Println(MagentaS("|"), "\t\t", Yellow("Paths ignored"), ":", MagentaS(val.Watcher.Ignore)) - fmt.Println(MagentaS("|"), "\t\t", Yellow("Watch preview"), ":", MagentaS(val.Watcher.Preview)) - } - return nil - } - return err -} diff --git a/realize/project.go b/realize/project.go deleted file mode 100644 index de1896e..0000000 --- a/realize/project.go +++ /dev/null @@ -1,142 +0,0 @@ -package realize - -import ( - "bufio" - "bytes" - "io" - "log" - "os" - "os/exec" - "path/filepath" - "sync" - "time" -) - -// The Project struct defines the informations about a 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"` -} - -// GoRun is an implementation of the bin execution -func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error { - - stop := make(chan bool, 1) - var build *exec.Cmd - if len(p.Params) != 0 { - build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.Path)), p.Params...) - } else { - build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.Path))) - } - build.Dir = p.base - defer func() { - if err := build.Process.Kill(); err != nil { - log.Fatal(Red("Failed to stop: "), Red(err)) - } - log.Println(pname(p.Name, 2), ":", RedS("Stopped")) - wr.Done() - }() - - stdout, err := build.StdoutPipe() - stderr, err := build.StderrPipe() - - // Read stdout and stderr in same var - outputs := io.MultiReader(stdout, stderr) - - if err != nil { - log.Println(Red(err.Error())) - return err - } - if err := build.Start(); err != nil { - log.Println(Red(err.Error())) - return err - } - close(runner) - - in := bufio.NewScanner(outputs) - go func() { - for in.Scan() { - select { - default: - log.Println(pname(p.Name, 3), ":", BlueS(in.Text())) - } - } - close(stop) - }() - - for { - select { - case <-channel: - return nil - case <-stop: - return nil - } - } -} - -// GoBuild is an implementation of the "go build" -func (p *Project) GoBuild() (string, error) { - var out bytes.Buffer - var stderr bytes.Buffer - build := exec.Command("go", "build") - build.Dir = p.base - build.Stdout = &out - build.Stderr = &stderr - if err := build.Run(); err != nil { - return stderr.String(), err - } - return "", nil -} - -// GoInstall is an implementation of the "go install" -func (p *Project) GoInstall() (string, error) { - var out bytes.Buffer - var stderr bytes.Buffer - err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin")) - if err != nil { - return "", nil - } - build := exec.Command("go", "install") - build.Dir = p.base - build.Stdout = &out - build.Stderr = &stderr - if err := build.Run(); err != nil { - return stderr.String(), err - } - return "", nil -} - -// GoFmt is an implementation of the gofmt -func (p *Project) GoFmt(path string) (io.Writer, error) { - var out bytes.Buffer - build := exec.Command("gofmt", "-s", "-w", "-e", path) - build.Dir = p.base - build.Stdout = &out - build.Stderr = &out - if err := build.Run(); err != nil { - return build.Stderr, err - } - return nil, nil -} - -// GoTest is an implementation of the go test -func (p *Project) GoTest(path string) (io.Writer, error) { - var out bytes.Buffer - build := exec.Command("go", "test") - build.Dir = path - build.Stdout = &out - build.Stderr = &out - if err := build.Run(); err != nil { - return build.Stdout, err - } - return nil, nil -}