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

Env Var Default value for flag sourced from a Plugin #385

Open
ffluk3 opened this issue Oct 13, 2023 · 3 comments
Open

Env Var Default value for flag sourced from a Plugin #385

ffluk3 opened this issue Oct 13, 2023 · 3 comments

Comments

@ffluk3
Copy link

ffluk3 commented Oct 13, 2023

We are trying to construct a plugin that could apply a --plain flag to several CLI tools. The ideal state is that either of the following are vaild:

my-cli --plain

# ...or...

PLAIN=true my-cli

The rough structure of our plugin is the following:

package kongplugin

import (
	"github.com/alecthomas/kong"
	"github.com/pterm/pterm"
)

type PlainFlag struct {
	Plain plainFlag `env:"PLAIN" help:"disable styling of the output"`
}

type plainFlag bool

func (v plainFlag) IsBool() bool { return true }
func (v plainFlag) BeforeApply(ktx *kong.Context) error {
	// Disable styling for any of our downstream tools

	return nil
}

Our main problem with this approach is that the environment variable PLAIN does not get respected by this approach, primarily because it appears Kong will not run any of plainFlag's hooks unless the flag is explicitly declared. We have tried the following:

  • Leverage all lifecycle hooks supported by Kong
  • Customer Resolver configurations to respect the default

Are there any recommendations of how we could get this functionality?

@testinfected
Copy link

I have the same question, how did you end up doing things @ffluk3?

Thanks

@ffluk3
Copy link
Author

ffluk3 commented Apr 5, 2024

Unfortunately, we ended up needing to duplicate the code in several CLIs we manage, sharing only the definition of the flag. Still hoping to hear guidance from the Kong team on a better approach.

@testinfected
Copy link

Thanks for getting back to me @ffluk3

I ended up using an if statement after parsing and before running the command to replace the hook.

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

2 participants