2020-08-30 03:30:07 +00:00
|
|
|
package configfilearg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-10-08 00:36:57 +00:00
|
|
|
"io"
|
2020-08-30 03:30:07 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2021-04-15 18:29:24 +00:00
|
|
|
"path/filepath"
|
2021-11-08 18:54:48 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2020-08-30 03:30:07 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/agent/util"
|
2024-05-06 16:42:27 +00:00
|
|
|
"github.com/rancher/wrangler/v3/pkg/data/convert"
|
2021-11-12 00:01:23 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli"
|
2020-08-30 03:30:07 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Parser struct {
|
|
|
|
After []string
|
2023-06-07 22:57:52 +00:00
|
|
|
ConfigFlags []string
|
|
|
|
OverrideFlags []string
|
2020-08-30 03:30:07 +00:00
|
|
|
EnvName string
|
|
|
|
DefaultConfig string
|
2023-06-07 22:57:52 +00:00
|
|
|
// ValidFlags are maps of flags that are valid for that particular conmmand. This enables us to ignore flags in
|
|
|
|
// the config file that do no apply to the current command.
|
|
|
|
ValidFlags map[string][]cli.Flag
|
2020-08-30 03:30:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 18:29:24 +00:00
|
|
|
// Parse will parse an os.Args style slice looking for Parser.FlagNames after Parse.After.
|
2020-08-30 03:30:07 +00:00
|
|
|
// It will read the parameter value of Parse.FlagNames and read the file, appending all flags directly after
|
|
|
|
// the Parser.After value. This means a the non-config file flags will override, or if a slice append to, the config
|
|
|
|
// file values.
|
|
|
|
// If Parser.DefaultConfig is set, the existence of the config file is optional if not set in the os.Args. This means
|
|
|
|
// if Parser.DefaultConfig is set we will always try to read the config file but only fail if it's not found if the
|
|
|
|
// args contains Parser.FlagNames
|
|
|
|
func (p *Parser) Parse(args []string) ([]string, error) {
|
|
|
|
prefix, suffix, found := p.findStart(args)
|
|
|
|
if !found {
|
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
if configFile := p.findConfigFileFlag(args); configFile != "" {
|
2020-08-30 03:30:07 +00:00
|
|
|
values, err := readConfigFile(configFile)
|
2024-07-29 20:36:58 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return args, nil
|
|
|
|
}
|
2020-08-30 03:30:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-12 00:01:23 +00:00
|
|
|
if len(args) > 1 {
|
|
|
|
values, err = p.stripInvalidFlags(args[1], values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-08-30 03:30:07 +00:00
|
|
|
return append(prefix, append(values, suffix...)...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 00:01:23 +00:00
|
|
|
func (p *Parser) stripInvalidFlags(command string, args []string) ([]string, error) {
|
|
|
|
var result []string
|
|
|
|
var cmdFlags []cli.Flag
|
|
|
|
for k, v := range p.ValidFlags {
|
|
|
|
if k == command {
|
|
|
|
cmdFlags = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(cmdFlags) == 0 {
|
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
validFlags := make(map[string]bool, len(cmdFlags))
|
|
|
|
for _, f := range cmdFlags {
|
|
|
|
//split flags with aliases into 2 entries
|
|
|
|
for _, s := range strings.Split(f.GetName(), ",") {
|
|
|
|
validFlags[s] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 19:16:13 +00:00
|
|
|
re, err := regexp.Compile("^-+([^=]*)=")
|
2021-11-12 00:01:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return args, err
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
mArg := arg
|
|
|
|
if match := re.FindAllStringSubmatch(arg, -1); match != nil {
|
|
|
|
mArg = match[0][1]
|
|
|
|
}
|
|
|
|
if validFlags[mArg] {
|
|
|
|
result = append(result, arg)
|
|
|
|
} else {
|
2021-11-15 20:17:10 +00:00
|
|
|
logrus.Warnf("Unknown flag %s found in config.yaml, skipping\n", strings.Split(arg, "=")[0])
|
2021-11-12 00:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
// FindString returns the string value of a flag, checking the CLI args,
|
|
|
|
// config file, and config file dropins. If the value is not found,
|
|
|
|
// an empty string is returned. It is not an error if no args,
|
|
|
|
// configfile, or dropins are present.
|
2020-12-16 17:27:57 +00:00
|
|
|
func (p *Parser) FindString(args []string, target string) (string, error) {
|
2023-06-07 22:57:52 +00:00
|
|
|
// Check for --help or --version flags, which override any other flags
|
|
|
|
if val, found := p.findOverrideFlag(args); found {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
var files []string
|
2023-05-02 18:18:23 +00:00
|
|
|
var lastVal string
|
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
if configFile := p.findConfigFileFlag(args); configFile != "" {
|
|
|
|
if _, err := os.Stat(configFile); err == nil {
|
|
|
|
files = append(files, configFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
dropinFiles, err := dotDFiles(configFile)
|
|
|
|
if err != nil {
|
2020-12-16 17:27:57 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2024-07-29 20:36:58 +00:00
|
|
|
files = append(files, dropinFiles...)
|
|
|
|
}
|
2020-12-16 17:27:57 +00:00
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
for _, file := range files {
|
|
|
|
bytes, err := readConfigFileData(file)
|
2023-05-02 18:18:23 +00:00
|
|
|
if err != nil {
|
2020-12-16 17:27:57 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
data := yaml.MapSlice{}
|
|
|
|
if err := yaml.Unmarshal(bytes, &data); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, i := range data {
|
|
|
|
k, v := convert.ToString(i.Key), convert.ToString(i.Value)
|
|
|
|
isAppend := strings.HasSuffix(k, "+")
|
|
|
|
k = strings.TrimSuffix(k, "+")
|
|
|
|
if k == target {
|
|
|
|
if isAppend {
|
|
|
|
lastVal = lastVal + "," + v
|
|
|
|
} else {
|
|
|
|
lastVal = v
|
2023-05-02 18:18:23 +00:00
|
|
|
}
|
2020-12-16 17:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-02 18:18:23 +00:00
|
|
|
return lastVal, nil
|
2020-12-16 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 22:57:52 +00:00
|
|
|
func (p *Parser) findOverrideFlag(args []string) (string, bool) {
|
|
|
|
for _, arg := range args {
|
|
|
|
for _, flagName := range p.OverrideFlags {
|
|
|
|
if flagName == arg {
|
|
|
|
return arg, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
// findConfigFileFlag returns the value of the config file env var or CLI flag.
|
|
|
|
// If neither are set, it returns the default value.
|
|
|
|
func (p *Parser) findConfigFileFlag(args []string) string {
|
2020-08-30 03:30:07 +00:00
|
|
|
if envVal := os.Getenv(p.EnvName); p.EnvName != "" && envVal != "" {
|
2024-07-29 20:36:58 +00:00
|
|
|
return envVal
|
2020-08-30 03:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, arg := range args {
|
2023-06-07 22:57:52 +00:00
|
|
|
for _, flagName := range p.ConfigFlags {
|
2020-08-30 03:30:07 +00:00
|
|
|
if flagName == arg {
|
|
|
|
if len(args) > i+1 {
|
2024-07-29 20:36:58 +00:00
|
|
|
return args[i+1]
|
2020-08-30 03:30:07 +00:00
|
|
|
}
|
|
|
|
// This is actually invalid, so we rely on the CLI parser after the fact flagging it as bad
|
2024-07-29 20:36:58 +00:00
|
|
|
return ""
|
2020-08-30 03:30:07 +00:00
|
|
|
} else if strings.HasPrefix(arg, flagName+"=") {
|
2024-07-29 20:36:58 +00:00
|
|
|
return arg[len(flagName)+1:]
|
2020-08-30 03:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
return p.DefaultConfig
|
2020-08-30 03:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) findStart(args []string) ([]string, []string, bool) {
|
|
|
|
if len(p.After) == 0 {
|
|
|
|
return []string{}, args, true
|
|
|
|
}
|
2021-11-12 00:01:23 +00:00
|
|
|
afterTemp := append([]string{}, p.After...)
|
2021-11-08 18:54:48 +00:00
|
|
|
afterIndex := make(map[string]int)
|
|
|
|
re, err := regexp.Compile(`(.+):(\d+)`)
|
|
|
|
if err != nil {
|
|
|
|
return args, nil, false
|
|
|
|
}
|
|
|
|
// After keywords ending with ":<NUM>" can set + NUM of arguments as the split point.
|
|
|
|
// used for matching on subcommmands
|
2021-11-12 00:01:23 +00:00
|
|
|
for i, arg := range afterTemp {
|
2021-11-08 18:54:48 +00:00
|
|
|
if match := re.FindAllStringSubmatch(arg, -1); match != nil {
|
2021-11-12 00:01:23 +00:00
|
|
|
afterTemp[i] = match[0][1]
|
2021-11-08 18:54:48 +00:00
|
|
|
afterIndex[match[0][1]], err = strconv.Atoi(match[0][2])
|
|
|
|
if err != nil {
|
|
|
|
return args, nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-30 03:30:07 +00:00
|
|
|
for i, val := range args {
|
2021-11-12 00:01:23 +00:00
|
|
|
for _, test := range afterTemp {
|
2020-08-30 03:30:07 +00:00
|
|
|
if val == test {
|
2021-11-08 18:54:48 +00:00
|
|
|
if skip := afterIndex[test]; skip != 0 {
|
|
|
|
if len(args) <= i+skip || strings.HasPrefix(args[i+skip], "-") {
|
|
|
|
return args[0 : i+1], args[i+1:], true
|
|
|
|
}
|
|
|
|
return args[0 : i+skip+1], args[i+skip+1:], true
|
|
|
|
}
|
2020-08-30 03:30:07 +00:00
|
|
|
return args[0 : i+1], args[i+1:], true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args, nil, false
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:29:24 +00:00
|
|
|
func dotDFiles(basefile string) (result []string, _ error) {
|
2022-10-08 00:36:57 +00:00
|
|
|
files, err := os.ReadDir(basefile + ".d")
|
2021-04-15 18:29:24 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() || !util.HasSuffixI(file.Name(), ".yaml", ".yml") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, filepath.Join(basefile+".d", file.Name()))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
// readConfigFile returns a flattened arg list generated from the specified config
|
|
|
|
// file, and any config file dropins in the dropin directory that corresponds to that
|
|
|
|
// config file. The config file or at least one dropin must exist.
|
2020-08-30 03:30:07 +00:00
|
|
|
func readConfigFile(file string) (result []string, _ error) {
|
2021-04-15 18:29:24 +00:00
|
|
|
files, err := dotDFiles(file)
|
2020-08-30 03:30:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
if _, err = os.Stat(file); err != nil {
|
|
|
|
// If the config file doesn't exist and we have dropins that's fine.
|
|
|
|
// Other errors are bubbled up regardless of how many dropins we have.
|
|
|
|
if !(os.IsNotExist(err) && len(files) > 0) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-15 18:29:24 +00:00
|
|
|
} else {
|
2024-07-29 20:36:58 +00:00
|
|
|
// The config file exists, load it first.
|
2021-04-15 18:29:24 +00:00
|
|
|
files = append([]string{file}, files...)
|
2020-08-30 03:30:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 05:52:02 +00:00
|
|
|
var (
|
|
|
|
keySeen = map[string]bool{}
|
|
|
|
keyOrder []string
|
|
|
|
values = map[string]interface{}{}
|
|
|
|
)
|
|
|
|
for _, file := range files {
|
2021-04-15 18:29:24 +00:00
|
|
|
bytes, err := readConfigFileData(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := yaml.MapSlice{}
|
|
|
|
if err := yaml.Unmarshal(bytes, &data); err != nil {
|
|
|
|
return nil, err
|
2020-08-30 03:30:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 18:29:24 +00:00
|
|
|
for _, i := range data {
|
|
|
|
k, v := convert.ToString(i.Key), i.Value
|
2021-04-25 05:52:02 +00:00
|
|
|
isAppend := strings.HasSuffix(k, "+")
|
|
|
|
k = strings.TrimSuffix(k, "+")
|
2021-04-15 18:29:24 +00:00
|
|
|
|
2021-04-25 05:52:02 +00:00
|
|
|
if !keySeen[k] {
|
|
|
|
keySeen[k] = true
|
|
|
|
keyOrder = append(keyOrder, k)
|
2021-04-15 18:29:24 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 05:52:02 +00:00
|
|
|
if oldValue, ok := values[k]; ok && isAppend {
|
|
|
|
values[k] = append(toSlice(oldValue), toSlice(v)...)
|
2021-04-15 18:29:24 +00:00
|
|
|
} else {
|
2021-04-25 05:52:02 +00:00
|
|
|
values[k] = v
|
2020-08-30 03:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 05:52:02 +00:00
|
|
|
for _, k := range keyOrder {
|
|
|
|
v := values[k]
|
|
|
|
|
|
|
|
prefix := "--"
|
|
|
|
if len(k) == 1 {
|
|
|
|
prefix = "-"
|
|
|
|
}
|
|
|
|
|
|
|
|
if slice, ok := v.([]interface{}); ok {
|
|
|
|
for _, v := range slice {
|
|
|
|
result = append(result, prefix+k+"="+convert.ToString(v))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
str := convert.ToString(v)
|
|
|
|
result = append(result, prefix+k+"="+str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-30 03:30:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-25 05:52:02 +00:00
|
|
|
func toSlice(v interface{}) []interface{} {
|
|
|
|
switch k := v.(type) {
|
|
|
|
case string:
|
|
|
|
return []interface{}{k}
|
|
|
|
case []interface{}:
|
|
|
|
return k
|
|
|
|
default:
|
|
|
|
str := strings.TrimSpace(convert.ToString(v))
|
|
|
|
if str == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return []interface{}{str}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-29 20:36:58 +00:00
|
|
|
// readConfigFileData returns the contents of a local or remote file
|
2020-08-30 03:30:07 +00:00
|
|
|
func readConfigFileData(file string) ([]byte, error) {
|
|
|
|
u, err := url.Parse(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse config location %s: %w", file, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch u.Scheme {
|
|
|
|
case "http", "https":
|
|
|
|
resp, err := http.Get(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read http config %s: %w", file, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2022-10-08 00:36:57 +00:00
|
|
|
return io.ReadAll(resp.Body)
|
2020-08-30 03:30:07 +00:00
|
|
|
default:
|
2022-10-08 00:36:57 +00:00
|
|
|
return os.ReadFile(file)
|
2020-08-30 03:30:07 +00:00
|
|
|
}
|
|
|
|
}
|