convert.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2020 Docker Compose CLI 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 compatibility
  14. import (
  15. "fmt"
  16. "os"
  17. "strings"
  18. "github.com/docker/compose/v2/cmd/compose"
  19. )
  20. func getCompletionCommands() []string {
  21. return []string{
  22. "__complete",
  23. "__completeNoDesc",
  24. }
  25. }
  26. func getBoolFlags() []string {
  27. return []string{
  28. "--debug", "-D",
  29. "--verbose",
  30. "--tls",
  31. "--tlsverify",
  32. }
  33. }
  34. func getStringFlags() []string {
  35. return []string{
  36. "--tlscacert",
  37. "--tlscert",
  38. "--tlskey",
  39. "--host", "-H",
  40. "--context",
  41. "--log-level",
  42. }
  43. }
  44. // Convert transforms standalone docker-compose args into CLI plugin compliant ones
  45. func Convert(args []string) []string {
  46. var rootFlags []string
  47. command := []string{compose.PluginName}
  48. l := len(args)
  49. ARGS:
  50. for i := 0; i < l; i++ {
  51. arg := args[i]
  52. if contains(getCompletionCommands(), arg) {
  53. command = append([]string{arg}, command...)
  54. continue
  55. }
  56. if arg != "" && arg[0] != '-' {
  57. command = append(command, args[i:]...)
  58. break
  59. }
  60. switch arg {
  61. case "--verbose":
  62. arg = "--debug"
  63. case "-h":
  64. // docker cli has deprecated -h to avoid ambiguity with -H, while docker-compose still support it
  65. arg = "--help"
  66. case "--version", "-v":
  67. // redirect --version pseudo-command to actual command
  68. arg = "version"
  69. }
  70. if contains(getBoolFlags(), arg) {
  71. rootFlags = append(rootFlags, arg)
  72. continue
  73. }
  74. for _, flag := range getStringFlags() {
  75. if arg == flag {
  76. i++
  77. if i >= l {
  78. fmt.Fprintf(os.Stderr, "flag needs an argument: '%s'\n", arg)
  79. os.Exit(1)
  80. }
  81. rootFlags = append(rootFlags, arg, args[i])
  82. continue ARGS
  83. }
  84. if strings.HasPrefix(arg, flag) {
  85. _, val, found := strings.Cut(arg, "=")
  86. if found {
  87. rootFlags = append(rootFlags, flag, val)
  88. continue ARGS
  89. }
  90. }
  91. }
  92. command = append(command, arg)
  93. }
  94. return append(rootFlags, command...)
  95. }
  96. func contains(array []string, needle string) bool {
  97. for _, val := range array {
  98. if val == needle {
  99. return true
  100. }
  101. }
  102. return false
  103. }