2021-06-02 20:51:34 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-02-10 19:12:13 +00:00
|
|
|
"fmt"
|
2021-06-02 20:51:34 +00:00
|
|
|
"io"
|
|
|
|
|
2023-02-10 19:12:13 +00:00
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
|
2021-06-02 20:51:34 +00:00
|
|
|
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
|
2023-02-10 19:12:13 +00:00
|
|
|
ErrorOutput(string)
|
|
|
|
HeaderOutput(string)
|
|
|
|
WarnOutput(string)
|
|
|
|
SuccessOutput(string)
|
|
|
|
UnchangedOutput(string)
|
|
|
|
Table(tbl *Table)
|
2021-06-02 20:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-02-10 19:12:13 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2021-06-02 20:51:34 +00:00
|
|
|
// Command is an alias to reduce the diff. It can be removed at any time.
|
|
|
|
type Command mcli.Command
|
2023-02-10 19:12:13 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|