Skip to content

Commit 0718fac

Browse files
Make list output a little more useful/readable
1 parent 9bd82c9 commit 0718fac

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

internal/cmd/list.go

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package cmd
22

33
import (
4-
"encoding/json"
4+
"fmt"
55
"net/rpc"
6-
"os"
6+
"strings"
77

88
"github.com/spf13/cobra"
99

@@ -36,8 +36,39 @@ func (c *listCommand) run(cmd *cobra.Command, args []string) error {
3636
return err
3737
}
3838

39-
json.NewEncoder(os.Stdout).Encode(response)
40-
39+
c.displayResponse(response)
4140
return nil
4241
})
4342
}
43+
44+
func (c *listCommand) displayResponse(reponse server.ListResponse) {
45+
fmt.Println(c.format("Service", 30, italic) +
46+
c.format("Host", 30, italic) + c.format("Target", 30, italic) +
47+
c.format("State", 10, italic) + c.format("TLS", 10, italic))
48+
49+
for name, service := range reponse.Targets {
50+
tls := "no"
51+
if service.TLS {
52+
tls = "yes"
53+
}
54+
55+
fmt.Println(c.format(name, 30, bold) +
56+
c.format(service.Host, 30, plain) + c.format(service.Target, 30, plain) +
57+
c.format(service.State, 10, plain) + c.format(tls, 10, plain))
58+
}
59+
}
60+
61+
const (
62+
plain = ""
63+
bold = "1;34"
64+
italic = "3;94"
65+
)
66+
67+
func (c *listCommand) format(text string, width int, style string) string {
68+
text = (text + strings.Repeat(" ", width-len(text)))[0:width]
69+
if style != "" {
70+
text = "\033[" + style + "m" + text + "\033[0m"
71+
}
72+
73+
return text
74+
}

internal/server/pause_control.go

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ const (
1313
PauseStateStopped
1414
)
1515

16+
func (ps PauseState) String() string {
17+
switch ps {
18+
case PauseStateRunning:
19+
return "running"
20+
case PauseStatePaused:
21+
return "paused"
22+
case PauseStateStopped:
23+
return "stopped"
24+
default:
25+
return ""
26+
}
27+
}
28+
1629
type PauseWaitAction int
1730

1831
const (

internal/server/router.go

+4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ type Router struct {
2929

3030
type ServiceDescription struct {
3131
Host string `json:"host"`
32+
TLS bool `json:"tls"`
3233
Target string `json:"target"`
34+
State string `json:"state"`
3335
}
3436

3537
type ServiceDescriptionMap map[string]ServiceDescription
@@ -167,6 +169,8 @@ func (r *Router) ListActiveServices() ServiceDescriptionMap {
167169
result[service.name] = ServiceDescription{
168170
Host: host,
169171
Target: service.active.Target(),
172+
TLS: service.options.RequireTLS(),
173+
State: service.pauseControl.State().String(),
170174
}
171175
}
172176
}

0 commit comments

Comments
 (0)