|
| 1 | +package get |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/AlecAivazis/survey/v2" |
| 6 | + "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil" |
| 7 | + "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil" |
| 8 | + "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" |
| 9 | + "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump" |
| 10 | + "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" |
| 11 | + "github.com/redhat-developer/app-services-cli/pkg/core/localize" |
| 12 | + "github.com/redhat-developer/app-services-cli/pkg/core/logging" |
| 13 | + "github.com/redhat-developer/app-services-cli/pkg/core/servicecontext" |
| 14 | + "github.com/redhat-developer/app-services-cli/pkg/shared/factory" |
| 15 | + "github.com/spf13/cobra" |
| 16 | + |
| 17 | + "github.com/redhat-developer/app-services-cli/pkg/shared/contextutil" |
| 18 | +) |
| 19 | + |
| 20 | +type options struct { |
| 21 | + IO *iostreams.IOStreams |
| 22 | + Connection factory.ConnectionFunc |
| 23 | + Logger logging.Logger |
| 24 | + localizer localize.Localizer |
| 25 | + Context context.Context |
| 26 | + ServiceContext servicecontext.IContext |
| 27 | + |
| 28 | + registryID string |
| 29 | + settingName string |
| 30 | + output string |
| 31 | +} |
| 32 | + |
| 33 | +// NewGetCommand creates a new command to get a service registry setting |
| 34 | +func NewGetCommand(f *factory.Factory) *cobra.Command { |
| 35 | + |
| 36 | + opts := &options{ |
| 37 | + IO: f.IOStreams, |
| 38 | + Connection: f.Connection, |
| 39 | + Logger: f.Logger, |
| 40 | + localizer: f.Localizer, |
| 41 | + Context: f.Context, |
| 42 | + ServiceContext: f.ServiceContext, |
| 43 | + } |
| 44 | + |
| 45 | + cmd := &cobra.Command{ |
| 46 | + Use: "get", |
| 47 | + Short: f.Localizer.MustLocalize("setting.get.cmd.description.short"), |
| 48 | + Long: f.Localizer.MustLocalize("setting.get.cmd.description.long"), |
| 49 | + Example: f.Localizer.MustLocalize("setting.get.cmd.example"), |
| 50 | + Args: cobra.NoArgs, |
| 51 | + RunE: func(cmd *cobra.Command, _ []string) (err error) { |
| 52 | + |
| 53 | + if opts.settingName == "" { |
| 54 | + if !opts.IO.CanPrompt() { |
| 55 | + return flagutil.RequiredWhenNonInteractiveError("setting-name") |
| 56 | + } |
| 57 | + err = runInteractivePrompt(opts) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + registryInstance, err := contextutil.GetCurrentRegistryInstance(f) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + opts.registryID = registryInstance.GetId() |
| 69 | + |
| 70 | + return runGet(opts) |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + flags := rulecmdutil.NewFlagSet(cmd, f) |
| 75 | + |
| 76 | + flags.AddRegistryInstance(&opts.registryID) |
| 77 | + |
| 78 | + flags.StringVarP(&opts.settingName, "setting-name", "n", "", f.Localizer.MustLocalize("setting.get.cmd.flag.settingName.description")) |
| 79 | + |
| 80 | + flags.AddOutput(&opts.output) |
| 81 | + |
| 82 | + return cmd |
| 83 | +} |
| 84 | + |
| 85 | +func runGet(opts *options) error { |
| 86 | + conn, err := opts.Connection() |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + api := conn.API() |
| 92 | + |
| 93 | + a, _, err := api.ServiceRegistryInstance(opts.registryID) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + request := a.AdminApi.GetConfigProperty(opts.Context, opts.settingName) |
| 99 | + |
| 100 | + configProperty, _, err := request.Execute() |
| 101 | + if err != nil { |
| 102 | + return registrycmdutil.TransformInstanceError(err) |
| 103 | + } |
| 104 | + |
| 105 | + return dump.Formatted(opts.IO.Out, opts.output, configProperty) |
| 106 | +} |
| 107 | + |
| 108 | +func runInteractivePrompt(opts *options) (err error) { |
| 109 | + |
| 110 | + settingNamePrompt := &survey.Input{ |
| 111 | + Message: opts.localizer.MustLocalize("setting.get.input.settingName.message"), |
| 112 | + } |
| 113 | + |
| 114 | + err = survey.AskOne(settingNamePrompt, &opts.settingName) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
0 commit comments