improved: change default to install + run and change start -> run
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
abs3nt 2023-03-05 23:15:54 -08:00
parent d6c1b9704c
commit 712ef52265
5 changed files with 90 additions and 127 deletions

View File

@ -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
}

80
cmd/run.go Normal file
View File

@ -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()
}

View File

@ -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()
}

View File

@ -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))

View File

@ -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 {