-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
commands.go
192 lines (173 loc) · 4.7 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/symfony-cli/symfony-cli/local/php"
)
type definition struct {
Namespaces []namespace
Commands []command
}
type namespace struct {
ID string
Commands []string
}
type command struct {
Name string
Usage []string
Description string
Help string
Hidden bool
}
var commandsTemplate = template.Must(template.New("output").Parse(`// Code generated by platformsh/generator/main.go
// DO NOT EDIT
/*
* Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
*
* This file is part of Symfony CLI project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package platformsh
import (
"github.com/symfony-cli/console"
)
var Commands = []*console.Command{
{{ .Definition -}}
}
`))
func generateCommands() {
home, err := homedir.Dir()
if err != nil {
panic(err)
}
if err := php.InstallPlatformPhar(home); err != nil {
panic(err.Error())
}
definitionAsString, err := parseCommands(home)
if err != nil {
panic(err.Error())
}
data := map[string]interface{}{
"Definition": definitionAsString,
}
var buf bytes.Buffer
if err := commandsTemplate.Execute(&buf, data); err != nil {
panic(err)
}
f, err := os.Create("local/platformsh/commands.go")
if err != nil {
panic(err)
}
f.Write(buf.Bytes())
}
func parseCommands(home string) (string, error) {
dir := filepath.Join(home, ".platformsh", "bin")
var pharPath = filepath.Join(dir, "platform")
hasher := md5.New()
if s, err := ioutil.ReadFile(pharPath); err != nil {
hasher.Write(s)
}
var buf bytes.Buffer
e := &php.Executor{
BinName: "php",
Args: []string{"php", filepath.Join(dir, "platform"), "list", "--format=json"},
}
e.Paths = append([]string{dir}, e.Paths...)
e.Dir = dir
e.Stdout = &buf
if ret := e.Execute(false); ret != 0 {
return "", errors.Errorf("unable to list commands: %s", buf.String())
}
// Fix PHP types
cleanOutput := bytes.ReplaceAll(buf.Bytes(), []byte(`"arguments":[]`), []byte(`"arguments":{}`))
var definition definition
if err := json.Unmarshal(cleanOutput, &definition); err != nil {
return "", err
}
allCommandNames := map[string]bool{}
for _, n := range definition.Namespaces {
for _, name := range n.Commands {
allCommandNames[name] = true
}
// FIXME: missing the aliases here
}
definitionAsString := ""
for _, command := range definition.Commands {
if strings.Contains(command.Description, "deprecated") || strings.Contains(command.Description, "DEPRECATED") {
continue
}
if command.Name == "list" || command.Name == "help" {
continue
}
if strings.HasPrefix(command.Name, "self:") {
command.Hidden = true
}
namespace := "cloud"
loop:
for _, n := range definition.Namespaces {
for _, name := range n.Commands {
if name == command.Name {
if n.ID != "_global" {
namespace += ":" + n.ID
}
break loop
}
}
}
name := strings.TrimPrefix("cloud:"+command.Name, namespace+":")
aliases := []string{}
if namespace != "cloud" {
aliases = append(aliases, fmt.Sprintf("{Name: \"%s\", Hidden: true}", command.Name))
}
for _, usage := range command.Usage {
if allCommandNames[usage] {
aliases = append(aliases, fmt.Sprintf("{Name: \"cloud:%s\"}", usage))
aliases = append(aliases, fmt.Sprintf("{Name: \"%s\", Hidden: true}", usage))
}
}
if command.Name == "environment:push" {
aliases = append(aliases, "{Name: \"deploy\"}")
aliases = append(aliases, "{Name: \"cloud:deploy\"}")
}
aliasesAsString := ""
if len(aliases) > 0 {
aliasesAsString += "\n\t\tAliases: []*console.Alias{\n"
for _, alias := range aliases {
aliasesAsString += "\t\t\t" + alias + ",\n"
}
aliasesAsString += "\t\t},"
}
hide := ""
if command.Hidden {
hide = "\n\t\tHidden: console.Hide,"
}
definitionAsString += fmt.Sprintf(` {
Category: "%s",
Name: "%s",%s
Usage: %#v,%s
},
`, namespace, name, aliasesAsString, command.Description, hide)
}
return definitionAsString, nil
}