Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a project to be specified in --downloadSource #3044

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions pkg/odo/cli/component/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/openshift/odo/pkg/component"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/devfile"
"github.com/openshift/odo/pkg/devfile/parser/data/common"
"github.com/openshift/odo/pkg/envinfo"
"github.com/openshift/odo/pkg/kclient"
"github.com/openshift/odo/pkg/log"
Expand Down Expand Up @@ -64,7 +65,7 @@ type DevfileMetadata struct {
devfileSupport bool
devfileLink string
devfileRegistry catalog.Registry
downloadSource bool
downloadSource string
}

// CreateRecommendedCommandName is the recommended watch command name
Expand Down Expand Up @@ -109,7 +110,12 @@ var createExample = ktemplates.Examples(` # Create new Node.js component with t
%[1]s nodejs --git https://github.com/openshift/nodejs-ex.git

# Create new Node.js component with custom ports, additional environment variables and memory and cpu limits
%[1]s nodejs --port 8080,8100/tcp,9100/udp --env key=value,key1=value1 --memory 4Gi --cpu 2`)
%[1]s nodejs --port 8080,8100/tcp,9100/udp --env key=value,key1=value1 --memory 4Gi --cpu 2

# Create new Node.js component and download the sample project named nodejs-web-app
%[1]s nodejs --downloadSource=nodejs-web-app`)

const defaultProjectName = "devfile-project-name"

// NewCreateOptions returns new instance of CreateOptions
func NewCreateOptions() *CreateOptions {
Expand Down Expand Up @@ -702,7 +708,8 @@ func (co *CreateOptions) Validate() (err error) {

// Downloads first project from list of projects in devfile
// Currenty type git with a non github url is not supported
func (co *CreateOptions) downloadProject() error {
func (co *CreateOptions) downloadProject(projectPassed string) error {
var project common.DevfileProject
devObj, err := devfile.Parse(DevfilePath)
if err != nil {
return err
Expand All @@ -713,11 +720,24 @@ func (co *CreateOptions) downloadProject() error {
return errors.Errorf("No project found in devfile component.")
}

if nOfProjects > 1 {
log.Warning("Only downloading first project from list.")
}
if nOfProjects == 1 && projectPassed == defaultProjectName {
project = projects[0]
} else if nOfProjects > 1 && projectPassed == defaultProjectName {
project = projects[0]
log.Warning("There are multiple projects in this devfile but none have been specified in --downloadSource. Downloading the first: " + project.Name)
} else { //If the user has specified a project
projectFound := false
for indexOfProject, projectInfo := range projects {
if projectInfo.Name == projectPassed { //Get the index
project = projects[indexOfProject]
projectFound = true
}
}

project := projects[0]
if !projectFound {
return errors.Errorf("The project: %s specified in --downloadSource does not exist", projectPassed)
}
}

path, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -786,8 +806,8 @@ func (co *CreateOptions) Run() (err error) {
}
}

if util.CheckPathExists(DevfilePath) && co.devfileMetadata.downloadSource {
err = co.downloadProject()
if util.CheckPathExists(DevfilePath) && co.devfileMetadata.downloadSource != "" {
err = co.downloadProject(co.devfileMetadata.downloadSource)
if err != nil {
return errors.Wrap(err, "Failed to download project for devfile component")
}
Expand Down Expand Up @@ -906,8 +926,9 @@ func NewCmdCreate(name, fullName string) *cobra.Command {
componentCreateCmd.Flags().StringSliceVar(&co.componentEnvVars, "env", []string{}, "Environmental variables for the component. For example --env VariableName=Value")

if experimental.IsExperimentalModeEnabled() {
componentCreateCmd.Flags().StringVar(&co.devfileMetadata.downloadSource, "downloadSource", "", "Download sample project from devfile.")
componentCreateCmd.Flags().Lookup("downloadSource").NoOptDefVal = defaultProjectName //Default value to pass to the flag if one is not specified.
componentCreateCmd.Flags().StringVar(&co.devfileMetadata.devfileRegistry.Name, "registry", "", "Create devfile component from specific registry")
componentCreateCmd.Flags().BoolVar(&co.devfileMetadata.downloadSource, "downloadSource", false, "Download sample project from devfile. (ex. odo component create <component_type> [component_name] --downloadSource")
}

componentCreateCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
Expand Down
31 changes: 31 additions & 0 deletions tests/integration/devfile/cmd_devfile_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,37 @@ var _ = Describe("odo devfile create command tests", func() {
})
})

Context("When executing odo create with devfile component and --downloadSource flag with a valid project", func() {
It("should succesfully create the compoment specified and download the source", func() {
helper.CmdShouldPass("odo", "preference", "set", "Experimental", "true")
contextDevfile := helper.CreateNewContext()
helper.Chdir(contextDevfile)
devfile := "devfile.yaml"
helper.CmdShouldPass("odo", "create", "nodejs", "--downloadSource=nodejs-web-app")
expectedFiles := []string{"package.json", "package-lock.json", "README.md", devfile}
Expect(helper.VerifyFilesExist(contextDevfile, expectedFiles)).To(Equal(true))
helper.DeleteDir(contextDevfile)
helper.Chdir(context)
})
})

Context("When executing odo create with an invalid project specified in --downloadSource", func() {
It("should fail with please run 'The project: invalid-project-name specified in --downloadSource does not exist'", func() {
invalidProjectName := "invalid-project-name"
output := helper.CmdShouldFail("odo", "create", "nodejs", "--downloadSource=invalid-project-name")
expectedString := "The project: " + invalidProjectName + " specified in --downloadSource does not exist"
helper.MatchAllInOutput(output, []string{expectedString})
})
})

Context("When executing odo create using --downloadSource with a devfile component that contains no projects", func() {
It("should fail with please run 'No project found in devfile component.'", func() {
output := helper.CmdShouldFail("odo", "create", "maven", "--downloadSource")
expectedString := "No project found in devfile component."
helper.MatchAllInOutput(output, []string{expectedString})
})
})

// Currently these tests need interactive mode in order to set the name of the component.
// Once this feature is added we can change these tests.
//Context("When executing odo create with devfile component and --downloadSource flag with github type", func() {
Expand Down