Env Var Default value for flag sourced from a Plugin
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
Resolverconfigurations to respect the default
Are there any recommendations of how we could get this functionality?
I have the same question, how did you end up doing things @ffluk3?
Thanks
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.
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.