Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(influx): add support for json out and hide headers to additional influx commands #17466

Merged
merged 4 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 96 additions & 64 deletions cmd/influx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type cmdConfigBuilder struct {
active bool
org string

json bool
hideHeaders bool

svc config.ConfigsService
}

Expand All @@ -54,6 +57,8 @@ func (b *cmdConfigBuilder) cmd() *cobra.Command {
func (b *cmdConfigBuilder) cmdCreate() *cobra.Command {
cmd := b.newCmd("create", b.cmdCreateRunEFn, false)
cmd.Short = "Create config"

b.registerPrintFlags(cmd)
cmd.Flags().StringVarP(&b.name, "name", "n", "", "The config name (required)")
cmd.MarkFlagRequired("name")
cmd.Flags().StringVarP(&b.token, "token", "t", "", "The config token (required)")
Expand All @@ -71,6 +76,7 @@ func (b *cmdConfigBuilder) cmdCreateRunEFn(*cobra.Command, []string) error {
if err != nil {
return err
}

p := config.Config{
Host: b.url,
Token: b.token,
Expand All @@ -83,41 +89,31 @@ func (b *cmdConfigBuilder) cmdCreateRunEFn(*cobra.Command, []string) error {
Msg: fmt.Sprintf("name %q already exists", b.name),
}
}

pp[b.name] = p
active := ""
if p.Active {
active = "*"
if err := pp.Switch(b.name); err != nil {
return err
}
}

if err = b.svc.WriteConfigs(pp); err != nil {
return err
}
w := b.newTabWriter()
w.WriteHeaders(
"Active",
"Name",
"URL",
"Org",
"Created",
)

w.Write(map[string]interface{}{
"Active": active,
"Name": b.name,
"URL": p.Host,
"Org": p.Org,
"Created": true,
return b.printConfigs(configPrintOpts{
config: cfg{
name: b.name,
Config: p,
},
})
w.Flush()
return nil
}

func (b *cmdConfigBuilder) cmdDelete() *cobra.Command {
cmd := b.newCmd("delete", b.cmdDeleteRunEFn, false)
cmd.Short = "Delete config"

b.registerPrintFlags(cmd)
cmd.Flags().StringVarP(&b.name, "name", "n", "", "The config name (required)")
cmd.MarkFlagRequired("name")

Expand All @@ -129,6 +125,7 @@ func (b *cmdConfigBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) er
if err != nil {
return err
}

p, ok := pp[b.name]
if !ok {
return &influxdb.Error{
Expand All @@ -137,31 +134,26 @@ func (b *cmdConfigBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) er
}
}
delete(pp, b.name)

if err = b.svc.WriteConfigs(pp); err != nil {
return err
}
w := b.newTabWriter()
w.WriteHeaders(
"Name",
"URL",
"Org",
"Deleted",
)

w.Write(map[string]interface{}{
"Name": b.name,
"URL": p.Host,
"Org": p.Org,
"Deleted": true,
return b.printConfigs(configPrintOpts{
delete: true,
config: cfg{
name: b.name,
Config: p,
},
})
w.Flush()
return nil
}

func (b *cmdConfigBuilder) cmdUpdate() *cobra.Command {
cmd := b.newCmd("set", b.cmdUpdateRunEFn, false)
cmd.Aliases = []string{"update"}
cmd.Short = "Update config"

b.registerPrintFlags(cmd)
cmd.Flags().StringVarP(&b.name, "name", "n", "", "The config name (required)")
cmd.MarkFlagRequired("name")

Expand All @@ -177,6 +169,7 @@ func (b *cmdConfigBuilder) cmdUpdateRunEFn(*cobra.Command, []string) error {
if err != nil {
return err
}

p0, ok := pp[b.name]
if !ok {
return &influxdb.Error{
Expand All @@ -193,41 +186,31 @@ func (b *cmdConfigBuilder) cmdUpdateRunEFn(*cobra.Command, []string) error {
if b.org != "" {
p0.Org = b.org
}

pp[b.name] = p0
active := ""
if b.active {
active = "*"
if err := pp.Switch(b.name); err != nil {
return err
}
}

if err = b.svc.WriteConfigs(pp); err != nil {
return err
}
w := b.newTabWriter()
w.WriteHeaders(
"Active",
"Name",
"URL",
"Org",
"Updated",
)

w.Write(map[string]interface{}{
"Active": active,
"Name": b.name,
"URL": p0.Host,
"Org": p0.Org,
"Updated": true,
return b.printConfigs(configPrintOpts{
config: cfg{
name: b.name,
Config: p0,
},
})
w.Flush()
return nil
}

func (b *cmdConfigBuilder) cmdList() *cobra.Command {
cmd := b.newCmd("list", b.cmdListRunEFn, false)
cmd.Aliases = []string{"ls"}
cmd.Short = "List configs"
b.registerPrintFlags(cmd)
return cmd
}

Expand All @@ -236,26 +219,75 @@ func (b *cmdConfigBuilder) cmdListRunEFn(*cobra.Command, []string) error {
if err != nil {
return err
}
w := b.newTabWriter()
w.WriteHeaders(
"Active",
"Name",
"URL",
"Org",
)

var cfgs []cfg
for n, p := range pp {
cfgs = append(cfgs, cfg{
name: n,
Config: p,
})
}

return b.printConfigs(configPrintOpts{configs: cfgs})
}

func (b *cmdConfigBuilder) registerPrintFlags(cmd *cobra.Command) {
registerPrintOptions(cmd, &b.hideHeaders, &b.json)
}

func (b *cmdConfigBuilder) printConfigs(opts configPrintOpts) error {
if b.json {
var v interface{} = opts.configs
if opts.configs == nil {
v = opts.config
}
return b.writeJSON(v)
}

w := b.newTabWriter()
defer w.Flush()

w.HideHeaders(b.hideHeaders)

headers := []string{"Active", "Name", "URL", "Org"}
if opts.delete {
headers = append(headers, "Deleted")
}
w.WriteHeaders(headers...)

if opts.configs == nil {
opts.configs = append(opts.configs, opts.config)
}
for _, c := range opts.configs {
var active string
if p.Active {
if c.Active {
active = "*"
}
w.Write(map[string]interface{}{
m := map[string]interface{}{
"Active": active,
"Name": n,
"URL": p.Host,
"Org": p.Org,
})
"Name": c.name,
"URL": c.Host,
"Org": c.Org,
}
if opts.delete {
m["Deleted"] = true
}

w.Write(m)
}

w.Flush()
return nil
}

type (
configPrintOpts struct {
delete bool
config cfg
configs []cfg
}

cfg struct {
name string
config.Config
}
)
8 changes: 4 additions & 4 deletions cmd/influx/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

// Config store the crendentials of influxdb host and token.
type Config struct {
Host string `toml:"url"`
Host string `toml:"url" json:"url"`
// Token is base64 encoded sequence.
Token string `toml:"token"`
Org string `toml:"org,omitempty"`
Active bool `toml:"active,omitempty"`
Token string `toml:"token" json:"token"`
Org string `toml:"org" json:"org"`
Active bool `toml:"active" json:"active"`
}

// DefaultConfig is default config without token
Expand Down
Loading