Skip to content

Commit

Permalink
Personalize devfile (#5467)
Browse files Browse the repository at this point in the history
* Fix: Ask for devfile personalization

* Update devfile library

Signed-off-by: Parthvi Vala <[email protected]>

* Add todo

* Print live configuration, make it work

* Run mockgen script

Signed-off-by: Parthvi Vala <[email protected]>

* TODO

* Add missing mock methods

* Add review suggestions

* Add mock methods

* Add unit tests

* Add mock methods post rebase

* Fixes post rebase

Signed-off-by: Parthvi Vala <[email protected]>

* Review, only run devfile personalization if the dir is not empty and is interactive mode

* Mock methods

* Fix unit tests

Signed-off-by: Parthvi Vala <[email protected]>

* Review

Signed-off-by: Parthvi Vala <[email protected]>
  • Loading branch information
valaparthvi authored Feb 24, 2022
1 parent 0e4e55b commit 39c8e40
Show file tree
Hide file tree
Showing 26 changed files with 1,300 additions and 338 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/Xuanwo/go-locale v1.0.0
github.com/blang/semver v3.5.1+incompatible
github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c
github.com/devfile/library v1.2.1-0.20220201144851-0749c7fa35a1
github.com/devfile/library v1.2.1-0.20220217161036-0f5995513e92
github.com/devfile/registry-support/index/generator v0.0.0-20211012185733-0a73f866043f
github.com/devfile/registry-support/registry-library v0.0.0-20211125162259-d7edf148d3e2
github.com/fatih/color v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ github.com/devfile/api/v2 v2.0.0-20210910153124-da620cd1a7a1/go.mod h1:kLX/nW93g
github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c h1:sjghKUov/WT71dBreHYQcTgQctxHT/p4uAhUFdGQfSU=
github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c/go.mod h1:d99eTN6QxgzihOOFyOZA+VpUyD4Q1pYRYHZ/ci9J96Q=
github.com/devfile/library v1.1.1-0.20210910214722-7c5ff63711ec/go.mod h1:svPWwWb+BP15SXCHl0dyOeE4Sohrjl5a2BaOzc/riLc=
github.com/devfile/library v1.2.1-0.20220201144851-0749c7fa35a1 h1:cenRsPWEfoCvKit8AtYnPtVlxA5z9EyVDqK3JW62er4=
github.com/devfile/library v1.2.1-0.20220201144851-0749c7fa35a1/go.mod h1:nZ9P4D8QxHdWe/czYx9sn+BxzsTOXMj/LAUfcqkvvuU=
github.com/devfile/library v1.2.1-0.20220217161036-0f5995513e92 h1:RIrd0pBAET9vLHEZGyaG1PSjp/lJXa+CZfuWiih2p6g=
github.com/devfile/library v1.2.1-0.20220217161036-0f5995513e92/go.mod h1:GSPfJaBg0+bBjBHbwBE5aerJLH6tWGQu2q2rHYd9czM=
github.com/devfile/registry-support/index/generator v0.0.0-20211012185733-0a73f866043f h1:fKNUmoOPh7yAs69uMRZWHvev+m3e7T4jBL/hOXZB9ys=
github.com/devfile/registry-support/index/generator v0.0.0-20211012185733-0a73f866043f/go.mod h1:bLGagbW2SFn7jo5+kUPlCMehIGqWkRtLKc5O0OyJMJM=
github.com/devfile/registry-support/registry-library v0.0.0-20211125162259-d7edf148d3e2 h1:Rwuc0bdx8xSZWdIwXjAxaKYnZIWHneJmDAAZ6a5jXzY=
Expand Down
118 changes: 116 additions & 2 deletions pkg/init/asker/asker.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package asker

import (
"sort"

"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/redhat-developer/odo/pkg/log"
"sort"

"github.com/redhat-developer/odo/pkg/catalog"
)
Expand Down Expand Up @@ -90,3 +91,116 @@ func (o *Survey) AskCorrect() (bool, error) {
}
return answer, nil
}

// AskPersonalizeConfiguration asks the configuration user wants to change
func (o *Survey) AskPersonalizeConfiguration(configuration ContainerConfiguration) (OperationOnContainer, error) {
options, tracker := buildPersonalizedConfigurationOptions(configuration)
configChangeQuestion := &survey.Select{
Message: "What configuration do you want change?",
Default: options[0],
Options: options,
}
var configChangeIndex int
err := survey.AskOne(configChangeQuestion, &configChangeIndex)
if err != nil {
return OperationOnContainer{}, err
}
return tracker[configChangeIndex], nil
}

