Skip to content

Commit 333d845

Browse files
committed
Extract logic for parsing Symfony CLI apps
1 parent ca46fc9 commit 333d845

File tree

2 files 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
@@ -11,44 +11,11 @@ import (
1111
"text/template"
1212

1313
"github.com/mitchellh/go-homedir"
14-
"github.com/pkg/errors"
1514
"github.com/symfony-cli/console"
1615
"github.com/symfony-cli/symfony-cli/local/platformsh"
16+
"github.com/symfony-cli/symfony-cli/symfony"
1717
)
1818

19-
type application struct {
20-
Namespaces []namespace
21-
Commands []command
22-
}
23-
24-
type namespace struct {
25-
ID string
26-
Commands []string
27-
}
28-
29-
type command struct {
30-
Name string
31-
Usage []string
32-
Description string
33-
Help string
34-
Definition definition
35-
Hidden bool
36-
Aliases []string
37-
}
38-
39-
type definition struct {
40-
Arguments map[string]argument
41-
Options map[string]option
42-
}
43-
44-
type argument struct {
45-
}
46-
47-
type option struct {
48-
Shortcut string
49-
Default interface{}
50-
}
51-
5219
var commandsTemplate = template.Must(template.New("output").Parse(`// Code generated by platformsh/generator/main.go
5320
// DO NOT EDIT
5421
@@ -112,20 +79,12 @@ func generateCommands() {
11279
}
11380

11481
func parseCommands(cloudPath string) (string, error) {
115-
var buf bytes.Buffer
116-
var bufErr bytes.Buffer
117-
cmd := exec.Command(cloudPath, "list", "--format=json", "--all")
118-
cmd.Stdout = &buf
119-
cmd.Stderr = &bufErr
120-
if err := cmd.Run(); err != nil {
121-
return "", errors.Errorf("unable to list commands: %s\n%s\n%s", err, bufErr.String(), buf.String())
82+
wd, err := os.Getwd()
83+
if err != nil {
84+
return "", err
12285
}
123-
124-
// Fix PHP types
125-
cleanOutput := bytes.ReplaceAll(buf.Bytes(), []byte(`"arguments":[]`), []byte(`"arguments":{}`))
126-
127-
var definition application
128-
if err := json.Unmarshal(cleanOutput, &definition); err != nil {
86+
cliApp, err := symfony.NewGoCliApp(wd, cloudPath, []string{"--all"})
87+
if err != nil {
12988
return "", err
13089
}
13190

@@ -147,7 +106,7 @@ func parseCommands(cloudPath string) (string, error) {
147106
excludedOptions = append(excludedOptions, console.VersionFlag.Names()...)
148107

149108
definitionAsString := ""
150-
for _, command := range definition.Commands {
109+
for _, command := range cliApp.Commands {
151110
if strings.Contains(command.Description, "deprecated") || strings.Contains(command.Description, "DEPRECATED") {
152111
continue
153112
}
@@ -162,7 +121,7 @@ func parseCommands(cloudPath string) (string, error) {
162121
}
163122
namespace := "cloud"
164123
loop:
165-
for _, n := range definition.Namespaces {
124+
for _, n := range cliApp.Namespaces {
166125
for _, name := range n.Commands {
167126
if name == command.Name {
168127
if n.ID != "_global" {
@@ -282,7 +241,7 @@ func getCommandAliases(name, cloudPath string) ([]string, error) {
282241
return []string{}, nil
283242
//return nil, errors.Errorf("unable to get definition for command %s: %s\n%s\n%s", name, err, bufErr.String(), buf.String())
284243
}
285-
var cmd command
244+
var cmd symfony.CliCommand
286245
if err := json.Unmarshal(buf.Bytes(), &cmd); err != nil {
287246
return nil, err
288247
}

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy