-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
cli.go
102 lines (80 loc) · 2.27 KB
/
cli.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package cli
import (
"fmt"
"io"
"github.com/olekukonko/tablewriter"
mcli "github.com/mitchellh/cli"
)
// Ui implements the mitchellh/cli.Ui interface, while exposing the underlying
// io.Writer used for stdout and stderr.
// TODO: rename to UI to match golang style guide
type Ui interface {
mcli.Ui
Stdout() io.Writer
Stderr() io.Writer
ErrorOutput(string)
HeaderOutput(string)
WarnOutput(string)
SuccessOutput(string)
UnchangedOutput(string)
Table(tbl *Table)
}
// BasicUI augments mitchellh/cli.BasicUi by exposing the underlying io.Writer.
type BasicUI struct {
mcli.BasicUi
}
func (b *BasicUI) Stdout() io.Writer {
return b.BasicUi.Writer
}
func (b *BasicUI) Stderr() io.Writer {
return b.BasicUi.ErrorWriter
}
func (b *BasicUI) HeaderOutput(s string) {
b.Output(colorize(fmt.Sprintf("==> %s", s), UiColorNone))
}
func (b *BasicUI) ErrorOutput(s string) {
b.Output(colorize(fmt.Sprintf(" ! %s", s), UiColorRed))
}
func (b *BasicUI) WarnOutput(s string) {
b.Output(colorize(fmt.Sprintf(" * %s", s), UiColorYellow))
}
func (b *BasicUI) SuccessOutput(s string) {
b.Output(colorize(fmt.Sprintf(" ✓ %s", s), UiColorGreen))
}
func (b *BasicUI) UnchangedOutput(s string) {
b.Output(colorize(fmt.Sprintf(" %s", s), UiColorNone))
}
// Command is an alias to reduce the diff. It can be removed at any time.
type Command mcli.Command
// Table implements UI.
func (u *BasicUI) Table(tbl *Table) {
// Build our config and set our options
table := tablewriter.NewWriter(u.Stdout())
table.SetHeader(tbl.Headers)
table.SetBorder(false)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(false)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
for _, row := range tbl.Rows {
colors := make([]tablewriter.Colors, len(row))
entries := make([]string, len(row))
for i, ent := range row {
entries[i] = ent.Value
color, ok := colorMapping[ent.Color]
if ok {
colors[i] = tablewriter.Colors{color}
}
}
table.Rich(entries, colors)
}
table.Render()
}