forked from juju/juju
/
output_oneline.go
60 lines (51 loc) · 1.63 KB
/
output_oneline.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package status
import (
"fmt"
"io"
"strings"
"github.com/juju/errors"
"github.com/juju/naturalsort"
)
// FormatOneline writes a brief list of units and their subordinates.
// Subordinates will be indented 2 spaces and listed under their
// superiors. This format works with version 2 of the CLI.
func FormatOneline(writer io.Writer, value interface{}) error {
return formatOneline(writer, value, func(out io.Writer, format, uName string, u unitStatus, level int) {
status := fmt.Sprintf(
"agent:%s, workload:%s",
u.JujuStatusInfo.Current,
u.WorkloadStatusInfo.Current,
)
fmt.Fprintf(out, format,
uName,
u.PublicAddress,
status,
)
})
}
type onelinePrintf func(out io.Writer, format, uName string, u unitStatus, level int)
func formatOneline(writer io.Writer, value interface{}, printf onelinePrintf) error {
fs, valueConverted := value.(formattedStatus)
if !valueConverted {
return errors.Errorf("expected value of type %T, got %T", fs, value)
}
pprint := func(uName string, u unitStatus, level int) {
var fmtPorts string
if len(u.OpenedPorts) > 0 {
fmtPorts = fmt.Sprintf(" %s", strings.Join(u.OpenedPorts, ", "))
}
format := indent("\n", level*2, "- %s: %s (%v)"+fmtPorts)
printf(writer, format, uName, u, level)
}
for _, svcName := range naturalsort.Sort(stringKeysFromMap(fs.Applications)) {
svc := fs.Applications[svcName]
for _, uName := range naturalsort.Sort(stringKeysFromMap(svc.Units)) {
unit := svc.Units[uName]
pprint(uName, unit, 0)
recurseUnits(unit, 1, pprint)
}
}
return nil
}