kong icon indicating copy to clipboard operation
kong copied to clipboard

Env Var Default value for flag sourced from a Plugin

Open ffluk3 opened this issue 2 years ago • 3 comments

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?

ffluk3 avatar Oct 13 '23 16:10 ffluk3

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

Thanks

testinfected avatar Apr 04 '24 21:04 testinfected

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.

ffluk3 avatar Apr 05 '24 01:04 ffluk3

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.

testinfected avatar Apr 06 '24 18:04 testinfected