// AskAddEnvVar asks the key and value for env var
func (o *Survey) AskAddEnvVar() (string, string, error) {
newEnvNameQuesion := &survey.Input{
Message: "Enter new environment variable name:",
}
var newEnvNameAnswer string
err := survey.AskOne(newEnvNameQuesion, &newEnvNameAnswer)
if err != nil {
return "", "", err
}
newEnvValueQuestion := &survey.Input{
Message: fmt.Sprintf("Enter value for %q environment variable:", newEnvNameAnswer),
}
var newEnvValueAnswer string
err = survey.AskOne(newEnvValueQuestion, &newEnvValueAnswer)
if err != nil {
return "", "", err
}
return newEnvNameAnswer, newEnvValueAnswer, nil
}

// AskAddPort asks the container name and port that user wants to add
func (o *Survey) AskAddPort() (string, error) {
newPortQuestion := &survey.Input{
Message: "Enter port number:",
}
var newPortAnswer string
log.Warning("Please ensure that you do not add a duplicate port number")
err := survey.AskOne(newPortQuestion, &newPortAnswer)
if err != nil {
return "", err
}
return newPortAnswer, nil
}

func (o *Survey) AskContainerName(containers []string) (string, error) {
selectContainerQuestion := &survey.Select{
Message: "Select container for which you want to change configuration?",
Default: containers[len(containers)-1],
Options: containers,
}
var selectContainerAnswer string
err := survey.AskOne(selectContainerQuestion, &selectContainerAnswer)
if err != nil {
return selectContainerAnswer, err
}
return selectContainerAnswer, nil
}

func (dc *DevfileConfiguration) GetContainers() []string {
keys := []string{}
for k := range *dc {
keys = append(keys, k)
}
return keys
}

func buildPersonalizedConfigurationOptions(configuration ContainerConfiguration) (options []string, tracker []OperationOnContainer) {
options = []string{
"NOTHING - configuration is correct",
}
// track the option selected by the user without relying on the UI message
tracker = []OperationOnContainer{{Ops: "Nothing"}}

// Add available ports
for _, port := range configuration.Ports {
options = append(options, fmt.Sprintf("Delete port %q", port))
tracker = append(tracker, OperationOnContainer{
Ops: "Delete",
Kind: "Port",
Key: port,
})
}
options = append(options, "Add new port")
tracker = append(tracker, OperationOnContainer{
Ops: "Add",
Kind: "Port",
})

// Add available env vars
for key := range configuration.Envs {
options = append(options, fmt.Sprintf("Delete environment variable %q", key))
tracker = append(tracker, OperationOnContainer{
Ops: "Delete",
Kind: "EnvVar",
Key: key,
})
}
options = append(options, "Add new environment variable")
tracker = append(tracker, OperationOnContainer{
Ops: "Add",
Kind: "EnvVar",
})

return
}
29 changes: 28 additions & 1 deletion pkg/init/asker/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// needed to initiate a project.
package asker

import "github.com/redhat-developer/odo/pkg/catalog"
import (
"github.com/redhat-developer/odo/pkg/catalog"
)

// Asker interactively asks for information to the user
type Asker interface {
Expand All @@ -22,4 +24,29 @@ type Asker interface {

// AskCorrect asks for confirmation
AskCorrect() (bool, error)

AskContainerName(containers []string) (string, error)

// AskPersonalizeConfiguration asks the configuration user wants to change
AskPersonalizeConfiguration(configuration ContainerConfiguration) (OperationOnContainer, error)

// AskAddEnvVar asks the key and value for env var
AskAddEnvVar() (string, string, error)

// AskAddPort asks the container name and port that user wants to add
AskAddPort() (string, error)
}

type ContainerConfiguration struct {
Ports []string
Envs map[string]string
}

type OperationOnContainer struct {
Ops string
Kind string
Key string
}

// key is container name
type DevfileConfiguration map[string]ContainerConfiguration
138 changes: 99 additions & 39 deletions pkg/init/asker/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 39c8e40

Please sign in to comment.