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

93 lines
2.9 KiB
Go

package backend
import (
"fmt"
"reflect"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/redhat-developer/alizer/go/pkg/apis/recognizer"
"github.com/redhat-developer/odo/pkg/catalog"
"github.com/redhat-developer/odo/pkg/init/asker"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)
type AlizerBackend struct {
askerClient asker.Asker
catalogClient catalog.Client
}
func NewAlizerBackend(askerClient asker.Asker, catalogClient catalog.Client) *AlizerBackend {
return &AlizerBackend{
askerClient: askerClient,
catalogClient: catalogClient,
}
}
func (o *AlizerBackend) Validate(flags map[string]string, fs filesystem.Filesystem, dir string) error {
return nil
}
// detectFramework uses the alizer library in order to detect the devfile
// to use depending on the files in the path
func (o *AlizerBackend) detectFramework(path string) (recognizer.DevFileType, catalog.Registry, error) {
types := []recognizer.DevFileType{}
components, err := o.catalogClient.ListDevfileComponents("")
if err != nil {
return recognizer.DevFileType{}, catalog.Registry{}, err
}
for _, component := range components.Items {
types = append(types, recognizer.DevFileType{
Name: component.Name,
Language: component.Language,
ProjectType: component.ProjectType,
Tags: component.Tags,
})
}
typ, err := recognizer.SelectDevFileFromTypes(path, types)
if err != nil {
return recognizer.DevFileType{}, catalog.Registry{}, err
}
// TODO(feloy): This part won't be necessary when SelectDevFileFromTypes returns the index
var indexOfDetected int
for i, typeFromList := range types {
if reflect.DeepEqual(typeFromList, typ) {
indexOfDetected = i
break
}
}
registry := components.Items[indexOfDetected].Registry
return typ, registry, nil
}
// SelectDevfile calls thz Alizer to detect the devfile and asks for confirmation to the user
func (o *AlizerBackend) SelectDevfile(flags map[string]string, fs filesystem.Filesystem, dir string) (location *DevfileLocation, err error) {
selected, registry, err := o.detectFramework(dir)
if err != nil {
return nil, err
}
fmt.Printf("Based on the files in the current directory odo detected\nLanguage: %s\nProject type: %s\n", selected.Language, selected.ProjectType)
fmt.Printf("The devfile %q from the registry %q will be downloaded.\n", selected.Name, registry.Name)
confirm, err := o.askerClient.AskCorrect()
if err != nil {
return nil, err
}
if !confirm {
return nil, nil
}
return &DevfileLocation{
Devfile: selected.Name,
DevfileRegistry: registry.Name,
}, nil
}
func (o *AlizerBackend) SelectStarterProject(devfile parser.DevfileObj, flags map[string]string) (starter *v1alpha2.StarterProject, err error) {
return nil, nil
}
func (o *AlizerBackend) PersonalizeName(devfile parser.DevfileObj, flags map[string]string) error {
return nil
}