Skip to content

Commit

Permalink
Make list output a little more useful/readable
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmcconnell committed Jul 24, 2024
1 parent 9bd82c9 commit 0718fac
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
39 changes: 35 additions & 4 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cmd

import (
"encoding/json"
"fmt"
"net/rpc"
"os"
"strings"

"github.com/spf13/cobra"

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

json.NewEncoder(os.Stdout).Encode(response)

c.displayResponse(response)
return nil
})
}

func (c *listCommand) displayResponse(reponse server.ListResponse) {
fmt.Println(c.format("Service", 30, italic) +
c.format("Host", 30, italic) + c.format("Target", 30, italic) +
c.format("State", 10, italic) + c.format("TLS", 10, italic))

for name, service := range reponse.Targets {
tls := "no"
if service.TLS {
tls = "yes"
}

fmt.Println(c.format(name, 30, bold) +
c.format(service.Host, 30, plain) + c.format(service.Target, 30, plain) +
c.format(service.State, 10, plain) + c.format(tls, 10, plain))
}
}

const (
plain = ""
bold = "1;34"
italic = "3;94"
)

func (c *listCommand) format(text string, width int, style string) string {
text = (text + strings.Repeat(" ", width-len(text)))[0:width]
if style != "" {
text = "\033[" + style + "m" + text + "\033[0m"
}

return text
}
13 changes: 13 additions & 0 deletions internal/server/pause_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ const (
PauseStateStopped
)

func (ps PauseState) String() string {
switch ps {
case PauseStateRunning:
return "running"
case PauseStatePaused:
return "paused"
case PauseStateStopped:
return "stopped"
default:
return ""
}
}

type PauseWaitAction int

const (
Expand Down
4 changes: 4 additions & 0 deletions internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ type Router struct {

type ServiceDescription struct {
Host string `json:"host"`
TLS bool `json:"tls"`
Target string `json:"target"`
State string `json:"state"`
}

type ServiceDescriptionMap map[string]ServiceDescription
Expand Down Expand Up @@ -167,6 +169,8 @@ func (r *Router) ListActiveServices() ServiceDescriptionMap {
result[service.name] = ServiceDescription{
Host: host,
Target: service.active.Target(),
TLS: service.options.RequireTLS(),
State: service.pauseControl.State().String(),
}
}
}
Expand Down

0 comments on commit 0718fac

Please sign in to comment.