8000 Merge pull request #574 from symfony-cli/extract-symfony-cli · tucksaun/symfonycli@fa3f5a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa3f5a5

Browse files
authored
Merge pull request symfony-cli#574 from symfony-cli/extract-symfony-cli
Extract logic for parsing Symfony CLI apps
2 parents 24ef352 + 333d845 commit fa3f5a5

File tree

2s changed

+121
-50
lines changed

2 files changed

+121
-50
lines changed

local/platformsh/generator/commands.go

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,11 @@ import (
3030
"text/template"
3131

3232
"github.com/mitchellh/go-homedir"
33-
"github.com/pkg/errors"
3433
"github.com/symfony-cli/console"
3534
"github.com/symfony-cli/symfony-cli/local/platformsh"
35+
"github.com/symfony-cli/symfony-cli/symfony"
3636
)
3737

38-
type application struct {
39-
Namespaces []namespace
40-
Commands []command
41-
}
42-
43-
type namespace struct {
44-
ID string
45-
Commands []string
46-
}
47-
48-
type command struct {
49-
Name string
50-
Usage []string
51-
Description string
52-
Help string
53-
Definition definition
54-
Hidden bool
55-
Aliases []string
56-
}
57-
58-
type definition struct {
59-
Arguments map[string]argument
60-
Options map[string]option
61-
}
62-
63-
type argument struct {
64-
}
65-
66-
type option struct {
67-
Shortcut string
68-
Default interface{}
69-
}
70-
7138
var commandsTemplate = template.Must(template.New("output").Parse(`// Code generated by platformsh/generator/main.go
7239
// DO NOT EDIT
7340
@@ -131,20 +98,12 @@ func generateCommands() {
13198
}
13299

133100
func parseCommands(cloudPath string) (string, error) {
134-
var buf bytes.Buffer
135-
var bufErr bytes.Buffer
136-
cmd := exec.Command(cloudPath, "list", "--format=json", "--all")
137-
cmd.Stdout = &buf
138-
cmd.Stderr = &bufErr
139-
if err := cmd.Run(); err != nil {
140-
return "", errors.Errorf("unable to list commands: %s\n%s\n%s", err, bufErr.String(), buf.String())
101+
wd, err := os.Getwd()
102+
if err != nil {
103+
return "", err
141104
}
142-
143-
// Fix PHP types
144-
cleanOutput := bytes.ReplaceAll(buf.Bytes(), []byte(`"arguments":[]`), []byte(`"arguments":{}`))
145-
146-
var definition application
147-
if err := json.Unmarshal(cleanOutput, &definition); err != nil {
105+
cliApp, err := symfony.NewGoCliApp(wd, cloudPath, []string{"--all"})
106+
if err != nil {
148107
return "", err
149108
}
150109

@@ -166,7 +125,7 @@ func parseCommands(cloudPath string) (string, error) {
166125
excludedOptions = append(excludedOptions, console.VersionFlag.Names()...)
167126

168127
definitionAsString := ""
169-
for _, command := range definition.Commands {
128+
for _, command := range cliApp.Commands {
170129
if strings.Contains(command.Description, "deprecated") || strings.Contains(command.Description, "DEPRECATED") {
171130
continue
172131
}
@@ -181,7 +140,7 @@ func parseCommands(cloudPath string) (string, error) {
181140
}
182141
namespace := "cloud"
183142
loop:
184-
for _, n := range definition.Namespaces {
143+
for _, n := range cliApp.Namespaces {
185144
for _, name := range n.Commands {
186145
if name == command.Name {
187146
if n.ID != "_global" {
@@ -301,7 +260,7 @@ func getCommandAliases(name, cloudPath string) ([]string, error) {
301260
return []string{}, nil
302261
//return nil, errors.Errorf("unable to get definition for command %s: %s\n%s\n%s", name, err, bufErr.String(), buf.String())
303262
}
304-
var cmd command
263+
var cmd symfony.CliCommand
305264
if err := json.Unmarshal(buf.Bytes(), &cmd); err != nil {
306265
return nil, err
307266
}

symfony/cli.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)
0