/
describe.go
222 lines (197 loc) · 7.25 KB
/
describe.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
package cmd
import (
"bytes"
"fmt"
"github.com/drud/ddev/pkg/ddevapp"
"github.com/drud/ddev/pkg/dockerutil"
"github.com/drud/ddev/pkg/globalconfig"
"github.com/drud/ddev/pkg/nodeps"
"github.com/drud/ddev/pkg/output"
"github.com/drud/ddev/pkg/styles"
"github.com/drud/ddev/pkg/util"
"sort"
"strings"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra"
)
// DescribeCommand represents the `ddev config` command
var DescribeCommand = &cobra.Command{
Use: "describe [projectname]",
Aliases: []string{"status", "st", "desc"},
Short: "Get a detailed description of a running ddev project.",
Long: `Get a detailed description of a running ddev project. Describe provides basic
information about a ddev project, including its name, location, url, and status.
It also provides details for MySQL connections, and connection information for
additional services like MailHog and phpMyAdmin. You can run 'ddev describe' from
a project directory to describe that project, or you can specify a project to describe by
running 'ddev describe <projectname>'.`,
Example: "ddev describe\nddev describe <projectname>\nddev status\nddev st",
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
util.Failed("Too many arguments provided. Please use 'ddev describe' or 'ddev describe [projectname]'")
}
apps, err := getRequestedProjects(args, false)
if err != nil {
util.Failed("Failed to describe project(s): %v", err)
}
app := apps[0]
if err := ddevapp.CheckForMissingProjectFiles(app); err != nil {
util.Failed("Failed to describe %s: %v", app.Name, err)
}
desc, err := app.Describe(false)
if err != nil {
util.Failed("Failed to describe project %s: %v", app.Name, err)
}
renderedDesc, err := renderAppDescribe(app, desc)
util.CheckErr(err) // We shouldn't ever end up with an unrenderable desc.
output.UserOut.WithField("raw", desc).Print(renderedDesc)
},
}
// renderAppDescribe takes the map describing the app and renders it for plain-text output
func renderAppDescribe(app *ddevapp.DdevApp, desc map[string]interface{}) (string, error) {
status := desc["status"]
var out bytes.Buffer
t := table.NewWriter()
t.SetOutputMirror(&out)
styles.SetGlobalTableStyle(t)
tWidth, _ := nodeps.GetTerminalWidthHeight()
urlPortWidth := float64(35)
infoWidth := 30
urlPortWidthFactor := float64(2.5)
if tWidth != 0 {
urlPortWidth = float64(tWidth) / urlPortWidthFactor
infoWidth = tWidth / 4
}
util.Debug("detected terminal width=%v urlPortWidth=%v infoWidth=%v", tWidth, urlPortWidth, infoWidth)
if !globalconfig.DdevGlobalConfig.SimpleFormatting {
t.SetColumnConfigs([]table.ColumnConfig{
{
Name: "Service",
WidthMax: 10,
},
{
Name: "URL/Port",
//WidthMax: int(urlPortWidth),
},
{
Name: "Info",
WidthMax: infoWidth,
},
})
}
dockerEnv := fmt.Sprintf("docker %s", dockerutil.DockerVersion)
if dockerutil.IsColima() {
dockerEnv = "Colima"
}
router := "traditional"
if globalconfig.DdevGlobalConfig.UseTraefik {
router = "traefik"
}
t.SetTitle(fmt.Sprintf("Project: %s %s %s\nDocker provider: %s\nRouter: %s", app.Name, desc["shortroot"].(string), app.GetPrimaryURL(), dockerEnv, router))
t.AppendHeader(table.Row{"Service", "Stat", "URL/Port", "Info"})
// Only show extended status for running sites.
if status == ddevapp.SiteRunning {
serviceNames := []string{}
// Get a list of services in the order we want them, with web and db first
serviceMap := desc["services"].(map[string]map[string]string)
for k := range serviceMap {
if k != "web" && k != "db" {
serviceNames = append(serviceNames, k)
}
}
sort.Strings(serviceNames)
if _, ok := desc["dbinfo"]; ok {
serviceNames = append([]string{"db"}, serviceNames...)
}
serviceNames = append([]string{"web"}, serviceNames...)
for _, k := range serviceNames {
v := serviceMap[k]
httpURL := ""
urlPortParts := []string{}
switch {
// Normal case, using ddev-router based URLs
case !ddevapp.IsRouterDisabled(app):
if httpsURL, ok := v["https_url"]; ok {
urlPortParts = append(urlPortParts, httpsURL)
} else if httpURL, ok = v["http_url"]; ok {
urlPortParts = append(urlPortParts, httpURL)
}
// Gitpod, web container only, using port proxied by gitpod
case nodeps.IsGitpod() && k == "web":
urlPortParts = append(urlPortParts, app.GetPrimaryURL())
// Router disabled, but not because of gitpod, use direct http url
case ddevapp.IsRouterDisabled(app):
httpURL = v["host_http_url"]
if httpURL != "" {
urlPortParts = append(urlPortParts, httpURL)
}
}
if p, ok := v["exposed_ports"]; ok {
urlPortParts = append(urlPortParts, "InDocker: "+v["short_name"]+":"+p)
}
if p, ok := v["host_ports"]; ok && p != "" {
urlPortParts = append(urlPortParts, "Host: 127.0.0.1:"+p)
}
extraInfo := []string{}
// Get extra info for web container
if k == "web" {
extraInfo = append(extraInfo, fmt.Sprintf("%s PHP%s\n%s\ndocroot:'%s'", desc["type"], desc["php_version"], desc["webserver_type"], desc["docroot"]))
if desc["nfs_mount_enabled"].(bool) {
extraInfo = append(extraInfo, fmt.Sprintf("NFS Enabled"))
}
if desc["mutagen_enabled"].(bool) {
extraInfo = append(extraInfo, fmt.Sprintf("Mutagen enabled (%s)", ddevapp.FormatSiteStatus(desc["mutagen_status"].(string))))
}
if v, ok := desc["nodejs_version"].(string); ok {
extraInfo = append(extraInfo, fmt.Sprintf("NodeJS:%s", v))
}
}
// Get extra info for db container
if k == "db" {
extraInfo = append(extraInfo, app.Database.Type+":"+app.Database.Version)
extraInfo = append(extraInfo, "User/Pass: 'db/db'\nor 'root/root'")
}
if k == "dba" {
k = "PHPMyAdmin"
urlPortParts = append(urlPortParts, "`ddev launch -p`")
}
t.AppendRow(table.Row{k, ddevapp.FormatSiteStatus(v["status"]), strings.Join(urlPortParts, "\n"), strings.Join(extraInfo, "\n")})
}
if !ddevapp.IsRouterDisabled(app) {
// MailHog stanza
mailhogURL := ""
if _, ok := desc["mailhog_url"]; ok {
mailhogURL = desc["mailhog_url"].(string)
}
if _, ok := desc["mailhog_https_url"]; ok {
mailhogURL = desc["mailhog_https_url"].(string)
}
t.AppendRow(table.Row{"Mailhog", "", fmt.Sprintf("MailHog: %s\n`ddev launch -m`", mailhogURL)})
//WebExtraExposedPorts stanza
for _, extraPort := range app.WebExtraExposedPorts {
t.AppendRow(table.Row{extraPort.Name, "", fmt.Sprintf("InDocker: localhost:%d https://%s:%d http://%s:%d", extraPort.WebContainerPort, app.GetHostname(), extraPort.HTTPSPort, app.GetHostname(), extraPort.HTTPPort)})
}
// All URLs stanza
_, _, urls := app.GetAllURLs()
s := strings.Join(urls, ", ")
urlString := text.WrapSoft(s, int(urlPortWidth))
t.AppendRow(table.Row{"All URLs", "", urlString})
}
bindInfo := []string{}
if app.BindAllInterfaces {
bindInfo = append(bindInfo, "bind-all-interfaces ENABLED")
}
if globalconfig.DdevGlobalConfig.RouterBindAllInterfaces && !ddevapp.IsRouterDisabled(app) {
bindInfo = append(bindInfo, "router-bind-all-interfaces ENABLED")
}
if len(bindInfo) > 0 {
t.AppendRow(table.Row{"Network", "", strings.Join(bindInfo, "\n")})
}
}
t.Render()
return out.String(), nil
}
func init() {
RootCmd.AddCommand(DescribeCommand)
}