Skip to content

Commit

Permalink
Update preference view to show list of devfile registries (#5850)
Browse files Browse the repository at this point in the history
* Move registry list to preference view

* Remove preference registry list, fix test cases

* Fix documentation, and test cases

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* use ui.NewTable()

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Update pkg/odo/cli/preference/view.go

Co-authored-by: Armel Soro <armel@rm3l.org>

* fix test failures

Co-authored-by: Armel Soro <armel@rm3l.org>
  • Loading branch information
valaparthvi and rm3l authored Jun 27, 2022
1 parent 77ff2b8 commit 33733a0
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ For these examples, we consider we have two registries in our preferences:

```
shell
$ odo preference registry list
NAME URL SECURE
Staging https://registry.stage.devfile.io No
DefaultDevfileRegistry https://registry.devfile.io No
```
$ odo preference view
Preference parameters:
PARAMETER VALUE
ConsentTelemetry true
Ephemeral
PushTimeout
RegistryCacheTime
Timeout 5
UpdateNotification true (default)
Devfile registries:
NAME URL SECURE
Staging https://registry.stage.devfile.io No
DefaultDevfileRegistry https://registry.devfile.io No
```

To get the complete list of accessible Devfile stacks:

Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func NewCmdInit(name, fullName string) *cobra.Command {

initCmd.Flags().String(backend.FLAG_NAME, "", "name of the component to create")
initCmd.Flags().String(backend.FLAG_DEVFILE, "", "name of the devfile in devfile registry")
initCmd.Flags().String(backend.FLAG_DEVFILE_REGISTRY, "", "name of the devfile registry (as configured in \"odo preference registry list\"). It can be used in combination with --devfile, but not with --devfile-path")
initCmd.Flags().String(backend.FLAG_DEVFILE_REGISTRY, "", "name of the devfile registry (as configured in \"odo preference view\"). It can be used in combination with --devfile, but not with --devfile-path")
initCmd.Flags().String(backend.FLAG_STARTER, "", "name of the starter project")
initCmd.Flags().String(backend.FLAG_DEVFILE_PATH, "", "path to a devfile. This is an alternative to using devfile from Devfile registry. It can be local filesystem path or http(s) URL")

Expand Down
108 changes: 0 additions & 108 deletions pkg/odo/cli/preference/registry/list.go

This file was deleted.

6 changes: 2 additions & 4 deletions pkg/odo/cli/preference/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ var registryDesc = ktemplates.LongDesc(`Configure devfile registry`)
// NewCmdRegistry implements the registry configuration command
func NewCmdRegistry(name, fullName string) *cobra.Command {
registryAddCmd := NewCmdAdd(addCommandName, util.GetFullName(fullName, addCommandName))
registryListCmd := NewCmdList(listCommandName, util.GetFullName(fullName, listCommandName))
registryDeleteCmd := NewCmdDelete(deleteCommandName, util.GetFullName(fullName, deleteCommandName))

registryCmd := &cobra.Command{
Use: name,
Short: registryDesc,
Long: registryDesc,
Example: fmt.Sprintf("%s\n\n%s\n\n%s",
Example: fmt.Sprintf("%s\n\n%s",
registryAddCmd.Example,
registryListCmd.Example,
registryDeleteCmd.Example,
),
}

registryCmd.AddCommand(registryAddCmd, registryListCmd, registryDeleteCmd)
registryCmd.AddCommand(registryAddCmd, registryDeleteCmd)
registryCmd.SetUsageTemplate(util.CmdUsageTemplate)
registryCmd.Annotations = map[string]string{"command": "main"}

Expand Down
61 changes: 47 additions & 14 deletions pkg/odo/cli/preference/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package preference
import (
"context"
"fmt"
"os"
"reflect"
"text/tabwriter"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubectl/pkg/util/templates"

"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubectl/pkg/util/templates"
"github.com/redhat-developer/odo/pkg/preference"
)

const viewCommandName = "view"
Expand Down Expand Up @@ -47,19 +50,49 @@ func (o *ViewOptions) Validate() (err error) {

// Run contains the logic for the command
func (o *ViewOptions) Run(ctx context.Context) (err error) {
w := tabwriter.NewWriter(os.Stdout, 5, 2, 2, ' ', tabwriter.TabIndent)
fmt.Fprintln(w, "PARAMETER", "\t", "CURRENT_VALUE")
fmt.Fprintln(w, "UpdateNotification", "\t", showBlankIfNil(o.clientset.PreferenceClient.UpdateNotification()))
fmt.Fprintln(w, "Timeout", "\t", showBlankIfNil(o.clientset.PreferenceClient.Timeout()))
fmt.Fprintln(w, "PushTimeout", "\t", showBlankIfNil(o.clientset.PreferenceClient.PushTimeout()))
fmt.Fprintln(w, "RegistryCacheTime", "\t", showBlankIfNil(o.clientset.PreferenceClient.RegistryCacheTime()))
fmt.Fprintln(w, "Ephemeral", "\t", showBlankIfNil(o.clientset.PreferenceClient.EphemeralSourceVolume()))
fmt.Fprintln(w, "ConsentTelemetry", "\t", showBlankIfNil(o.clientset.PreferenceClient.ConsentTelemetry()))

w.Flush()
preferenceList := o.clientset.PreferenceClient.NewPreferenceList()
registryList := o.clientset.PreferenceClient.RegistryList()
HumanReadableOutput(preferenceList, registryList)
return
}

func HumanReadableOutput(preferenceList preference.PreferenceList, registryList *[]preference.Registry) {
preferenceT := ui.NewTable()
preferenceT.AppendHeader(table.Row{"PARAMETER", "VALUE"})
preferenceT.SortBy([]table.SortBy{{Name: "PARAMETER", Mode: table.Asc}})
for _, pref := range preferenceList.Items {
value := showBlankIfNil(pref.Value)
if reflect.DeepEqual(value, pref.Default) {
value = fmt.Sprintf("%v (default)", value)
}
preferenceT.AppendRow(table.Row{pref.Name, value})
}
registryT := ui.NewTable()
registryT.AppendHeader(table.Row{"NAME", "URL", "SECURE"})

var regList []preference.Registry
if registryList != nil {
regList = *registryList
}
// Loop backwards here to ensure the registry display order is correct (display latest newly added registry firstly)
for i := len(regList) - 1; i >= 0; i-- {
registry := regList[i]
secure := "No"
if registry.Secure {
secure = "Yes"
}
registryT.AppendRow(table.Row{registry.Name, registry.URL, secure})
}

log.Info("Preference parameters:")
preferenceT.Render()
log.Info("\nDevfile registries:")
if registryList == nil || len(*registryList) == 0 {
log.Warning("No devfile registries added to the configuration. Refer to `odo preference registry add -h` to add one")
return
}
registryT.Render()
}
func showBlankIfNil(intf interface{}) interface{} {
imm := reflect.ValueOf(intf)

Expand Down
64 changes: 54 additions & 10 deletions pkg/odo/cli/preference/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package preference
import (
"context"
"testing"
"time"

"github.com/golang/mock/gomock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/redhat-developer/odo/pkg/preference"

"k8s.io/utils/pointer"
)

func TestView(t *testing.T) {
Expand All @@ -36,14 +34,60 @@ func TestView(t *testing.T) {
t.Errorf("Expected nil error, got %s", err)
return
}
boolValue := true
intValue := 5
var intNilValue *int = nil
var boolNilValue *bool = nil

timeValue := 10 * time.Second
prefClient.EXPECT().UpdateNotification().Return(pointer.Bool(false))
prefClient.EXPECT().Timeout().Return(&timeValue)
prefClient.EXPECT().RegistryCacheTime().Return(&timeValue)
prefClient.EXPECT().PushTimeout().Return(&timeValue)
prefClient.EXPECT().EphemeralSourceVolume().Return(pointer.Bool(false))
prefClient.EXPECT().ConsentTelemetry().Return(pointer.Bool(false))
preferenceList := preference.PreferenceList{
TypeMeta: metav1.TypeMeta{},
Items: []preference.PreferenceItem{
{
Name: preference.UpdateNotificationSetting,
Value: boolNilValue,
Default: false,
},
{
Name: preference.PushTimeoutSetting,
Value: &intValue,
Default: preference.DefaultPushTimeout,
},
{
Name: preference.RegistryCacheTimeSetting,
Value: intNilValue,
Default: preference.DefaultRegistryCacheTime,
},
{
Name: preference.ConsentTelemetrySetting,
Value: &boolValue,
Default: preference.DefaultConsentTelemetrySetting,
},
{
Name: preference.TimeoutSetting,
Value: intNilValue,
Default: preference.DefaultTimeout,
},
{
Name: preference.EphemeralSetting,
Value: &boolValue,
Default: preference.DefaultEphemeralSetting,
},
},
}
registryList := []preference.Registry{
{
Name: preference.DefaultDevfileRegistryName,
URL: preference.DefaultDevfileRegistryURL,
Secure: false,
},
{
Name: "StagingRegistry",
URL: "https://registry.staging.devfile.io",
Secure: true,
},
}
prefClient.EXPECT().NewPreferenceList().Return(preferenceList)
prefClient.EXPECT().RegistryList().Return(&registryList)

err = opts.Run(context.Background())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tests/helper/odo_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// returns an empty string if value is not set
func GetPreferenceValue(key string) string {
stdOut := Cmd("odo", "preference", "view").ShouldPass().Out()
re := regexp.MustCompile(key + `.+`)
re := regexp.MustCompile(" " + key + `.+`)
odoConfigKeyValue := re.FindString(stdOut)
if odoConfigKeyValue == "" {
return fmt.Sprintf("%s not found", key)
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/devfile/cmd_devfile_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ = Describe("odo devfile registry command tests", func() {
})

It("Should list all default registries", func() {
output := helper.Cmd("odo", "preference", "registry", "list").ShouldPass().Out()
output := helper.Cmd("odo", "preference", "view").ShouldPass().Out()
helper.MatchAllInOutput(output, []string{"DefaultDevfileRegistry"})
})

Expand Down Expand Up @@ -84,8 +84,8 @@ var _ = Describe("odo devfile registry command tests", func() {

It("Should fail with an error with no registries", func() {
helper.Cmd("odo", "preference", "registry", "delete", "DefaultDevfileRegistry", "-f").ShouldPass()
output := helper.Cmd("odo", "preference", "registry", "list").ShouldFail().Err()
helper.MatchAllInOutput(output, []string{"No devfile registries added to the configuration. Refer `odo preference registry add -h` to add one"})
output := helper.Cmd("odo", "preference", "view").ShouldRun().Err()
helper.MatchAllInOutput(output, []string{"No devfile registries added to the configuration. Refer to `odo preference registry add -h` to add one"})
})

It("Should fail to delete the registry, when registry is not present", func() {
Expand All @@ -98,7 +98,7 @@ var _ = Describe("odo devfile registry command tests", func() {
})

It("should list newly added registry", func() {
output := helper.Cmd("odo", "preference", "registry", "list").ShouldPass().Out()
output := helper.Cmd("odo", "preference", "view").ShouldPass().Out()
helper.MatchAllInOutput(output, []string{registryName, addRegistryURL})
})

Expand Down

0 comments on commit 33733a0

Please sign in to comment.