mirror of
https://git.vectorsigma.ru/public/k3s.git
synced 2026-08-04 20:29:46 +00:00
External CLI actions cannot short-circuit on --help or --version, so we
cannot skip loading the config file if these flags are present when
running these wrapped commands. The behavior of just returning the
override flag name instead of the requested flag value was breaking
data-dir lookup when running wrapped commands.
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
(cherry picked from commit ff5c633fe7)
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package configfilearg
|
|
|
|
import (
|
|
"slices"
|
|
|
|
"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, commandsWithoutOverride ...string) string {
|
|
overrideFlags := []string{"--help", "-h", "--version", "-v"}
|
|
// Check to see if the command or subcommand being executed supports override flags.
|
|
// Some subcommands such as `k3s ctr` or just `ctr` need to be extracted out even to
|
|
// provide version or help text, and we cannot short-circuit loading the config file. For
|
|
// these commands, treat failure to load the config file as a warning instead of a fatal.
|
|
if len(args) > 0 && args[0] == version.Program {
|
|
args = args[1:]
|
|
}
|
|
if len(args) > 0 && slices.Contains(commandsWithoutOverride, args[0]) {
|
|
overrideFlags = nil
|
|
}
|
|
|
|
parser := &Parser{
|
|
OverrideFlags: overrideFlags,
|
|
EnvName: version.ProgramUpper + "_CONFIG_FILE",
|
|
DefaultConfig: "/etc/rancher/" + version.Program + "/config.yaml",
|
|
}
|
|
result, err := parser.FindString(args, target)
|
|
if err != nil {
|
|
if len(overrideFlags) > 0 {
|
|
logrus.Fatal(err)
|
|
} else {
|
|
logrus.Warn(err)
|
|
}
|
|
}
|
|
return result
|
|
}
|