@@ -16,6 +16,14 @@ import (
16
16
"gitlab.com/postgres-ai/database-lab/cmd/cli/commands"
17
17
)
18
18
19
+ // headers of a config list.
20
+ const (
21
+ envHeader = "ENV "
22
+ urlHeader = "URL"
23
+ fwServerHeader = "Forwarding server URL"
24
+ fwPortHeader = "Forwarding local port"
25
+ )
26
+
19
27
// createEnvironment creates a new CLI environment.
20
28
func createEnvironment () func (* cli.Context ) error {
21
29
return func (cliCtx * cli.Context ) (err error ) {
@@ -119,32 +127,54 @@ func list() func(*cli.Context) error {
119
127
}
120
128
121
129
environmentNames := make ([]string , 0 , len (cfg .Environments ))
122
- maxNameLength := 0
130
+ maxNameLen := 0
131
+ maxURLLen := len (urlHeader )
132
+ maxFwServerLen := len (fwServerHeader )
123
133
124
134
for environmentName := range cfg .Environments {
125
135
environmentNames = append (environmentNames , environmentName )
126
136
127
137
nameLength := len (environmentName )
128
- if maxNameLength < nameLength {
129
- maxNameLength = nameLength
138
+ if maxNameLen < nameLength {
139
+ maxNameLen = nameLength
140
+ }
141
+
142
+ urlLength := len (cfg .Environments [environmentName ].URL )
143
+ if maxURLLen < urlLength {
144
+ maxURLLen = urlLength
145
+ }
146
+
147
+ urlFwLength := len (cfg .Environments [environmentName ].Forwarding .ServerURL )
148
+ if maxFwServerLen < urlFwLength {
149
+ maxFwServerLen = urlFwLength
130
150
}
131
151
}
132
152
133
153
sort .Strings (environmentNames )
134
154
135
- listOutput := buildListOutput (cfg , environmentNames , maxNameLength )
155
+ listOutput := buildListOutput (cfg , environmentNames , maxNameLen , maxURLLen , maxFwServerLen )
136
156
137
157
_ , err = fmt .Fprintf (cliCtx .App .Writer , "Available CLI environments:\n %s" , listOutput )
138
158
139
159
return commands .ToActionError (err )
140
160
}
141
161
}
142
162
143
- func buildListOutput (cfg * CLIConfig , environmentNames []string , maxNameLength int ) string {
163
+ func buildListOutput (cfg * CLIConfig , environmentNames []string , maxNameLen , maxURLLen , maxFwLen int ) string {
164
+ // TODO(akartasov): Draw as a table.
144
165
const outputAlign = 2
145
166
146
167
s := strings.Builder {}
147
168
169
+ s .WriteString (envHeader )
170
+ s .WriteString (strings .Repeat (" " , maxNameLen + outputAlign ))
171
+ s .WriteString (urlHeader )
172
+ s .WriteString (strings .Repeat (" " , maxURLLen - len (urlHeader )+ outputAlign ))
173
+ s .WriteString (fwServerHeader )
174
+ s .WriteString (strings .Repeat (" " , maxFwLen - len (fwServerHeader )+ outputAlign ))
175
+ s .WriteString (fwPortHeader )
176
+ s .WriteString ("\n " )
177
+
148
178
for _ , environmentName := range environmentNames {
149
179
if environmentName == cfg .CurrentEnvironment {
150
180
s .WriteString ("[*] " )
@@ -153,8 +183,12 @@ func buildListOutput(cfg *CLIConfig, environmentNames []string, maxNameLength in
153
183
}
154
184
155
185
s .WriteString (environmentName )
156
- s .WriteString (strings .Repeat (" " , maxNameLength - len (environmentName )+ outputAlign ))
186
+ s .WriteString (strings .Repeat (" " , maxNameLen - len (environmentName )+ outputAlign ))
157
187
s .WriteString (cfg .Environments [environmentName ].URL )
188
+ s .WriteString (strings .Repeat (" " , maxURLLen - len (cfg .Environments [environmentName ].URL )+ outputAlign ))
189
+ s .WriteString (cfg .Environments [environmentName ].Forwarding .ServerURL )
190
+ s .WriteString (strings .Repeat (" " , maxFwLen - len (cfg .Environments [environmentName ].Forwarding .ServerURL )+ outputAlign ))
191
+ s .WriteString (cfg .Environments [environmentName ].Forwarding .LocalPort )
158
192
s .WriteString ("\n " )
159
193
}
160
194
0 commit comments