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

View File

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