From 712ef52265824e887f430df05f5be5bf684a164d Mon Sep 17 00:00:00 2001 From: abs3nt Date: Sun, 5 Mar 2023 23:15:54 -0800 Subject: [PATCH] improved: change default to install + run and change start -> run --- cmd/clean.go | 2 +- cmd/run.go | 80 ++++++++++++++++++++++++++++++ cmd/start.go | 120 --------------------------------------------- src/haunt/cli.go | 7 +-- src/haunt/tools.go | 8 ++- 5 files changed, 90 insertions(+), 127 deletions(-) create mode 100644 cmd/run.go delete mode 100644 cmd/start.go diff --git a/cmd/clean.go b/cmd/clean.go index cd4852e..8230770 100644 --- a/cmd/clean.go +++ b/cmd/clean.go @@ -24,6 +24,6 @@ func clean(cmd *cobra.Command, args []string) (err error) { if err := r.Settings.Remove(haunt.RFile); err != nil { return err } - log.Println(r.Prefix(haunt.Green.Bold("folder successfully removed"))) + log.Println(r.Prefix(haunt.Green.Bold("config file removed successfully removed"))) return nil } diff --git a/cmd/run.go b/cmd/run.go new file mode 100644 index 0000000..f174121 --- /dev/null +++ b/cmd/run.go @@ -0,0 +1,80 @@ +package cmd + +import ( + "log" + "strings" + + "github.com/abs3ntdev/haunt/src/haunt" + "github.com/spf13/cobra" +) + +var runCmd = &cobra.Command{ + Use: "run", + Short: "run haunt, optionally provide the name of projects to only run those otherwise will run all configured projects", + Args: cobra.MatchAll(cobra.OnlyValidArgs), + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return getProjectNamesToRun(toComplete), cobra.ShellCompDirectiveNoFileComp + }, + RunE: run, +} + +func init() { + rootCmd.AddCommand(runCmd) +} + +func getProjectNamesToRun(input string) []string { + r := haunt.NewHaunt() + // read a config if exist + err := r.Settings.Read(&r) + if err != nil { + return []string{} + } + names := []string{} + for _, project := range r.Projects { + if strings.HasPrefix(project.Name, input) { + names = append(names, project.Name) + } + } + return names +} + +// haunt workflow +func run(cmd *cobra.Command, args []string) (err error) { + r := haunt.NewHaunt() + + // read a config if exist + err = r.Settings.Read(&r) + if err != nil { + return err + } + if len(args) >= 1 { + // filter by name flag if exist + r.Projects = r.Filter(args) + if len(r.Projects) == 0 { + log.Println(r.Prefix("No valid project found, exiting. Check your config file or run haunt add")) + return + } + } + // increase file limit + if r.Settings.FileLimit != 0 { + if err = r.Settings.Flimit(); err != nil { + return err + } + } + + // web server + if r.Server.Status { + r.Server.Parent = r + err = r.Server.Start() + if err != nil { + return err + } + err = r.Server.OpenURL() + if err != nil { + return err + } + } + + // run workflow + return r.Run() +} diff --git a/cmd/start.go b/cmd/start.go deleted file mode 100644 index 5453bf0..0000000 --- a/cmd/start.go +++ /dev/null @@ -1,120 +0,0 @@ -package cmd - -import ( - "log" - "strings" - - "github.com/abs3ntdev/haunt/src/config" - "github.com/abs3ntdev/haunt/src/haunt" - "github.com/spf13/cobra" -) - -var startConfig config.Flags - -var startCmd = &cobra.Command{ - Use: "start", - Short: "Start haunt on a given path, generates a config file if one does not already exist", - Args: cobra.MatchAll(cobra.OnlyValidArgs), - ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return getProjectNamesToStart(toComplete), cobra.ShellCompDirectiveNoFileComp - }, - RunE: start, -} - -func init() { - rootCmd.AddCommand(startCmd) - startCmd.Flags().StringVarP(&startConfig.Path, "path", "p", "", "Project base path") - startCmd.Flags().BoolVarP(&startConfig.Format, "fmt", "f", false, "Enable go fmt") - startCmd.Flags().BoolVarP(&startConfig.Vet, "vet", "v", false, "Enable go vet") - startCmd.Flags().BoolVarP(&startConfig.Test, "test", "t", false, "Enable go test") - startCmd.Flags().BoolVarP(&startConfig.Generate, "generate", "g", false, "Enable go generate") - startCmd.Flags().BoolVarP(&startConfig.Server, "server", "s", false, "Start server") - startCmd.Flags().BoolVarP(&startConfig.Open, "open", "o", false, "Open into the default browser") - startCmd.Flags().BoolVarP(&startConfig.Install, "install", "i", true, "Enable go install") - startCmd.Flags().BoolVarP(&startConfig.Build, "build", "b", false, "Enable go build") - startCmd.Flags().BoolVarP(&startConfig.Run, "run", "r", true, "Enable go run") - startCmd.Flags().BoolVarP(&startConfig.Legacy, "legacy", "l", false, "Legacy watch by polling instead fsnotify") - startCmd.Flags().BoolVarP(&startConfig.NoConfig, "no-config", "c", false, "Ignore existing config and doesn't create a new one") -} - -func getProjectNamesToStart(input string) []string { - r := haunt.NewHaunt() - // read a config if exist - err := r.Settings.Read(&r) - if err != nil { - return []string{} - } - names := []string{} - for _, project := range r.Projects { - if strings.HasPrefix(project.Name, input) { - names = append(names, project.Name) - } - } - return names -} - -// Start haunt workflow -func start(cmd *cobra.Command, args []string) (err error) { - r := haunt.NewHaunt() - // set legacy watcher - if startConfig.Legacy { - r.Settings.Legacy.Set(startConfig.Legacy, 1) - } - // set server - if startConfig.Server { - r.Server.Set(startConfig.Server, startConfig.Open, haunt.Port, haunt.Host) - } - - // check no-config and read - if !startConfig.NoConfig { - // read a config if exist - err = r.Settings.Read(&r) - if err != nil { - return err - } - if len(args) >= 1 { - // filter by name flag if exist - r.Projects = r.Filter(args) - if len(r.Projects) == 0 { - log.Println(r.Prefix("Project not found, exiting")) - return - } - startConfig.Name = args[0] - } - // increase file limit - if r.Settings.FileLimit != 0 { - if err = r.Settings.Flimit(); err != nil { - return err - } - } - - } - // check project list length - if len(r.Projects) == 0 { - // create a new project based on given params - project := r.New(startConfig) - // Add to projects list - r.Add(project) - // save config - if !startConfig.NoConfig { - err = r.Settings.Write(r) - if err != nil { - return err - } - } - } - // Start web server - if r.Server.Status { - r.Server.Parent = r - err = r.Server.Start() - if err != nil { - return err - } - err = r.Server.OpenURL() - if err != nil { - return err - } - } - // start workflow - return r.Start() -} diff --git a/src/haunt/cli.go b/src/haunt/cli.go index 9c95629..8dd8fee 100644 --- a/src/haunt/cli.go +++ b/src/haunt/cli.go @@ -120,9 +120,6 @@ func (r *Haunt) SetDefaults() { Install: Tool{ Status: true, }, - Build: Tool{ - Status: true, - }, Run: Tool{ Status: true, }, @@ -148,8 +145,8 @@ func (r *Haunt) Stop() error { return nil } -// Start haunt workflow -func (r *Haunt) Start() error { +// Run haunt workflow +func (r *Haunt) Run() error { if len(r.Projects) > 0 { var wg sync.WaitGroup wg.Add(len(r.Projects)) diff --git a/src/haunt/tools.go b/src/haunt/tools.go index 91490f6..b16c226 100644 --- a/src/haunt/tools.go +++ b/src/haunt/tools.go @@ -183,8 +183,14 @@ func (t *Tool) Compile(path string, stop <-chan bool) (response Response) { var out bytes.Buffer var stderr bytes.Buffer done := make(chan error) - args := append(t.cmd, t.Args...) + buildPath, _ := filepath.Abs(Wdir()) + buildPath = "-o " + buildPath + buildPath += "/bin" + fmt.Println(t.cmd, t.Args) + args := append(t.cmd, buildPath) + args = append(args, t.Args...) cmd := exec.Command(args[0], args[1:]...) + fmt.Println(cmd) if t.Dir != "" { cmd.Dir, _ = filepath.Abs(t.Dir) } else {