Files
odo/pkg/init/backend/flags.go
Philippe Martin ff03b8e49a Select and pull a devfile using Alizer (#5464)
* Add alizer library and test functionality

<!--
Thank you for opening a PR! Here are some things you need to know before submitting:

1. Please read our developer guideline: https://github.com/redhat-developer/odo/wiki/Developer-Guidelines
2. Label this PR accordingly with the '/kind' line
3. Ensure you have written and ran the appropriate tests: https://github.com/redhat-developer/odo/wiki/Writing-and-running-tests
4. Read how we approve and LGTM each PR: https://github.com/redhat-developer/odo/wiki/PR-Review

Documentation:

If you are pushing a change to documentation, please read: https://github.com/redhat-developer/odo/wiki/Contributing-to-Docs
-->

**What type of PR is this:**

<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind tests
/kind documentation

Feel free to use other [labels](https://github.com/redhat-developer/odo/labels) as needed. However one of the above labels must be present or the PR will not be reviewed. This instruction is for reviewers as well.
-->

/kind feature

**What does this PR do / why we need it:**

Adds the alizer library from
https://github.com/redhat-developer/alizer/tree/main/go as part of our
implementaion of `odo dev` and `odo init`.

This builds upon @feloy 's PR located here: https://github.com/redhat-developer/odo/pull/5434

**Which issue(s) this PR fixes:**
<!--
Specifying the issue will automatically close it when this PR is merged
-->

Fixes #

**PR acceptance criteria:**

- [X] Unit test

- [X] Integration test

- [X] Documentation

**How to test changes / Special notes to the reviewer:**

N/A. Only function implementation

* New alizer version

* Use alizer for odo init

* Add integration tests

* Add Alizer to odo deploy

* review

* Ask component name for odo deploy

* Fix unit test

Co-authored-by: Charlie Drage <charlie@charliedrage.com>
2022-02-23 01:52:51 -05:00

100 lines
3.2 KiB
Go

package backend
import (
"errors"
"fmt"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
dfutil "github.com/devfile/library/pkg/util"
"github.com/redhat-developer/odo/pkg/devfile/location"
"github.com/redhat-developer/odo/pkg/preference"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)
const (
FLAG_NAME = "name"
FLAG_DEVFILE = "devfile"
FLAG_DEVFILE_REGISTRY = "devfile-registry"
FLAG_STARTER = "starter"
FLAG_DEVFILE_PATH = "devfile-path"
)
// FlagsBackend is a backend that will extract all needed information from flags passed to the command
type FlagsBackend struct {
preferenceClient preference.Client
}
func NewFlagsBackend(preferenceClient preference.Client) *FlagsBackend {
return &FlagsBackend{
preferenceClient: preferenceClient,
}
}
func (o *FlagsBackend) Validate(flags map[string]string, fs filesystem.Filesystem, dir string) error {
if flags[FLAG_NAME] == "" {
return errors.New("missing --name parameter: please add --name <name> to specify a name for the component")
}
if flags[FLAG_DEVFILE] == "" && flags[FLAG_DEVFILE_PATH] == "" {
return errors.New("either --devfile or --devfile-path parameter should be specified")
}
if flags[FLAG_DEVFILE] != "" && flags[FLAG_DEVFILE_PATH] != "" {
return errors.New("only one of --devfile or --devfile-path parameter should be specified")
}
if flags[FLAG_DEVFILE_REGISTRY] != "" && !o.preferenceClient.RegistryNameExists(flags[FLAG_DEVFILE_REGISTRY]) {
return fmt.Errorf("registry %q not found in the list of devfile registries. Please use `odo registry` command to configure devfile registries", flags[FLAG_DEVFILE_REGISTRY])
}
if flags[FLAG_DEVFILE_PATH] != "" && flags[FLAG_DEVFILE_REGISTRY] != "" {
return errors.New("--devfile-registry parameter cannot be used with --devfile-path")
}
err := dfutil.ValidateK8sResourceName("name", flags[FLAG_NAME])
if err != nil {
return err
}
empty, err := location.DirIsEmpty(fs, dir)
if err != nil {
return err
}
if !empty && flags[FLAG_STARTER] != "" {
return errors.New("--starter parameter cannot be used when the directory is not empty")
}
return nil
}
func (o *FlagsBackend) SelectDevfile(flags map[string]string, _ filesystem.Filesystem, _ string) (*DevfileLocation, error) {
return &DevfileLocation{
Devfile: flags[FLAG_DEVFILE],
DevfileRegistry: flags[FLAG_DEVFILE_REGISTRY],
DevfilePath: flags[FLAG_DEVFILE_PATH],
}, nil
}
func (o *FlagsBackend) SelectStarterProject(devfile parser.DevfileObj, flags map[string]string) (*v1alpha2.StarterProject, error) {
starter := flags[FLAG_STARTER]
if starter == "" {
return nil, nil
}
projects, err := devfile.Data.GetStarterProjects(common.DevfileOptions{})
if err != nil {
return nil, err
}
var prj v1alpha2.StarterProject
for _, prj = range projects {
if prj.Name == starter {
return &prj, nil
}
}
return nil, fmt.Errorf("starter project %q not found in devfile", starter)
}
func (o *FlagsBackend) PersonalizeName(devfile parser.DevfileObj, flags map[string]string) error {
return devfile.SetMetadataName(flags[FLAG_NAME])
}