refactor: expose generated code and client business logic to share with Kapacitor (#103)
* refactor: take clients out of internal * refactor: move stdio to pkg * Move internal/api to api * refactor: final changes for Kapacitor to access shared functionality * chore: regenerate mocks * fix: bad automated refactor * chore: extra formatting not caught by make fmt
This commit is contained in:
42
clients/cli.go
Normal file
42
clients/cli.go
Normal file
@ -0,0 +1,42 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/internal/config"
|
||||
"github.com/influxdata/influx-cli/v2/internal/tabwriter"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/stdio"
|
||||
)
|
||||
|
||||
const MinPasswordLen = 8
|
||||
|
||||
// CLI is a container for common functionality used to execute commands.
|
||||
type CLI struct {
|
||||
StdIO stdio.StdIO
|
||||
|
||||
HideTableHeaders bool
|
||||
PrintAsJSON bool
|
||||
|
||||
ActiveConfig config.Config
|
||||
ConfigService config.Service
|
||||
}
|
||||
|
||||
func (c *CLI) PrintJSON(v interface{}) error {
|
||||
enc := json.NewEncoder(c.StdIO)
|
||||
enc.SetIndent("", "\t")
|
||||
return enc.Encode(v)
|
||||
}
|
||||
|
||||
func (c *CLI) PrintTable(headers []string, rows ...map[string]interface{}) error {
|
||||
w := tabwriter.NewTabWriter(c.StdIO, c.HideTableHeaders)
|
||||
defer w.Flush()
|
||||
if err := w.WriteHeaders(headers...); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, r := range rows {
|
||||
if err := w.Write(r); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user