Skip to content

Conversation

david-wu-octopus
Copy link
Contributor

@david-wu-octopus david-wu-octopus commented Aug 22, 2024

Description

Closes #19528

This PR implements reusable functionality for interactive prompts:

  • Interactive prompts can be toggled on or off using the prompts-enabled flag in local configuration.
  • It adds a new CLI command argocd configure with flag --prompts-enabled which can be used to toggle the prompts-enabled flag in local configuration
  • It adds a new global CLI flag --force-prompts-enabled which will override the local configuration value if specified

Usage and examples:

How to add interactive prompts to a command:

  1. When initialising the command, pass in an instance of *apiclient.ClientOptions
  2. When defining the business logic for the command, create a new prompt using the util NewPrompt(clientOpts *apiclient.ClientOptions) (*Prompt, error) and then call (p *Prompt) Confirm(message string) bool in the command's logic

Example: argocd admin export command

admin.go:

command.AddCommand(NewExportCommand(clientOpts))

backup.go:

func NewExportCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
	
        // ...

	command := cobra.Command{
		Use:   "export",
		Short: "Export all Argo CD data to stdout (default) or a file",
		Run: func(c *cobra.Command, args []string) {

			// ...

			prompt, err := utils.NewPrompt(clientOpts.PromptsEnabled)
			errors.CheckError(err)

			if !prompt.Confirm("Are you sure you want to export? (y/n) ") {
				return
			}

			// ...

		},
	}

	// ...

	return &command
}

CLI examples

By default, optional prompts are disabled:

argocd admin export
# All Argo CD data is printed in the terminal

We can force prompts to be enabled for the current command only using a global flag --prompts-enabled:

argocd admin export --force-prompts-enabled
Are you sure you want to export? (y/n) n

A new local configuration field, prompts-enabled, can be set to enable optional prompts for all commands as the default. To set this field, we can use a new command argocd configuration --prompts-enabled (where --prompts-enabled=false will turn off optional interactive prompts):

argocd configure --prompts-enabled
Successfully updated the following configuration settings:
prompts-enabled: true
argocd admin export
Are you sure you want to export? (y/n) y
# All Argo CD data is printed in the terminal

The field will be added to the local configuration file if it isn't there already

The global flag --force-prompts-enabled overrides the local configuration value:

argocd admin export --force-prompts-enabled=false
# All Argo CD data is printed in the terminal

