2023-04-19 08:28:09 +03:00
|
|
|
package tablew
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
"github.com/fatih/color"
|
|
|
|
|
|
|
|
"github.com/samber/lo"
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
2023-04-19 08:28:09 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/output"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ output.ConfigWriter = (*configWriter)(nil)
|
|
|
|
|
|
|
|
// configWriter implements output.ConfigWriter.
|
|
|
|
type configWriter struct {
|
|
|
|
tbl *table
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfigWriter returns a new output.ConfigWriter.
|
2023-04-22 06:36:32 +03:00
|
|
|
func NewConfigWriter(out io.Writer, pr *output.Printing) output.ConfigWriter {
|
|
|
|
tbl := &table{out: out, pr: pr, header: pr.ShowHeader}
|
2023-04-19 08:28:09 +03:00
|
|
|
tbl.reset()
|
|
|
|
return &configWriter{tbl: tbl}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Location implements output.ConfigWriter.
|
|
|
|
func (w *configWriter) Location(path, origin string) error {
|
|
|
|
fmt.Fprintln(w.tbl.out, path)
|
2023-04-22 06:36:32 +03:00
|
|
|
if w.tbl.pr.Verbose && origin != "" {
|
|
|
|
w.tbl.pr.Faint.Fprint(w.tbl.out, "Origin: ")
|
|
|
|
w.tbl.pr.String.Fprintln(w.tbl.out, origin)
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 17:32:50 +03:00
|
|
|
// Opt implements output.ConfigWriter.
|
|
|
|
func (w *configWriter) Opt(o options.Options, opt options.Opt) error {
|
|
|
|
if o == nil || opt == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
o2 := options.Options{opt.Key(): o[opt.Key()]}
|
|
|
|
reg2 := &options.Registry{}
|
|
|
|
reg2.Add(opt)
|
|
|
|
return w.Options(reg2, o2)
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// Options implements output.ConfigWriter.
|
2023-05-01 06:59:34 +03:00
|
|
|
func (w *configWriter) Options(reg *options.Registry, o options.Options) error {
|
|
|
|
if o == nil {
|
2023-04-19 08:28:09 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
if w.tbl.pr.Verbose {
|
|
|
|
w.tbl.pr.ShowHeader = true
|
|
|
|
} else {
|
|
|
|
w.tbl.pr.ShowHeader = false
|
|
|
|
}
|
|
|
|
|
|
|
|
w.doPrintOptions(reg, o, true)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options implements output.ConfigWriter.
|
|
|
|
// If printUnset is true and we're in verbose mode, unset options
|
|
|
|
// are also printed.
|
|
|
|
func (w *configWriter) doPrintOptions(reg *options.Registry, o options.Options, printUnset bool) {
|
|
|
|
if o == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t, pr, verbose := w.tbl.tblImpl, w.tbl.pr, w.tbl.pr.Verbose
|
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
if pr.ShowHeader {
|
2023-05-01 06:59:34 +03:00
|
|
|
headers := []string{"KEY", "VALUE"}
|
|
|
|
if verbose {
|
|
|
|
headers = []string{"KEY", "VALUE", "DEFAULT"}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.SetHeader(headers)
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
2023-04-22 06:36:32 +03:00
|
|
|
t.SetColTrans(0, pr.Key.SprintFunc())
|
2023-04-19 08:28:09 +03:00
|
|
|
|
|
|
|
var rows [][]string
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
keys := o.Keys()
|
2023-04-26 18:16:42 +03:00
|
|
|
for _, k := range keys {
|
2023-05-01 06:59:34 +03:00
|
|
|
opt := reg.Get(k)
|
|
|
|
if opt == nil {
|
|
|
|
// Shouldn't happen, but print anyway just in case
|
|
|
|
row := []string{
|
|
|
|
k,
|
|
|
|
pr.Error.Sprintf("%v", o[k]),
|
|
|
|
}
|
|
|
|
if verbose {
|
|
|
|
// Can't know default in this situation.
|
|
|
|
row = append(row, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
rows = append(rows, row)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
clr := getOptColor(pr, opt)
|
|
|
|
|
2023-05-07 09:01:29 +03:00
|
|
|
val, ok := o[k]
|
|
|
|
if !ok || val == nil {
|
|
|
|
if verbose {
|
|
|
|
val = ""
|
|
|
|
} else {
|
|
|
|
val = "NULL"
|
|
|
|
}
|
|
|
|
clr = pr.Null
|
|
|
|
}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
row := []string{
|
|
|
|
k,
|
2023-05-07 09:01:29 +03:00
|
|
|
clr.Sprintf("%v", val),
|
2023-05-01 06:59:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if verbose {
|
|
|
|
defaultVal := pr.Faint.Sprintf("%v", opt.GetAny(nil))
|
|
|
|
row = append(row, defaultVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows = append(rows, row)
|
|
|
|
}
|
2023-04-19 08:28:09 +03:00
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
if !printUnset || !verbose {
|
|
|
|
w.tbl.appendRowsAndRenderAll(rows)
|
|
|
|
return
|
2023-04-26 18:16:42 +03:00
|
|
|
}
|
2023-04-19 08:28:09 +03:00
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
// Also print the unset opts
|
|
|
|
allKeys := reg.Keys()
|
|
|
|
setKeys := o.Keys()
|
|
|
|
unsetKeys, _ := lo.Difference(allKeys, setKeys)
|
|
|
|
|
|
|
|
for _, k := range unsetKeys {
|
|
|
|
opt := reg.Get(k)
|
|
|
|
row := []string{
|
|
|
|
pr.Faint.Sprintf("%v", k),
|
|
|
|
"", // opt is unset, so it doesn't have a value
|
|
|
|
pr.Faint.Sprintf("%v", opt.GetAny(nil)),
|
|
|
|
}
|
|
|
|
|
|
|
|
rows = append(rows, row)
|
|
|
|
}
|
2023-04-19 08:28:09 +03:00
|
|
|
|
|
|
|
w.tbl.appendRowsAndRenderAll(rows)
|
2023-05-01 06:59:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetOption implements output.ConfigWriter.
|
2023-05-05 17:32:50 +03:00
|
|
|
func (w *configWriter) SetOption(o options.Options, opt options.Opt) error {
|
2023-05-01 06:59:34 +03:00
|
|
|
if !w.tbl.pr.Verbose {
|
|
|
|
// No output unless verbose
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-03 15:36:10 +03:00
|
|
|
reg2 := &options.Registry{}
|
|
|
|
reg2.Add(opt)
|
|
|
|
o = options.Options{opt.Key(): opt.GetAny(o)}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
// It's verbose
|
|
|
|
o = options.Effective(o, opt)
|
2023-05-03 15:36:10 +03:00
|
|
|
w.tbl.pr.ShowHeader = true
|
|
|
|
w.doPrintOptions(reg2, o, false)
|
|
|
|
return nil
|
2023-05-01 06:59:34 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 17:32:50 +03:00
|
|
|
// UnsetOption implements output.ConfigWriter.
|
|
|
|
func (w *configWriter) UnsetOption(opt options.Opt) error {
|
|
|
|
if !w.tbl.pr.Verbose {
|
|
|
|
// No output unless verbose
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
reg := &options.Registry{}
|
|
|
|
reg.Add(opt)
|
|
|
|
o := options.Options{}
|
|
|
|
|
|
|
|
w.doPrintOptions(reg, o, true)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
func getOptColor(pr *output.Printing, opt options.Opt) *color.Color {
|
|
|
|
if opt == nil {
|
|
|
|
return pr.Null
|
|
|
|
}
|
|
|
|
|
|
|
|
clr := pr.String
|
|
|
|
switch opt.(type) {
|
|
|
|
case options.Bool:
|
|
|
|
clr = pr.Bool
|
|
|
|
case options.Int:
|
|
|
|
clr = pr.Number
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
return clr
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|