improved: added tab completion for start command
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
e131342e1f
commit
fa14ce5f2a
43
cmd/start.go
43
cmd/start.go
@ -1,6 +1,9 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/abs3ntdev/haunt/src/config"
|
"github.com/abs3ntdev/haunt/src/config"
|
||||||
"github.com/abs3ntdev/haunt/src/haunt"
|
"github.com/abs3ntdev/haunt/src/haunt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -11,13 +14,16 @@ var startConfig config.Flags
|
|||||||
var startCmd = &cobra.Command{
|
var startCmd = &cobra.Command{
|
||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Start haunt on a given path, generates a config file if one does not already exist",
|
Short: "Start haunt on a given path, generates a config file if one does not already exist",
|
||||||
|
Args: cobra.MatchAll(cobra.MaximumNArgs(1), cobra.OnlyValidArgs),
|
||||||
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
return getProjectNamesToStart(toComplete), cobra.ShellCompDirectiveNoFileComp
|
||||||
|
},
|
||||||
RunE: start,
|
RunE: start,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(startCmd)
|
rootCmd.AddCommand(startCmd)
|
||||||
startCmd.Flags().StringVarP(&startConfig.Path, "path", "p", "", "Project base path")
|
startCmd.Flags().StringVarP(&startConfig.Path, "path", "p", "", "Project base path")
|
||||||
startCmd.Flags().StringVarP(&startConfig.Name, "name", "n", "", "Run a project by its name")
|
|
||||||
startCmd.Flags().BoolVarP(&startConfig.Format, "fmt", "f", false, "Enable go fmt")
|
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.Vet, "vet", "v", false, "Enable go vet")
|
||||||
startCmd.Flags().BoolVarP(&startConfig.Test, "test", "t", false, "Enable go test")
|
startCmd.Flags().BoolVarP(&startConfig.Test, "test", "t", false, "Enable go test")
|
||||||
@ -31,8 +37,24 @@ func init() {
|
|||||||
startCmd.Flags().BoolVarP(&startConfig.NoConfig, "no-config", "c", false, "Ignore existing config and doesn't create a new one")
|
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
|
// Start haunt workflow
|
||||||
func start(cmd *cobra.Command, args []string) (err error) {
|
func start(_ *cobra.Command, args []string) (err error) {
|
||||||
r := haunt.NewHaunt()
|
r := haunt.NewHaunt()
|
||||||
// set legacy watcher
|
// set legacy watcher
|
||||||
if startConfig.Legacy {
|
if startConfig.Legacy {
|
||||||
@ -46,13 +68,18 @@ func start(cmd *cobra.Command, args []string) (err error) {
|
|||||||
// check no-config and read
|
// check no-config and read
|
||||||
if !startConfig.NoConfig {
|
if !startConfig.NoConfig {
|
||||||
// read a config if exist
|
// read a config if exist
|
||||||
err := r.Settings.Read(&r)
|
err = r.Settings.Read(&r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if startConfig.Name != "" {
|
if len(args) >= 1 && args[0] != "" {
|
||||||
|
name := args[0]
|
||||||
// filter by name flag if exist
|
// filter by name flag if exist
|
||||||
r.Schema.Projects = r.Schema.Filter("Name", startConfig.Name)
|
r.Projects = r.Filter("Name", name)
|
||||||
|
if len(r.Projects) == 0 {
|
||||||
|
fmt.Println("Project not found, exiting")
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// increase file limit
|
// increase file limit
|
||||||
if r.Settings.FileLimit != 0 {
|
if r.Settings.FileLimit != 0 {
|
||||||
@ -63,11 +90,11 @@ func start(cmd *cobra.Command, args []string) (err error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
// check project list length
|
// check project list length
|
||||||
if len(r.Schema.Projects) == 0 {
|
if len(r.Projects) == 0 {
|
||||||
// create a new project based on given params
|
// create a new project based on given params
|
||||||
project := r.Schema.New(startConfig)
|
project := r.New(startConfig)
|
||||||
// Add to projects list
|
// Add to projects list
|
||||||
r.Schema.Add(project)
|
r.Add(project)
|
||||||
// save config
|
// save config
|
||||||
if !startConfig.NoConfig {
|
if !startConfig.NoConfig {
|
||||||
err = r.Settings.Write(r)
|
err = r.Settings.Write(r)
|
||||||
|
@ -260,10 +260,10 @@ func (p *Project) Reload(path string, stop <-chan bool) {
|
|||||||
}()
|
}()
|
||||||
} else {
|
} else {
|
||||||
if install.Err != nil {
|
if install.Err != nil {
|
||||||
log.Println(p.parent.Prefix("Install failed for: " + p.Name + " exiting"))
|
log.Println(p.parent.Prefix("Install failed for: " + p.Name))
|
||||||
}
|
}
|
||||||
if build.Err != nil {
|
if build.Err != nil {
|
||||||
log.Println(p.parent.Prefix("Build failed for: " + p.Name + " exiting"))
|
log.Println(p.parent.Prefix("Build failed for: " + p.Name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if done {
|
if done {
|
||||||
|
Loading…
Reference in New Issue
Block a user