arguments.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "fmt"
  16. "sort"
  17. "strings"
  18. "github.com/pkg/errors"
  19. )
  20. // BuildArgumentListFromMap takes two string-string maps, one with the base arguments and one
  21. // with optional override arguments. In the return list override arguments will precede base
  22. // arguments
  23. func BuildArgumentListFromMap(baseArguments map[string]string, overrideArguments map[string]string) []string {
  24. var command []string
  25. var keys []string
  26. argsMap := make(map[string]string)
  27. for k, v := range baseArguments {
  28. argsMap[k] = v
  29. }
  30. for k, v := range overrideArguments {
  31. argsMap[k] = v
  32. }
  33. for k := range argsMap {
  34. keys = append(keys, k)
  35. }
  36. sort.Strings(keys)
  37. for _, k := range keys {
  38. command = append(command, fmt.Sprintf("--%s=%s", k, argsMap[k]))
  39. }
  40. return command
  41. }
  42. // ParseArgumentListToMap parses a CLI argument list in the form "--foo=bar" to a string-string map
  43. func ParseArgumentListToMap(arguments []string) map[string]string {
  44. resultingMap := map[string]string{}
  45. for i, arg := range arguments {
  46. key, val, err := parseArgument(arg)
  47. // Ignore if the first argument doesn't satisfy the criteria, it's most often the binary name
  48. // Warn in all other cases, but don't error out. This can happen only if the user has edited the argument list by hand, so they might know what they are doing
  49. if err != nil {
  50. if i != 0 {
  51. fmt.Printf("[kubeadm] WARNING: The component argument %q could not be parsed correctly. The argument must be of the form %q. Skipping...\n", arg, "--")
  52. }
  53. continue
  54. }
  55. resultingMap[key] = val
  56. }
  57. return resultingMap
  58. }
  59. // ReplaceArgument gets a command list; converts it to a map for easier modification, runs the provided function that
  60. // returns a new modified map, and then converts the map back to a command string slice
  61. func ReplaceArgument(command []string, argMutateFunc func(map[string]string) map[string]string) []string {
  62. argMap := ParseArgumentListToMap(command)
  63. // Save the first command (the executable) if we're sure it's not an argument (i.e. no --)
  64. var newCommand []string
  65. if len(command) > 0 && !strings.HasPrefix(command[0], "--") {
  66. newCommand = append(newCommand, command[0])
  67. }
  68. newArgMap := argMutateFunc(argMap)
  69. newCommand = append(newCommand, BuildArgumentListFromMap(newArgMap, map[string]string{})...)
  70. return newCommand
  71. }
  72. // parseArgument parses the argument "--foo=bar" to "foo" and "bar"
  73. func parseArgument(arg string) (string, string, error) {
  74. if !strings.HasPrefix(arg, "--") {
  75. return "", "", errors.New("the argument should start with '--'")
  76. }
  77. if !strings.Contains(arg, "=") {
  78. return "", "", errors.New("the argument should have a '=' between the flag and the value")
  79. }
  80. // Remove the starting --
  81. arg = strings.TrimPrefix(arg, "--")
  82. // Split the string on =. Return only two substrings, since we want only key/value, but the value can include '=' as well
  83. keyvalSlice := strings.SplitN(arg, "=", 2)
  84. // Make sure both a key and value is present
  85. if len(keyvalSlice) != 2 {
  86. return "", "", errors.New("the argument must have both a key and a value")
  87. }
  88. if len(keyvalSlice[0]) == 0 {
  89. return "", "", errors.New("the argument must have a key")
  90. }
  91. return keyvalSlice[0], keyvalSlice[1], nil
  92. }