Skip to content

Commit

Permalink
feat(cmd/influx): add profile management
Browse files Browse the repository at this point in the history
  • Loading branch information
kelwang committed Mar 12, 2020
1 parent 60c5825 commit c511f06
Show file tree
Hide file tree
Showing 21 changed files with 1,007 additions and 68 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
1. [17114](https://github.com/influxdata/influxdb/pull/17114): Allow for retention to be provided to influx setup command as a duration
1. [17138](https://github.com/influxdata/influxdb/pull/17138): Extend pkger export all capabilities to support filtering by lable name and resource type
1. [17049](https://github.com/influxdata/influxdb/pull/17049): Added new login and sign-up screen that for cloud users that allows direct login from their region
1. [17170](https://github.com/influxdata/influxdb/pull/17170): Added new cli multiple profiles management tool

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion cmd/influx/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func authCreateCmd() *cobra.Command {
}

func authorizationCreateF(cmd *cobra.Command, args []string) error {
if err := authCreateFlags.org.validOrgFlags(); err != nil {
if err := authCreateFlags.org.validOrgFlags(&flags); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/influx/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func init() {

func newBackupService() (influxdb.BackupService, error) {
return &http.BackupService{
Addr: flags.host,
Token: flags.token,
Addr: flags.Host,
Token: flags.Token,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/influx/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (b *cmdBucketBuilder) cmdCreate() *cobra.Command {
}

func (b *cmdBucketBuilder) cmdCreateRunEFn(*cobra.Command, []string) error {
if err := b.org.validOrgFlags(); err != nil {
if err := b.org.validOrgFlags(b.globalFlags); err != nil {
return err
}

Expand Down Expand Up @@ -183,7 +183,7 @@ func (b *cmdBucketBuilder) cmdFind() *cobra.Command {
}

func (b *cmdBucketBuilder) cmdFindRunEFn(cmd *cobra.Command, args []string) error {
if err := b.org.validOrgFlags(); err != nil {
if err := b.org.validOrgFlags(b.globalFlags); err != nil {
return err
}

Expand Down
265 changes: 265 additions & 0 deletions cmd/influx/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package main

import (
"fmt"

"github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/cmd/influx/config"
"github.com/spf13/cobra"
)

func cmdConfig(f *globalFlags, opt genericCLIOpts) *cobra.Command {
path, dir, err := defaultConfigPath()
if err != nil {
panic(err)
}
builder := cmdConfigBuilder{
genericCLIOpts: opt,
globalFlags: f,
svc: config.LocalConfigsSVC{
Path: path,
Dir: dir,
},
}
builder.globalFlags = f
return builder.cmd()
}

type cmdConfigBuilder struct {
genericCLIOpts
*globalFlags

name string
url string
token string
active bool
org string

svc config.ConfigsService
}

func (b *cmdConfigBuilder) cmd() *cobra.Command {
cmd := b.newCmd("config", nil)
cmd.Short = "Config management commands"
cmd.Run = seeHelp
cmd.AddCommand(
b.cmdCreate(),
b.cmdDelete(),
b.cmdUpdate(),
b.cmdList(),
)
return cmd
}

func (b *cmdConfigBuilder) cmdCreate() *cobra.Command {
cmd := b.newCmd("create", nil)
cmd.RunE = b.cmdCreateRunEFn
cmd.Short = "Create config"
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)")
cmd.MarkFlagRequired("token")
cmd.Flags().StringVarP(&b.url, "url", "u", "", "The config url (required)")
cmd.MarkFlagRequired("url")

cmd.Flags().BoolVarP(&b.active, "active", "a", false, "Set it to be the active config")
cmd.Flags().StringVarP(&b.org, "org", "o", "", "The optional organization name")
return cmd
}

func (b *cmdConfigBuilder) cmdCreateRunEFn(*cobra.Command, []string) error {
pp, err := b.svc.ParseConfigs()
if err != nil {
return err
}
p := config.Config{
Host: b.url,
Token: b.token,
Org: b.org,
Active: b.active,
}
if _, ok := pp[b.name]; ok {
return &influxdb.Error{
Code: influxdb.EConflict,
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,
})
w.Flush()
return nil
}

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

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

return cmd
}

func (b *cmdConfigBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) error {
pp, err := b.svc.ParseConfigs()
if err != nil {
return err
}
p, ok := pp[b.name]
if !ok {
return &influxdb.Error{
Code: influxdb.ENotFound,
Msg: fmt.Sprintf("name %q is not found", b.name),
}
}
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,
})
w.Flush()
return nil
}

func (b *cmdConfigBuilder) cmdUpdate() *cobra.Command {
cmd := b.newCmd("set", b.cmdUpdateRunEFn)
cmd.Aliases = []string{"update"}
cmd.RunE = b.cmdUpdateRunEFn
cmd.Short = "Update config"
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)")
cmd.Flags().StringVarP(&b.url, "url", "u", "", "The config url (required)")
cmd.Flags().BoolVarP(&b.active, "active", "a", false, "Set it to be the active config")
cmd.Flags().StringVarP(&b.org, "org", "o", "", "The optional organization name")
return cmd
}

func (b *cmdConfigBuilder) cmdUpdateRunEFn(*cobra.Command, []string) error {
pp, err := b.svc.ParseConfigs()
if err != nil {
return err
}
p0, ok := pp[b.name]
if !ok {
return &influxdb.Error{
Code: influxdb.ENotFound,
Msg: fmt.Sprintf("name %q is not found", b.name),
}
}
if b.token != "" {
p0.Token = b.token
}
if b.url != "" {
p0.Host = b.url
}
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,
})
w.Flush()
return nil
}

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

func (b *cmdConfigBuilder) cmdListRunEFn(*cobra.Command, []string) error {
pp, err := b.svc.ParseConfigs()
if err != nil {
return err
}
w := b.newTabWriter()
w.WriteHeaders(
"Active",
"Name",
"URL",
"Org",
)
for n, p := range pp {
var active string
if p.Active {
active = "*"
}
w.Write(map[string]interface{}{
"Active": active,
"Name": n,
"URL": p.Host,
"Org": p.Org,
})
}

w.Flush()
return nil
}
Loading

0 comments on commit c511f06

Please sign in to comment.