|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com> |
| 3 | + * |
| 4 | + * This file is part of Symfony CLI project |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package symfony |
| 21 | + |
| 22 | +import ( |
| 23 | + "bytes" |
| 24 | + "encoding/json" |
| 25 | + "fmt" |
| 26 | + "os/exec" |
| 27 | + "strings" |
| 28 | + |
| 29 | + "github.com/pkg/errors" |
| 30 | + "github.com/symfony-cli/symfony-cli/local/php" |
| 31 | +) |
| 32 | + |
| 33 | +type CliApp struct { |
| 34 | + Commands []CliCommand |
| 35 | + Namespaces []CliNamespace |
| 36 | +} |
| 37 | + |
| 38 | +type CliNamespace struct { |
| 39 | + ID string |
| 40 | + Commands []string |
| 41 | +} |
| 42 | + |
| 43 | +type CliCommand struct { |
| 44 | + Name string |
| 45 | + Usage []string |
| 46 | + Description string |
| 47 | + Help string |
| 48 | + Definition CliDefinition |
| 49 | + Hidden bool |
| 50 | + Aliases []string |
| 51 | +} |
| 52 | + |
| 53 | +type CliDefinition struct { |
| 54 | + Arguments map[string]CliArgument |
| 55 | + Options map[string]CliOption |
| 56 | +} |
| 57 | + |
| 58 | +type CliArgument struct { |
| 59 | + Required bool `json:"is_required"` |
| 60 | + IsArray bool `json:"is_array"` |
| 61 | + Description string `json:"description"` |
| 62 | + Default interface{} `json:"default"` |
| 63 | +} |
| 64 | + |
| 65 | +type CliOption struct { |
| 66 | + Shortcut string `json:"shortcut"` |
| 67 | + Description string `json:"description"` |
| 68 | + AcceptValue bool `json:"accept_value"` |
| 69 | + IsValueRequired bool `json:"is_value_required"` |
| 70 | + IsMultiple bool `json:"is_multiple"` |
| 71 | + Default interface{} `json:"default"` |
| 72 | +} |
| 73 | + |
| 74 | +func NewCliApp(projectDir string, args []string) (*CliApp, error) { |
| 75 | + args = append(args, "list", "--format=json") |
| 76 | + var buf bytes.Buffer |
| 77 | + e := &php.Executor{ |
| 78 | + BinName: "php", |
| 79 | + Dir: projectDir, |
| 80 | + Args: args, |
| 81 | + Stdout: &buf, |
| 82 | + Stderr: &buf, |
| 83 | + } |
| 84 | + if ret := e.Execute(false); ret != 0 { |
| 85 | + return nil, errors.Errorf("unable to list commands (%s):\n%s", strings.Join(args, " "), buf.String()) |
| 86 | + } |
| 87 | + return parseCommands(buf.Bytes()) |
| 88 | +} |
| 89 | + |
| 90 | +func NewGoCliApp(projectDir string, binPath string, args []string) (*CliApp, error) { |
| 91 | + var buf bytes.Buffer |
| 92 | + cmd := exec.Command(binPath, "list", "--format=json") |
| 93 | + cmd.Args = append(cmd.Args, args...) |
| 94 | + fmt.Println(cmd.Args) |
| 95 | + cmd.Dir = projectDir |
| 96 | + cmd.Stdout = &buf |
| 97 | + cmd.Stderr = &buf |
| 98 | + if err := cmd.Run(); err != nil { |
| 99 | + return nil, errors.Errorf("unable to list commands (%s):\n%s\n%s", strings.Join(args, " "), err, buf.String()) |
| 100 | + } |
| 101 | + return parseCommands(buf.Bytes()) |
| 102 | +} |
| 103 | + |
| 104 | +func parseCommands(output []byte) (*CliApp, error) { |
| 105 | + // Fix PHP types |
| 106 | + cleanOutput := bytes.ReplaceAll(output, []byte(`"arguments":[]`), []byte(`"arguments":{}`)) |
| 107 | + var app *CliApp |
| 108 | + if err := json.Unmarshal(cleanOutput, &app); err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + return app, nil |
| 112 | +} |
0 commit comments