haunt/cmd/run.go

81 lines
1.6 KiB
Go
Raw Normal View History

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 {
2023-03-06 07:39:01 +00:00
h := haunt.NewHaunt()
// read a config if exist
2023-03-06 07:39:01 +00:00
err := h.Settings.Read(&h)
if err != nil {
return []string{}
}
names := []string{}
2023-03-06 07:39:01 +00:00
for _, project := range h.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) {
2023-03-06 07:39:01 +00:00
h := haunt.NewHaunt()
// read a config if exist
2023-03-06 07:39:01 +00:00
err = h.Settings.Read(&h)
if err != nil {
return err
}
if len(args) >= 1 {
// filter by name flag if exist
2023-03-06 07:39:01 +00:00
h.Projects = h.Filter(args)
if len(h.Projects) == 0 {
log.Println(h.Prefix("No valid project found, exiting. Check your config file or run haunt add"))
return
}
}
// increase file limit
2023-03-06 07:39:01 +00:00
if h.Settings.FileLimit != 0 {
if err = h.Settings.Flimit(); err != nil {
return err
}
}
// web server
2023-03-06 07:39:01 +00:00
if h.Server.Status {
h.Server.Parent = h
err = h.Server.Start()
if err != nil {
return err
}
2023-03-06 07:39:01 +00:00
err = h.Server.OpenURL()
if err != nil {
return err
}
}
// run workflow
2023-03-06 07:39:01 +00:00
return h.Run()
}