-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add dev preview capability #668
Changes from 4 commits
5bba8e9
0a76500
16cee3e
53640c8
d7453c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,43 @@ | ||
// This file contains functions that add common arguments to the command line | ||
|
||
package arguments | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/redhat-developer/app-services-cli/internal/config" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/debug" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// AddDebugFlag adds the '--debug' flag to the given set of command line flags | ||
func AddDebugFlag(fs *pflag.FlagSet) { | ||
debug.AddFlag(fs) | ||
} | ||
|
||
// Enables dev preview in config | ||
func EnableDevPreview(f *factory.Factory, enablement *string) (*config.Config, error) { | ||
if *enablement == "" { | ||
// Flag not present no action needed. | ||
return nil, nil | ||
} | ||
|
||
logger, err := f.Logger() | ||
if err != nil { | ||
logger.Info("Cannot enable dev preview") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will fail because the logger is not created. |
||
return nil, err | ||
} | ||
config, err := f.Config.Load() | ||
if err != nil { | ||
logger.Info("Cannot enable dev preview") | ||
return nil, err | ||
} | ||
|
||
config.DevPreviewEnabled = strings.ToLower(*enablement) == "true" || *enablement == "yes" || *enablement == "y" | ||
err = f.Config.Save(config) | ||
if err != nil { | ||
logger.Info("Cannot enable dev preview") | ||
return nil, err | ||
} | ||
return config, err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"flag" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry" | ||
"github.com/redhat-developer/app-services-cli/pkg/profile" | ||
|
||
"github.com/redhat-developer/app-services-cli/internal/build" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/login" | ||
|
@@ -36,7 +37,6 @@ func NewRootCommand(f *factory.Factory, version string) *cobra.Command { | |
cmd.Version = version | ||
|
||
cmd.SetVersionTemplate(f.Localizer.MustLocalize("version.cmd.outputText", localize.NewEntry("Version", build.Version))) | ||
|
||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
|
||
fs := cmd.PersistentFlags() | ||
|
@@ -46,17 +46,25 @@ func NewRootCommand(f *factory.Factory, version string) *cobra.Command { | |
var help bool | ||
fs.BoolVarP(&help, "help", "h", false, f.Localizer.MustLocalize("root.cmd.flag.help.description")) | ||
|
||
devpreview := "" | ||
cmd.Flags().StringVar(&devpreview, "devpreview", "", f.Localizer.MustLocalize("root.cmd.flag.devpreview.description")) | ||
_, _ = profile.EnableDevPreview(f, &devpreview) | ||
|
||
// Child commands | ||
cmd.AddCommand(login.NewLoginCmd(f)) | ||
cmd.AddCommand(logout.NewLogoutCommand(f)) | ||
cmd.AddCommand(kafka.NewKafkaCommand(f)) | ||
cmd.AddCommand(registry.NewServiceRegistryCommand(f)) | ||
cmd.AddCommand(serviceaccount.NewServiceAccountCommand(f)) | ||
cmd.AddCommand(cluster.NewClusterCommand(f)) | ||
cmd.AddCommand(status.NewStatusCommand(f)) | ||
cmd.AddCommand(completion.NewCompletionCommand(f)) | ||
cmd.AddCommand(whoami.NewWhoAmICmd(f)) | ||
cmd.AddCommand(cliversion.NewVersionCmd(f)) | ||
|
||
// Dev preview commands | ||
if profile.DevPreviewEnabled(f) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have investigated using custom help message template based on the annotations, however that will require pulling some large parts of the internal cobra functions etc. so decided to simplify it by listing those commands bellow. This will be mostly seen by us so there is no big deal with it. |
||
cmd.AddCommand(registry.NewServiceRegistryCommand(f)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❯ ./rhoas --devpreview=yes registry
Error: unknown command "registry" for "rhoas" How do I get this to work? |
||
} | ||
|
||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// This file contains functions that help to manage visibility of early stage commands | ||
package profile | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/redhat-developer/app-services-cli/internal/config" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory" | ||
"github.com/redhat-developer/app-services-cli/pkg/color" | ||
) | ||
|
||
// Visual element displayed in help | ||
func DevPreviewLabel() string { | ||
return color.Info("[Preview] ") | ||
} | ||
|
||
// Annotation used in templates and tools like documentation generation | ||
func DevPreviewAnnotation() map[string]string { | ||
return map[string]string{"channel": "preview"} | ||
} | ||
|
||
// Check if preview is enabled | ||
func DevPreviewEnabled(f *factory.Factory) bool { | ||
logger, err := f.Logger() | ||
if err != nil { | ||
logger.Info("Cannot determine status of dev preview. ", err) | ||
return false | ||
} | ||
config, err := f.Config.Load() | ||
if err != nil { | ||
logger.Info("Cannot determine status of dev preview. ", err) | ||
return false | ||
} | ||
|
||
return config.DevPreviewEnabled | ||
} | ||
|
||
// Enable dev preview | ||
func EnableDevPreview(f *factory.Factory, enablement *string) (*config.Config, error) { | ||
if *enablement == "" { | ||
// Flag not present no action needed. | ||
return nil, nil | ||
} | ||
|
||
logger, err := f.Logger() | ||
if err != nil { | ||
logger.Info("Cannot enable dev preview. ", err) | ||
return nil, err | ||
} | ||
config, err := f.Config.Load() | ||
if err != nil { | ||
logger.Info("Cannot enable dev preview.", err) | ||
return nil, err | ||
} | ||
|
||
config.DevPreviewEnabled = strings.ToLower(*enablement) == "true" || *enablement == "yes" || *enablement == "y" | ||
err = f.Config.Save(config) | ||
if err != nil { | ||
logger.Info("Cannot enable dev preview. ", err) | ||
return nil, err | ||
} | ||
logger.Info("Successfully changed visibility of devpreview commands") | ||
return config, err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package profile | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory" | ||
) | ||
|
||
func TestEnableDevPreviewConfig(t *testing.T) { | ||
testVal := "" | ||
config, err := EnableDevPreview(factory.New("dev", nil), &testVal) | ||
if config != nil { | ||
t.Errorf("TestEnableDevPreviewConfig config = %v, want %v", config, nil) | ||
} | ||
if err != nil { | ||
t.Errorf("TestEnableDevPreviewConfig error = %v, want %v", err, nil) | ||
} | ||
|
||
testVal = "giberish" | ||
config, err = EnableDevPreview(factory.New("dev", nil), &testVal) | ||
if config.DevPreviewEnabled == true { | ||
t.Errorf("TestEnableDevPreviewConfig() config.DevPreviewEnabled = %v, want %v", config.DevPreviewEnabled, false) | ||
} | ||
if err != nil { | ||
t.Errorf("TestEnableDevPreviewConfig error = %v, want %v", err, nil) | ||
} | ||
|
||
testVal = "true" | ||
config, err = EnableDevPreview(factory.New("dev", nil), &testVal) | ||
if config.DevPreviewEnabled != true { | ||
t.Errorf("TestEnableDevPreviewConfig config.DevPreviewEnabled = %v, want %v", config.DevPreviewEnabled, true) | ||
} | ||
if err != nil { | ||
t.Errorf("TestEnableDevPreviewConfig error = %v, want %v", err, nil) | ||
} | ||
|
||
testVal = "yes" | ||
config, err = EnableDevPreview(factory.New("dev", nil), &testVal) | ||
if config.DevPreviewEnabled != true { | ||
t.Errorf("TestEnableDevPreviewConfig config.DevPreviewEnabled = %v, want %v", config.DevPreviewEnabled, true) | ||
} | ||
if err != nil { | ||
t.Errorf("TestEnableDevPreviewConfig error = %v, want %v", err, nil) | ||
} | ||
} | ||
|
||
func TestDevPreviewEnabled(t *testing.T) { | ||
f := factory.New("dev", nil) | ||
testVal := "false" | ||
config, _ := EnableDevPreview(f, &testVal) | ||
t.Log("Ess", config) | ||
enabled := DevPreviewEnabled(f) | ||
|
||
if enabled { | ||
t.Errorf("TestEnableDevPreviewConfig enabled = %v, want %v", enabled, false) | ||
} | ||
|
||
testVal = "true" | ||
_, _ = EnableDevPreview(f, &testVal) | ||
|
||
enabled = DevPreviewEnabled(f) | ||
if !enabled { | ||
t.Errorf("TestEnableDevPreviewConfig enabled = %v, want %v", enabled, true) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do i18n as last step on the feature branch. Ignore non externalized strings for now