-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
299 lines (271 loc) · 6.62 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/labstack/gommon/log"
homedir "github.com/mitchellh/go-homedir"
"github.com/urfave/cli"
)
// Mysql holds the mysql connection information for a site
type Mysql struct {
Database string `json:"database"`
User string `json:"user"`
Password string `json:"password"`
}
// Ports holds the ports each service runs on
type Ports struct {
HTTP int `json:"HTTP"`
HTTPS int `json:"HTTPS"`
MYSQL int `json:"MYSQL"`
MAILCATCHER int `json:"MAILCATCHER"`
}
// Config holds the configuration for a single site
type Config struct {
Blueprint string `json:"blueprint"`
Environment string `json:"environment"`
PhpVersion string `json:"phpVersion"`
MysqlVersion string `json:"mysqlVersion"`
WebServer string `json:"webServer"`
DevMode bool `json:"devMode"`
AdminUsername string `json:"adminUsername"`
AdminPassword string `json:"adminPassword"`
AdminEmail string `json:"adminEmail"`
MultiSite string `json:"multiSite"`
Name string `json:"name"`
Path string `json:"path"`
Domain string `json:"domain"`
ID string `json:"id"`
LocalVersion string `json:"localVersion"`
Container string `json:"container"`
Mysql Mysql `json:"mysql"`
Ports Ports `json:"ports"`
EnvironmentVersion string `json:"environmentVersion"`
SslSHA1 string `json:"sslSHA1"`
}
func main() {
app := cli.NewApp()
app.Name = "lfw"
app.Version = "0.1.0"
app.Description = "Client for Local by Flywheel"
app.Usage = "Client for Local by Flywheel"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "site",
Usage: "Site to access",
EnvVar: "LFW_SITE",
},
cli.StringFlag{
Name: "config",
Value: ConfigDir,
Usage: "Local by Flywheel configuration directory",
EnvVar: "LFW_CONFIG",
},
cli.StringFlag{
Name: "dockerdir",
Value: DockerDir,
Usage: "Directory containing docker binaries",
EnvVar: "LFW_DOCKER_DIR",
},
cli.StringFlag{
Name: "dockername",
Value: DockerName,
Usage: "Docker instance name",
EnvVar: "LFW_DOCKER_NAME",
},
}
app.Commands = []cli.Command{
{
Name: "shell",
Aliases: []string{"sh", "ssh"},
Usage: "open a shell on a site",
Action: CmdShell,
},
{
Name: "wp",
Usage: "run 'wp' on a site",
Action: CmdWp,
},
{
Name: "mysql",
Usage: "run 'mysql' on a site",
Action: CmdMysql,
},
{
Name: "command",
Aliases: []string{"cmd", "c"},
Usage: "run a command on a site",
SkipFlagParsing: true,
Action: CmdCommand,
},
{
Name: "list",
Usage: "list all sites",
Action: CmdList,
},
{
Name: "env",
Usage: "show docker environment",
Action: CmdEnv,
},
{
Name: "id",
Usage: "show container id for a site",
Action: CmdId,
},
{
Name: "info",
Usage: "show configuration for a site",
Action: CmdInfo,
},
{
Name: "ip",
Usage: "show IP address for a site",
Action: CmdIp,
},
{
Name: "dburi",
Usage: "get MySQL connection string for a site",
Action: CmdDburi,
},
}
err := app.Run(os.Args)
if err != nil {
// log.Fatal(err)
os.Exit(1)
}
}
var cachedConfig map[string]Config
// Configs loads the configuration for all sites
func Configs(c *cli.Context) (map[string]Config, error) {
if cachedConfig != nil {
return cachedConfig, nil
}
var err error
ret := map[string]Config{}
configFile := filepath.Join(c.GlobalString("config"), "sites.json")
if strings.HasPrefix(configFile, "~") {
configFile, err = homedir.Expand(configFile)
if err != nil {
return map[string]Config{}, err
}
}
cf, err := os.Open(configFile)
if err != nil {
return map[string]Config{}, err
}
decoder := json.NewDecoder(cf)
err = decoder.Decode(&ret)
if err != nil {
return map[string]Config{}, err
}
cachedConfig = ret
return cachedConfig, nil
}
// ConfigByName returns the configuration for a server
func ConfigByName(c *cli.Context, name string) (Config, error) {
configs, err := Configs(c)
if err != nil {
return Config{}, err
}
for _, conf := range configs {
if conf.Domain == name {
return conf, nil
}
}
for _, conf := range configs {
if conf.Name == name {
return conf, nil
}
}
for _, conf := range configs {
if conf.Domain == name+".local" {
return conf, nil
}
}
conf, ok := configs[name]
if ok {
return conf, nil
}
return Config{}, fmt.Errorf("no such server as '%s'", name)
}
// CurrentSite returns the current site, either explicitly or heuristically
func CurrentSite(c *cli.Context) (string, error) {
// Explicitly set, as flag or environment variable
site := c.GlobalString("site")
if site != "" {
return site, nil
}
confs, err := Configs(c)
if err != nil {
return "", err
}
// Heuristics time - check to see if we're in an app directory
wd, err := os.Getwd()
if err != nil {
log.Errorf("Couldn't get working directory: %s", err.Error())
} else {
for _, conf := range confs {
installdir, err := homedir.Expand(conf.Path)
if err == nil {
if strings.HasPrefix(wd, installdir) {
return conf.Domain, nil
}
}
}
}
// Maybe there's just a single site running
var statuses map[string]string
statusesFile := filepath.Join(c.GlobalString("config"), "site-statuses.json")
if strings.HasPrefix(statusesFile, "~") {
statusesFile, err = homedir.Expand(statusesFile)
if err != nil {
return "", err
}
}
cf, err := os.Open(statusesFile)
if err != nil {
return "", err
}
decoder := json.NewDecoder(cf)
err = decoder.Decode(&statuses)
if err != nil {
return "", err
}
running := []string{}
for k, v := range statuses {
if v == "running" {
running = append(running, k)
}
}
if len(running) != 1 {
list := ""
if len(running) <= 3 {
names := []string{}
for _, tag := range running {
cf, ok := confs[tag]
if ok {
names = append(names, strings.TrimSuffix(cf.Domain, ".local"))
}
}
list = fmt.Sprintf(" (%s)", strings.Join(names, ", "))
}
return "", fmt.Errorf("I can't guess which site to access - try with --site or $LFW_SITE%s", list)
}
conf, ok := confs[running[0]]
if !ok {
return "", fmt.Errorf("Can't find supposedly running site '%s'", running[0])
}
// TODO(steve): dotfiles in a project directory?
return conf.Domain, nil
}
// CurrentConfig returns the config of the current site
func CurrentConfig(c *cli.Context) (Config, error) {
name, err := CurrentSite(c)
if err != nil {
return Config{}, err
}
return ConfigByName(c, name)
}