main.go 2.7 KB

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