main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 main
  14. import (
  15. "fmt"
  16. "os/exec"
  17. "strings"
  18. "github.com/docker/compose-cli/utils"
  19. )
  20. var managementCommands = []string{"ecs", "assemble", "registry", "template", "cluster", "scan"}
  21. var commands = []string{}
  22. func main() {
  23. getCommands()
  24. getCommands("compose")
  25. fmt.Printf(`
  26. var managementCommands = []string{
  27. "%s",
  28. }
  29. var commands = []string{
  30. "%s",
  31. }
  32. `, strings.Join(managementCommands, "\", \n\t\""), strings.Join(commands, "\", \n\t\""))
  33. }
  34. const (
  35. mgtCommandsSection = "Management Commands:"
  36. commandsSection = "Commands:"
  37. aliasesSection = "Aliases:"
  38. )
  39. func getCommands(execCommands ...string) {
  40. withHelp := append(execCommands, "--help")
  41. cmd := exec.Command("docker", withHelp...)
  42. output, err := cmd.Output()
  43. if err != nil {
  44. return
  45. }
  46. text := string(output)
  47. lines := strings.Split(text, "\n")
  48. section := ""
  49. for _, line := range lines {
  50. trimmedLine := strings.TrimSpace(line)
  51. if strings.HasPrefix(trimmedLine, mgtCommandsSection) {
  52. section = mgtCommandsSection
  53. continue
  54. }
  55. if strings.HasPrefix(trimmedLine, commandsSection) || strings.HasPrefix(trimmedLine, "Available Commands:") {
  56. section = commandsSection
  57. if len(execCommands) > 0 {
  58. command := execCommands[len(execCommands)-1]
  59. managementCommands = append(managementCommands, command)
  60. }
  61. continue
  62. }
  63. if strings.HasPrefix(trimmedLine, aliasesSection) {
  64. section = aliasesSection
  65. continue
  66. }
  67. if trimmedLine == "" {
  68. section = ""
  69. continue
  70. }
  71. tokens := strings.Split(trimmedLine, " ")
  72. command := strings.Replace(tokens[0], "*", "", 1)
  73. switch section {
  74. case mgtCommandsSection:
  75. getCommands(append(execCommands, command)...)
  76. case commandsSection:
  77. if !utils.StringContains(commands, command) {
  78. commands = append(commands, command)
  79. }
  80. getCommands(append(execCommands, command)...)
  81. case aliasesSection:
  82. aliases := strings.Split(trimmedLine, ",")
  83. for _, alias := range aliases {
  84. trimmedAlias := strings.TrimSpace(alias)
  85. if !utils.StringContains(commands, trimmedAlias) {
  86. commands = append(commands, trimmedAlias)
  87. }
  88. }
  89. }
  90. }
  91. }