2023-04-19 08:28:09 +03:00
|
|
|
package tablew
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options implements output.ConfigWriter.
|
2023-04-26 18:16:42 +03:00
|
|
|
func (w *configWriter) Options(opts options.Options) error {
|
2023-04-19 08:28:09 +03:00
|
|
|
if opts == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
t, pr := w.tbl.tblImpl, w.tbl.pr
|
|
|
|
if pr.ShowHeader {
|
2023-04-19 08:28:09 +03:00
|
|
|
t.SetHeader([]string{"KEY", "VALUE"})
|
|
|
|
}
|
2023-04-22 06:36:32 +03:00
|
|
|
t.SetColTrans(0, pr.Key.SprintFunc())
|
2023-04-19 08:28:09 +03:00
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
// FIXME: Verbose functionality should show defaults
|
2023-04-19 08:28:09 +03:00
|
|
|
var rows [][]string
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
keys := opts.Keys()
|
|
|
|
for _, k := range keys {
|
|
|
|
rows = append(rows, []string{k, fmt.Sprintf("%v", opts[k])})
|
2023-04-19 08:28:09 +03:00
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
// FIXME: need to check type of Opt from registry, and SetCellTrans
|
|
|
|
// appropriately
|
|
|
|
}
|
2023-04-19 08:28:09 +03:00
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
// rows = append(rows, []string{"output_format", string(opts.Format)})
|
|
|
|
//
|
|
|
|
// rows = append(rows, []string{"output_header", strconv.FormatBool(opts.Header)})
|
|
|
|
// t.SetCellTrans(1, 1, pr.Bool.SprintFunc())
|
|
|
|
//
|
|
|
|
// rows = append(rows, []string{"ping_timeout", fmt.Sprintf("%v", opts.PingTimeout)})
|
|
|
|
// t.SetCellTrans(2, 1, pr.Datetime.SprintFunc())
|
|
|
|
//
|
|
|
|
// rows = append(rows, []string{"shell_completion_timeout", fmt.Sprintf("%v", opts.ShellCompletionTimeout)})
|
|
|
|
// t.SetCellTrans(3, 1, pr.Datetime.SprintFunc())
|
2023-04-19 08:28:09 +03:00
|
|
|
|
|
|
|
w.tbl.appendRowsAndRenderAll(rows)
|
|
|
|
return nil
|
|
|
|
}
|