Checklist:

  • Either (a) I've created an enhancement proposal and discussed it with the community, (b) this is a bug fix, or (c) this does not need to be in the release notes.
  • The title of the PR states what changed and the related issues number (used for the release note).
  • The title of the PR conforms to the Toolchain Guide
  • I've included "Closes [ISSUE #]" or "Fixes [ISSUE #]" in the description to automatically close the associated issue.
  • I've updated both the CLI and UI to expose my feature, or I plan to submit a second PR with them.
  • Does this PR require documentation updates?
  • I've updated documentation as required by this PR.
  • I have signed off all my commits as required by DCO
  • I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
  • My build is green (troubleshooting builds).
  • My new feature complies with the feature status guidelines.
  • I have added a brief description of why this PR is necessary and/or what this PR solves.
  • Optional. My organization is added to USERS.md.
  • Optional. For bug fixes, I've indicated what older releases this fix should be cherry-picked into (this may or may not happen depending on risk/complexity).

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
…eSettingsManager()` and `commandContext.CreateSettingsManager()` exportable

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Copy link

bunnyshell bot commented Aug 22, 2024

❌ Preview Environment deleted from Bunnyshell

Available commands (reply to this comment):

  • 🚀 /bns:deploy to deploy the environment

Copy link

bunnyshell bot commented Aug 22, 2024

❌ Preview Environment deleted from Bunnyshell

Available commands (reply to this comment):

  • 🚀 /bns:deploy to deploy the environment

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Copy link

codecov bot commented Aug 22, 2024

Codecov Report

Attention: Patch coverage is 84.48276% with 9 lines in your changes missing coverage. Please review.

Project coverage is 55.18%. Comparing base (ec499bb) to head (2fd0cbe).
Report is 409 commits behind head on master.

Files with missing lines Patch % Lines
util/localconfig/localconfig.go 66.66% 4 Missing and 2 partials ⚠️
cmd/argocd/commands/root.go 0.00% 2 Missing ⚠️
cmd/argocd/commands/utils/prompt.go 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #19637      +/-   ##
==========================================
+ Coverage   55.07%   55.18%   +0.10%     
==========================================
  Files         322      324       +2     
  Lines       54927    55078     +151     
==========================================
+ Hits        30253    30393     +140     
+ Misses      22068    22060       -8     
- Partials     2606     2625      +19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
@nitishfy
Copy link
Member

Hi @david-wu-octopus , can you please link the issue this PR is associated with?

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
enabled bool
}

func NewPrompt(clientOpts *apiclient.ClientOptions) (*Prompt, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've decided to pass in clientOpts *apiclient.ClientOptions here to reduce duplicated set up code and to make clear to other contributors that the boolean value for the prompts-enabled setting is supposed to come from local config

If we instead passed through a bool value as the argument, it may not be clear to other developers where this value comes from

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thats make sense, but i would just apply config path instead whole structure. Because here a lot of redundant fields in clients options that not relevant for prompts

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
@david-wu-octopus david-wu-octopus changed the title Prompt config feat: Add reusable interactive prompts and configuration command (issue #19528) Aug 23, 2024
@david-wu-octopus
Copy link
Contributor Author

Hi @david-wu-octopus , can you please link the issue this PR is associated with?

Done, thank you!

@david-wu-octopus david-wu-octopus marked this pull request as ready for review August 23, 2024 05:30
@david-wu-octopus david-wu-octopus requested a review from a team as a code owner August 23, 2024 05:30
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
enabled bool
}

func NewPrompt(clientOpts *apiclient.ClientOptions) (*Prompt, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thats make sense, but i would just apply config path instead whole structure. Because here a lot of redundant fields in clients options that not relevant for prompts

fmt.Printf("prompts-enabled: %v", strconv.FormatBool(localCfg.PromptsEnabled))
},
}
command.Flags().BoolVar(&promptsEnabled, "prompts-enabled", false, "Enable (or disable) optional interactive prompts")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets change it to pointer and prompts, so we can verify existance of this flag

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, the package pflag (https://github.com/spf13/pflag) doesn't actually support pointers for boolean flags

The alternative approach I've identified is for the default --prompts-enabled and --force-prompts-enabled flag values to be read from local configuration

if err != nil {
return nil, err
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add override param on clientOpts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a global CLI flag --force-prompts-enabled that will override the value in local config if it's set

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
…or `--prompts-enabled`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
…led`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
@david-wu-octopus david-wu-octopus requested a review from a team as a code owner October 11, 2024 05:39
…ntOptions` arg

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Copy link
Member

@pasha-codefresh pasha-codefresh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, amazing work

@pasha-codefresh pasha-codefresh merged commit b5d8edd into argoproj:master Oct 25, 2024
27 checks passed
adriananeci pushed a commit to adriananeci/argo-cd that referenced this pull request Dec 4, 2024
…rgoproj#19528) (argoproj#19637)

* Add `Prompt`, with `prompts.enabled` setting in `argocd-cm`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Make `SettingsOpts`, `SettingsOpts.ArgocdCMPath`, `SettingsOpts.CreateSettingsManager()`  and `commandContext.CreateSettingsManager()` exportable

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add `prompt_test.go`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Refactor `NewPrompt()` and move into new package `utils`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Update `NewPrompt()` to use local config

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add `NewConfigurationCommand()`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Move `prompt_test.go`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Remove `prompt_test.go` for now

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add back and update `prompt_test.go`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add `configuration_test.go`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Fix linting issues

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Fix linting issues

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Reverse early-termination logic in `Confirm()`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Rename `ArgocdCMPath` to `argocdCMPath`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Rename `SettingsOpts` to `settingsOpts`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Rename `CreateSettingsManager()` to `createSettingsManager()`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Rename `configuration` to `configure`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Further rename `configuration` to `configure`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Remove redundant Argo CD ConfigMap logic

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Fix terminal output spacing

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Make `argocd configure` use local config value as the default value for `--prompts-enabled`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add global CLI flag `--force-prompts-enabled`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Update existing `prompt_test.go` test cases

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add test case for `(p *Prompt).Confirm()`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add test cases for `GetBoolFlagWithFallback()` to `env_test.go`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Format imports

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Further format imports and remove unused variable

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Again format imports

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add fallback to `GetPromptsEnabled()`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Fix bug in `GetPromptsEnabled()`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Fix missing import

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add auto-generated docs for `argocd configure`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add auto-generated docs for new global CLI flag `--force-prompts-enabled`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Update `NewPrompt()` to receive a `bool` rather than `*apiclient.ClientOptions` arg

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Remove arg `fallback` from `GetPromptsEnabled()`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add arg `useCLIOpts bool` to `GetPromptsEnabled()`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Make `config.LoadFlags()` exportable

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Add tests for `GetPromptsEnabled()`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Fix linting errors in tests

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

* Run `make codegen-local`

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>

---------

Signed-off-by: David Wu <155603967+david-wu-octopus@users.noreply.github.com>
Signed-off-by: Adrian Aneci <aneci@adobe.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support prompt for certain argocd cmd commands
3 participants