k3s/pkg/configfilearg/defaultparser.go
Derek Nola dc6c569b98
Shortcircuit commands with version or help flags (#7683)
* Shortcircuit search with help and version flag

Signed-off-by: Derek Nola <derek.nola@suse.com>

* Keep functions seperate

Signed-off-by: Derek Nola <derek.nola@suse.com>

---------

Signed-off-by: Derek Nola <derek.nola@suse.com>
2023-06-07 15:57:52 -07:00

38 lines
1.0 KiB
Go

package configfilearg
import (
"github.com/k3s-io/k3s/pkg/cli/cmds"
"github.com/k3s-io/k3s/pkg/version"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var DefaultParser = &Parser{
After: []string{"server", "agent", "etcd-snapshot:1"},
ConfigFlags: []string{"--config", "-c"},
EnvName: version.ProgramUpper + "_CONFIG_FILE",
DefaultConfig: "/etc/rancher/" + version.Program + "/config.yaml",
ValidFlags: map[string][]cli.Flag{"server": cmds.ServerFlags, "etcd-snapshot": cmds.EtcdSnapshotFlags},
}
func MustParse(args []string) []string {
result, err := DefaultParser.Parse(args)
if err != nil {
logrus.Fatal(err)
}
return result
}
func MustFindString(args []string, target string) string {
parser := &Parser{
OverrideFlags: []string{"--help", "-h", "--version", "-v"},
EnvName: version.ProgramUpper + "_CONFIG_FILE",
DefaultConfig: "/etc/rancher/" + version.Program + "/config.yaml",
}
result, err := parser.FindString(args, target)
if err != nil {
logrus.Fatal(err)
}
return result
}