feat: start now accepts multiple arguments
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
abs3nt 2023-03-05 13:15:04 -08:00
parent fa14ce5f2a
commit d7b13f16ca
2 changed files with 8 additions and 12 deletions

View File

@ -14,7 +14,7 @@ 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), Args: cobra.MatchAll(cobra.OnlyValidArgs),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return getProjectNamesToStart(toComplete), cobra.ShellCompDirectiveNoFileComp return getProjectNamesToStart(toComplete), cobra.ShellCompDirectiveNoFileComp
}, },
@ -72,10 +72,9 @@ func start(_ *cobra.Command, args []string) (err error) {
if err != nil { if err != nil {
return err return err
} }
if len(args) >= 1 && args[0] != "" { if len(args) >= 1 {
name := args[0]
// filter by name flag if exist // filter by name flag if exist
r.Projects = r.Filter("Name", name) r.Projects = r.Filter(args)
if len(r.Projects) == 0 { if len(r.Projects) == 0 {
fmt.Println("Project not found, exiting") fmt.Println("Project not found, exiting")
return return

View File

@ -70,18 +70,15 @@ func (s *Schema) New(flags config.Flags) Project {
return project return project
} }
// Filter project list by field // Filter project list by names
func (s *Schema) Filter(field string, value interface{}) []Project { func (s *Schema) Filter(names []string) []Project {
result := []Project{} result := []Project{}
for _, item := range s.Projects { for _, item := range s.Projects {
v := reflect.ValueOf(item) for _, name := range names {
for i := 0; i < v.NumField(); i++ { if item.Name == name {
if v.Type().Field(i).Name == field {
if reflect.DeepEqual(v.Field(i).Interface(), value) {
result = append(result, item) result = append(result, item)
} }
} }
} }
}
return result return result
} }