diff --git a/pkg/devfile/parser/data/v2/commands.go b/pkg/devfile/parser/data/v2/commands.go index 42a1b6a1..8b45991f 100644 --- a/pkg/devfile/parser/data/v2/commands.go +++ b/pkg/devfile/parser/data/v2/commands.go @@ -43,7 +43,9 @@ func (d *DevfileV2) GetCommands(options common.DevfileOptions) ([]v1.Command, er continue } - commands = append(commands, command) + if options.FilterByName == "" || command.Id == options.FilterByName { + commands = append(commands, command) + } } return commands, nil diff --git a/pkg/devfile/parser/data/v2/commands_test.go b/pkg/devfile/parser/data/v2/commands_test.go index bf9c9c16..ebf70df2 100644 --- a/pkg/devfile/parser/data/v2/commands_test.go +++ b/pkg/devfile/parser/data/v2/commands_test.go @@ -137,6 +137,54 @@ func TestDevfile200_GetCommands(t *testing.T) { }, wantCommands: []string{"command3"}, }, + { + name: "Get command with the specified name", + currentCommands: []v1.Command{ + { + Id: "command1", + CommandUnion: v1.CommandUnion{ + Exec: &v1.ExecCommand{}, + }, + }, + { + Id: "command2", + CommandUnion: v1.CommandUnion{ + Composite: &v1.CompositeCommand{}, + }, + }, + { + Id: "command3", + CommandUnion: v1.CommandUnion{ + Composite: &v1.CompositeCommand{}, + }, + }, + }, + filterOptions: common.DevfileOptions{ + FilterByName: "command3", + }, + wantCommands: []string{"command3"}, + }, + { + name: "command name not found", + currentCommands: []v1.Command{ + { + Id: "command1", + CommandUnion: v1.CommandUnion{ + Exec: &v1.ExecCommand{}, + }, + }, + { + Id: "command2", + CommandUnion: v1.CommandUnion{ + Composite: &v1.CompositeCommand{}, + }, + }, + }, + filterOptions: common.DevfileOptions{ + FilterByName: "command3", + }, + wantCommands: []string{}, + }, { name: "Wrong filter for commands", currentCommands: []v1.Command{ diff --git a/pkg/devfile/parser/data/v2/common/options.go b/pkg/devfile/parser/data/v2/common/options.go index b1443151..64c93219 100644 --- a/pkg/devfile/parser/data/v2/common/options.go +++ b/pkg/devfile/parser/data/v2/common/options.go @@ -20,6 +20,9 @@ type DevfileOptions struct { // ProjectOptions specifies the various options available to filter projects/starterProjects ProjectOptions ProjectOptions + + // FilterByName specifies the name for the particular devfile object that's been looking for + FilterByName string } // CommandOptions specifies the various options available to filter commands diff --git a/pkg/devfile/parser/data/v2/components.go b/pkg/devfile/parser/data/v2/components.go index 13a905b6..6b88034f 100644 --- a/pkg/devfile/parser/data/v2/components.go +++ b/pkg/devfile/parser/data/v2/components.go @@ -35,7 +35,9 @@ func (d *DevfileV2) GetComponents(options common.DevfileOptions) ([]v1.Component continue } - components = append(components, component) + if options.FilterByName == "" || component.Name == options.FilterByName { + components = append(components, component) + } } return components, nil diff --git a/pkg/devfile/parser/data/v2/components_test.go b/pkg/devfile/parser/data/v2/components_test.go index 03696364..c0480b15 100644 --- a/pkg/devfile/parser/data/v2/components_test.go +++ b/pkg/devfile/parser/data/v2/components_test.go @@ -360,6 +360,54 @@ func TestGetDevfileComponents(t *testing.T) { }, wantComponents: []string{"comp3"}, }, + { + name: "Get component with the specified name", + component: []v1.Component{ + { + Name: "comp1", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{}, + }, + }, + { + Name: "comp2", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{}, + }, + }, + { + Name: "comp3", + ComponentUnion: v1.ComponentUnion{ + Volume: &v1.VolumeComponent{}, + }, + }, + }, + filterOptions: common.DevfileOptions{ + FilterByName: "comp3", + }, + wantComponents: []string{"comp3"}, + }, + { + name: "component name not found", + component: []v1.Component{ + { + Name: "comp1", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{}, + }, + }, + { + Name: "comp2", + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{}, + }, + }, + }, + filterOptions: common.DevfileOptions{ + FilterByName: "comp3", + }, + wantComponents: []string{}, + }, { name: "Wrong filter for component", component: []v1.Component{ diff --git a/pkg/devfile/parser/data/v2/projects.go b/pkg/devfile/parser/data/v2/projects.go index c90fb2bf..714ed5be 100644 --- a/pkg/devfile/parser/data/v2/projects.go +++ b/pkg/devfile/parser/data/v2/projects.go @@ -33,8 +33,9 @@ func (d *DevfileV2) GetProjects(options common.DevfileOptions) ([]v1.Project, er if options.ProjectOptions.ProjectSourceType != "" && projectSourceType != options.ProjectOptions.ProjectSourceType { continue } - - projects = append(projects, project) + if options.FilterByName == "" || project.Name == options.FilterByName { + projects = append(projects, project) + } } return projects, nil @@ -118,7 +119,9 @@ func (d *DevfileV2) GetStarterProjects(options common.DevfileOptions) ([]v1.Star continue } - starterProjects = append(starterProjects, starterProject) + if options.FilterByName == "" || starterProject.Name == options.FilterByName { + starterProjects = append(starterProjects, starterProject) + } } return starterProjects, nil diff --git a/pkg/devfile/parser/data/v2/projects_test.go b/pkg/devfile/parser/data/v2/projects_test.go index b79c4952..683181a9 100644 --- a/pkg/devfile/parser/data/v2/projects_test.go +++ b/pkg/devfile/parser/data/v2/projects_test.go @@ -77,6 +77,48 @@ func TestDevfile200_GetProjects(t *testing.T) { }, wantProjects: []string{"project1"}, }, + { + name: "Get project with the specified name", + currentProjects: []v1.Project{ + { + Name: "project1", + ProjectSource: v1.ProjectSource{ + Git: &v1.GitProjectSource{}, + }, + }, + { + Name: "project2", + ProjectSource: v1.ProjectSource{ + Zip: &v1.ZipProjectSource{}, + }, + }, + }, + filterOptions: common.DevfileOptions{ + FilterByName: "project2", + }, + wantProjects: []string{"project2"}, + }, + { + name: "project name not found", + currentProjects: []v1.Project{ + { + Name: "project1", + ProjectSource: v1.ProjectSource{ + Git: &v1.GitProjectSource{}, + }, + }, + { + Name: "project2", + ProjectSource: v1.ProjectSource{ + Zip: &v1.ZipProjectSource{}, + }, + }, + }, + filterOptions: common.DevfileOptions{ + FilterByName: "project3", + }, + wantProjects: []string{}, + }, { name: "Wrong filter for projects", currentProjects: []v1.Project{ @@ -493,6 +535,48 @@ func TestDevfile200_GetStarterProjects(t *testing.T) { }, wantStarterProjects: []string{"project1", "project3"}, }, + { + name: "Get starter project with specified name", + currentStarterProjects: []v1.StarterProject{ + { + Name: "project1", + ProjectSource: v1.ProjectSource{ + Git: &v1.GitProjectSource{}, + }, + }, + { + Name: "project2", + ProjectSource: v1.ProjectSource{ + Zip: &v1.ZipProjectSource{}, + }, + }, + }, + filterOptions: common.DevfileOptions{ + FilterByName: "project2", + }, + wantStarterProjects: []string{"project2"}, + }, + { + name: "starter project name not found", + currentStarterProjects: []v1.StarterProject{ + { + Name: "project1", + ProjectSource: v1.ProjectSource{ + Git: &v1.GitProjectSource{}, + }, + }, + { + Name: "project2", + ProjectSource: v1.ProjectSource{ + Zip: &v1.ZipProjectSource{}, + }, + }, + }, + filterOptions: common.DevfileOptions{ + FilterByName: "project3", + }, + wantStarterProjects: []string{}, + }, { name: "Wrong filter for starter projects", currentStarterProjects: []v1.StarterProject{