Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a7e2329
[#1587] Exclude a common part of CLI parser
fivitti Jan 2, 2025
ac04f65
[#1587] Working general CLI parser
fivitti Jan 3, 2025
ffd2cf4
[#1587] Fix the wrong argument
fivitti Jan 7, 2025
8808118
[#1587] Unify agent and server parsers
fivitti Jan 7, 2025
e301a43
[#1587] Verify the envvars
fivitti Jan 7, 2025
30c4d21
[#1587] Fix linter issues
fivitti Jan 7, 2025
c8ca1c5
[#1587] Verify system environment variables
fivitti Jan 7, 2025
81b857a
[#1587] Add unit tests
fivitti Jan 7, 2025
48add73
[#1587] Extend unit test
fivitti Jan 7, 2025
06ba0b6
[#1587] Fix unit tests
fivitti Jan 7, 2025
cfb6ec4
[#1587] Simplify utility
fivitti Jan 7, 2025
7a29eb8
[#1587] Fix linter issue
fivitti Jan 8, 2025
522be1b
[#1587] Remove redundant flags
fivitti Jan 8, 2025
c6657d4
[#1587] Add unit tests
fivitti Jan 8, 2025
bf98f00
[#1587] Add a Changelog entry
fivitti Jan 8, 2025
19b7c94
[#1587] Unify the CLI handling in the Stork tool
fivitti Jan 8, 2025
66db02f
[#1587] Move package
fivitti Jan 8, 2025
425910d
[#1587] Exclude app to a separate file
fivitti Jan 8, 2025
7b201bd
[#1587] Unexport structs
fivitti Jan 8, 2025
6569020
[#1587] Unify code-gen CLI
fivitti Jan 8, 2025
27c6b7c
[#1587] Remove unnecessary dependencies
fivitti Jan 8, 2025
185123d
[#1587] Rename structs
fivitti Jan 9, 2025
f1d16f2
[#1587] Add unit tests
fivitti Jan 9, 2025
60f2fe4
[#1587] Rephrase a sentence
fivitti Jan 9, 2025
1acfd8d
[#1587] Support hooks only for agent and server
fivitti Jan 9, 2025
9285a3c
[#1587] Add unit test
fivitti Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[#1587] Unify the CLI handling in the Stork tool
  • Loading branch information
fivitti authored and tomaszmrugalski committed Jun 2, 2025
commit 19b7c94be6f1e6237bf68fa60d73449a4dcbdff8
78 changes: 53 additions & 25 deletions backend/appcfg/stork/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ type CLIParser struct {
// Accepts the main parser. It should be configured with the application
// settings.
// The application name is used to construct the namespaces for the CLI flags
// and environment variables. It should be either 'server' or 'agent'.
// and environment variables. It should be either 'server', 'agent', 'tool'.
// The callback is called when the environment file is loaded. Its purpose is
// to allow reconfiguring the logging using the new environment variables as
// soon as they are available.
func NewCLIParser(parser *flags.Parser, app string, onLoadEnvironmentFileCallback func()) *CLIParser {
if app != "server" && app != "agent" {
if app != "server" && app != "agent" && app != "tool" {
// Programming error.
panic("invalid application name")
}
Expand All @@ -61,16 +61,18 @@ func NewCLIParser(parser *flags.Parser, app string, onLoadEnvironmentFileCallbac
// At the end, it composes the CLI parser from all the flags and runs it.
// Returns a hook directory settings, hook settings extracted from the hooks,
// flag indication if the help was requested and an error if any.
func (p *CLIParser) Parse() (*HookDirectorySettings, GroupedHookCLIFlags, bool, error) {
hookDirectorySettings, allHookFLags, err := p.bootstrap()
// Accepts the command line arguments. The passed arguments should exclude
// the application name (the first argument).
func (p *CLIParser) Parse(args []string) (*HookDirectorySettings, GroupedHookCLIFlags, bool, error) {
hookDirectorySettings, allHookFLags, err := p.bootstrap(args)
if err != nil {
if isHelpRequest(err) {
return nil, nil, true, nil
}
return nil, nil, false, err
}

err = p.parse()
err = p.parse(args)
if err != nil {
if isHelpRequest(err) {
return nil, nil, true, nil
Expand All @@ -81,9 +83,9 @@ func (p *CLIParser) Parse() (*HookDirectorySettings, GroupedHookCLIFlags, bool,
}

// Parse the CLI flags stored in the main parser.
func (p *CLIParser) parse() (err error) {
func (p *CLIParser) parse(args []string) (err error) {
// Do args parsing.
if _, err = p.parser.Parse(); err != nil {
if _, err = p.parser.ParseArgs(args); err != nil {
err = errors.Wrap(err, "cannot parse the CLI flags")
return err
}
Expand All @@ -95,11 +97,11 @@ func (p *CLIParser) parse() (err error) {
// is provided, the content is loaded.
// Next, it parses the hooks location and extracts their CLI flags.
// The hook flags are then merged with the core flags.
func (p *CLIParser) bootstrap() (*HookDirectorySettings, GroupedHookCLIFlags, error) {
func (p *CLIParser) bootstrap(args []string) (*HookDirectorySettings, GroupedHookCLIFlags, error) {
// Environment variables.
envFileSettings := &environmentFileSettings{}
envParser := p.createSubParser(envFileSettings)
if _, err := envParser.Parse(); err != nil {
if _, err := envParser.ParseArgs(args); err != nil {
return nil, nil, err
}
err := p.loadEnvironmentFile(envFileSettings)
Expand All @@ -110,7 +112,7 @@ func (p *CLIParser) bootstrap() (*HookDirectorySettings, GroupedHookCLIFlags, er
// Process the hook directory location.
hookDirectorySettings := &HookDirectorySettings{}
hookParser := p.createSubParser(hookDirectorySettings)
if _, err := hookParser.Parse(); err != nil {
if _, err := hookParser.ParseArgs(args); err != nil {
return nil, nil, err
}

Expand Down Expand Up @@ -217,6 +219,7 @@ func (p *CLIParser) createSubParser(settings any) *flags.Parser {

p.substitutePlaceholders(parser)

parser.Name = p.parser.Name
parser.ShortDescription = p.parser.ShortDescription
parser.LongDescription = p.parser.LongDescription
return parser
Expand Down Expand Up @@ -287,13 +290,7 @@ func (p *CLIParser) verifyEnvironmentFile(envFileSettings *environmentFileSettin
return err
}

// Collect all known environment variables.
knownEnvironmentVariables := make(map[string]bool)
for _, group := range p.parser.Groups() {
for _, option := range group.Options() {
knownEnvironmentVariables[option.EnvKeyWithNamespace()] = true
}
}
knownEnvironmentVariables := collectKnownEnvironmentVariables(p.parser.Command)

// Check if all environment variables are known.
for key := range entries {
Expand All @@ -312,12 +309,7 @@ func (p *CLIParser) verifyEnvironmentFile(envFileSettings *environmentFileSettin
// unknown Stork-specific environment variables.
func (p *CLIParser) verifySystemEnvironmentVariables() {
// Collect all known environment variables.
knownEnvironmentVariables := make(map[string]bool)
for _, group := range p.parser.Groups() {
for _, option := range group.Options() {
knownEnvironmentVariables[option.EnvKeyWithNamespace()] = true
}
}
knownEnvironmentVariables := collectKnownEnvironmentVariables(p.parser.Command)

// Contains the prefixes of the Stork-specific environment variables.
// Stork environment variables starts with the 'STORK' part and then
Expand Down Expand Up @@ -394,9 +386,17 @@ func (p *CLIParser) collectHookCLIFlags(hookDirectorySettings *HookDirectorySett
case err == nil && stat.IsDir():
// Gather the hook flags.
hookWalker := hooksutil.NewHookWalker()
program := hooks.HookProgramServer
if p.application == "agent" {
var program string
switch p.application {
case "server":
program = hooks.HookProgramServer
case "agent":
program = hooks.HookProgramAgent
case "tool":
program = hooks.HookProgramTool
default:
// Programming error.
return nil, errors.Errorf("unknown application name: %s", p.application)
}

allCLIFlags, err = hookWalker.CollectCLIFlags(
Expand Down Expand Up @@ -464,3 +464,31 @@ func getHookNamespaces(application string, hookName string) (flagNamespace, envN
envNamespace = strings.ToUpper(envNamespace)
return
}

// Collects all known environment variables from a parser. Returns a set of
// the full environment variable names.
func collectKnownEnvironmentVariables(parser *flags.Command) map[string]bool {
knownEnvironmentVariables := make(map[string]bool)

// The options of the main group of the top-level parser.
for _, option := range parser.Group.Options() {
knownEnvironmentVariables[option.EnvKeyWithNamespace()] = true
}

// The groups of the top-level parser.
for _, group := range parser.Groups() {
for _, option := range group.Options() {
knownEnvironmentVariables[option.EnvKeyWithNamespace()] = true
}
}

// The subcommands of the top-level parser.
for _, subcommand := range parser.Commands() {
subcommandEnvironmentVariables := collectKnownEnvironmentVariables(subcommand)
for key := range subcommandEnvironmentVariables {
knownEnvironmentVariables[key] = true
}
}

return knownEnvironmentVariables
}
Loading