haunt/cmd/start.go

121 lines
3.4 KiB
Go
Raw Permalink Normal View History

2023-03-04 22:22:03 +00:00
package cmd
import (
2023-03-05 22:28:09 +00:00
"log"
"strings"
2023-03-04 22:22:03 +00:00
"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,
2023-03-04 22:22:03 +00:00
}
func init() {
rootCmd.AddCommand(startCmd)
2023-03-05 22:40:00 +00:00
startCmd.Flags().StringVarP(&startConfig.Path, "path", "p", "", "Project base path")
2023-03-04 22:22:03 +00:00
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")
2023-03-05 22:40:00 +00:00
startCmd.Flags().BoolVarP(&startConfig.Install, "install", "i", true, "Enable go install")
2023-03-06 00:00:54 +00:00
startCmd.Flags().BoolVarP(&startConfig.Build, "build", "b", false, "Enable go build")
2023-03-05 22:40:00 +00:00
startCmd.Flags().BoolVarP(&startConfig.Run, "run", "r", true, "Enable go run")
2023-03-04 22:22:03 +00:00
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
}
2023-03-04 22:22:03 +00:00
// Start haunt workflow
2023-03-05 22:28:09 +00:00
func start(cmd *cobra.Command, args []string) (err error) {
2023-03-04 22:22:03 +00:00
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)
2023-03-04 22:22:03 +00:00
if err != nil {
return err
}
if len(args) >= 1 {
2023-03-04 22:22:03 +00:00
// filter by name flag if exist
r.Projects = r.Filter(args)
if len(r.Projects) == 0 {
2023-03-05 22:28:09 +00:00
log.Println(r.Prefix("Project not found, exiting"))
return
}
2023-03-05 22:28:09 +00:00
startConfig.Name = args[0]
2023-03-04 22:22:03 +00:00
}
// 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 {
2023-03-04 22:22:03 +00:00
// create a new project based on given params
project := r.New(startConfig)
2023-03-04 22:22:03 +00:00
// Add to projects list
r.Add(project)
2023-03-04 22:22:03 +00:00
// 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()
}