Skip to content
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

Recommended Pattern for Argument Pre-Processing? #2171

Open
austinarbor opened this issue Jul 15, 2024 · 0 comments
Open

Recommended Pattern for Argument Pre-Processing? #2171

austinarbor opened this issue Jul 15, 2024 · 0 comments

Comments

@austinarbor
Copy link

austinarbor commented Jul 15, 2024

Is there a recommended pattern for pre-processing the arguments? I have hundreds of commands in different projects which get passed in args, but some of the args may be encrypted values and need to be decrypted before getting passed to the commands. To be more specific, the values are vault paths and network requests need to be made to fetch the actual values, but the concept is the same.

./my-cli --my-arg=/path/to/secret

Since the commands live in different repositories, I'd like to find a way to extract the common pre-processing (decryption) logic into a shareable format.

Both OnInitialize and PersistentPreRun run after arguments are parsed, so i don't think those can be used.

It seems like the only options are:

  1. Have an init function in every repo with logic to replace os.Args with the decrypted values. This would work but would basically be no common logic that could be shared (except for the decryption logic itself). This also feels easy to mess up, in that someone setting up a new repository could easily forget to add this init logic.

  2. Introduce a helper function in a common module to create the root command?

func NewRootCommand() (*cobra.Command, error) {
 os.Args = .... // replace with decrypted values
}

This would solve the "easy-to-forget" issue, but the NewRootCommand function really has no purpose other than making sure the arguments get decrypted. The other problem with this approach is that if someone were to use SetArgs, it would break the logic.

Is there a better approach that I am missing?

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

No branches or pull requests

1 participant