Skip to content

Commit

Permalink
rpk: add root level --profile flag
Browse files Browse the repository at this point in the history
This will let user pick an specific profile while
running any command. Useful when you want to run
the same across multiple profiles.
  • Loading branch information
r-vasquez committed Jun 8, 2023
1 parent 2759ae4 commit 0ac68fa
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/profile/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ was the selected profile, rpk will use in-memory defaults until a new profile
is selected.
`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: validProfiles(fs, p),
ValidArgsFunction: ValidProfiles(fs, p),
Run: func(_ *cobra.Command, args []string) {
cfg, err := p.Load(fs)
out.MaybeDie(err, "unable to load config: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/profile/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ If you want to edit the current raw profile as it exists in rpk.yaml, you
can use the --raw flag.
`,
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: validProfiles(fs, p),
ValidArgsFunction: ValidProfiles(fs, p),
Run: func(_ *cobra.Command, args []string) {
cfg, err := p.Load(fs)
out.MaybeDie(err, "unable to load config: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/profile/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ variables applied. If you wish to print the current raw profile as it exists
in rpk.yaml, you can use the --raw flag.
`,
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: validProfiles(fs, p),
ValidArgsFunction: ValidProfiles(fs, p),
Run: func(_ *cobra.Command, args []string) {
cfg, err := p.Load(fs)
out.MaybeDie(err, "unable to load config: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ your configuration in one place.
return cmd
}

func validProfiles(fs afero.Fs, p *config.Params) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
func ValidProfiles(fs afero.Fs, p *config.Params) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
cfg, err := p.Load(fs)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/profile/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func newUseCommand(fs afero.Fs, p *config.Params) *cobra.Command {
Use: "use [NAME]",
Short: "Select the rpk profile to use",
Args: cobra.ExactArgs(1),
ValidArgsFunction: validProfiles(fs, p),
ValidArgsFunction: ValidProfiles(fs, p),
Run: func(_ *cobra.Command, args []string) {
cfg, err := p.Load(fs)
out.MaybeDie(err, "unable to load config: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions src/go/rpk/pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func Execute() {
}
pf := root.PersistentFlags()
pf.StringVar(&p.ConfigFlag, "config", "", "Redpanda or rpk config file; default search paths are ~/.config/rpk/rpk.yaml, $PWD, and /etc/redpanda/redpanda.yaml")
pf.StringVar(&p.Profile, "profile", "", "rpk profile to use")
pf.StringArrayVarP(&p.FlagOverrides, "config-opt", "X", nil, "Override rpk configuration settings; '-X help' for detail or '-X list' for terser detail")
pf.BoolVarP(&p.DebugLogs, "verbose", "v", false, "Enable verbose logging")

Expand All @@ -92,6 +93,7 @@ func Execute() {
}
return opts, cobra.ShellCompDirectiveNoSpace
})
root.RegisterFlagCompletionFunc("profile", profile.ValidProfiles(fs, p))

root.AddCommand(
acl.NewCommand(fs, p),
Expand Down
14 changes: 10 additions & 4 deletions src/go/rpk/pkg/config/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,18 @@ func XFlagYamlPath(x string) (string, bool) {
// Params contains rpk-wide configuration parameters.
type Params struct {
// ConfigFlag is any flag-specified config path.
//
// This is unused until step (2) in the refactoring process.
ConfigFlag string

// Profile is any flag-specified profile name.
Profile string

// DebugLogs opts into debug logging.
//
// This field only for setting, to actually get a logger after the
// field is set, use Logger().
DebugLogs bool

// FlagOverrides are any flag-specified config overrides.
//
// This is unused until step (2) in the refactoring process.
FlagOverrides []string

loggerOnce sync.Once
Expand Down Expand Up @@ -949,6 +948,13 @@ func (p *Params) readRpkConfig(fs afero.Fs, c *Config) error {
}
yaml.Unmarshal(file, &c.rpkYamlActual)

if p.Profile != "" {
if c.rpkYaml.Profile(p.Profile) == nil {
return fmt.Errorf("selected profile %q does not exist", p.Profile)
}
c.rpkYaml.CurrentProfile = p.Profile
c.rpkYamlActual.CurrentProfile = p.Profile
}
c.rpkYamlExists = true
c.rpkYaml.fileLocation = abs
c.rpkYamlActual.fileLocation = abs
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/out/color.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Redpanda Data, Inc.
// Copyright 2023 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
Expand Down

0 comments on commit 0ac68fa

Please sign in to comment.