Files
odo/pkg/init/backend/interactive.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

107 lines
2.7 KiB
Go

package backend
import (
"fmt"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
parsercommon "github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/redhat-developer/odo/pkg/catalog"
"github.com/redhat-developer/odo/pkg/init/asker"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)
const (
STATE_ASK_LANG = iota
STATE_ASK_TYPE
STATE_END
)
// InteractiveBackend is a backend that will ask information interactively using the `asker` package
type InteractiveBackend struct {
askerClient asker.Asker
catalogClient catalog.Client
}
func NewInteractiveBackend(askerClient asker.Asker, catalogClient catalog.Client) *InteractiveBackend {
return &InteractiveBackend{
askerClient: askerClient,
catalogClient: catalogClient,
}
}
func (o *InteractiveBackend) Validate(flags map[string]string, fs filesystem.Filesystem, dir string) error {
return nil
}
func (o *InteractiveBackend) SelectDevfile(flags map[string]string, _ filesystem.Filesystem, _ string) (*DevfileLocation, error) {
result := &DevfileLocation{}
devfileEntries, _ := o.catalogClient.ListDevfileComponents("")
langs := devfileEntries.GetLanguages()
state := STATE_ASK_LANG
var lang string
var err error
var details catalog.DevfileComponentType
loop:
for {
switch state {
case STATE_ASK_LANG:
lang, err = o.askerClient.AskLanguage(langs)
if err != nil {
return nil, err
}
state = STATE_ASK_TYPE
case STATE_ASK_TYPE:
types := devfileEntries.GetProjectTypes(lang)
var back bool
back, details, err = o.askerClient.AskType(types)
if err != nil {
return nil, err
}
if back {
state = STATE_ASK_LANG
continue loop
}
result.DevfileRegistry = details.Registry.Name
result.Devfile = details.Name
state = STATE_END
case STATE_END:
break loop
}
}
return result, nil
}
func (o *InteractiveBackend) SelectStarterProject(devfile parser.DevfileObj, flags map[string]string) (*v1alpha2.StarterProject, error) {
starterProjects, err := devfile.Data.GetStarterProjects(parsercommon.DevfileOptions{})
if err != nil {
return nil, err
}
names := make([]string, 0, len(starterProjects))
for _, starterProject := range starterProjects {
names = append(names, starterProject.Name)
}
ok, starter, err := o.askerClient.AskStarterProject(names)
if err != nil {
return nil, err
}
if !ok {
return nil, nil
}
return &starterProjects[starter], nil
}
func (o *InteractiveBackend) PersonalizeName(devfile parser.DevfileObj, flags map[string]string) error {
name, err := o.askerClient.AskName(fmt.Sprintf("my-%s-app", devfile.Data.GetMetadata().Name))
if err != nil {
return err
}
return devfile.SetMetadataName(name)
}