main.go 2.7 KB

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