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): enable dynamic configs destination #19030

Merged
merged 1 commit into from
Jul 23, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
1. [18910](https://github.com/influxdata/influxdb/pull/18910): Add uninstall functionality for stacks
1. [18912](https://github.com/influxdata/influxdb/pull/18912): Drop deprecated influx pkg command tree
1. [18997](https://github.com/influxdata/influxdb/pull/18997): Add telegraf management commands to influx CLI
1. [19030](https://github.com/influxdata/influxdb/pull/19030): Enable dynamic destination for the influx CLI configs file

### Bug Fixes

Expand Down
43 changes: 31 additions & 12 deletions cmd/influx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ package main
import (
"errors"
"net/url"
"path/filepath"

"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/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,
svc: config.NewLocalConfigSVC(path, dir),
globalFlags: f,
svcFn: newConfigService,
}
return builder.cmd()
}

type cmdConfigBuilder struct {
genericCLIOpts
*globalFlags

name string
url string
Expand All @@ -33,7 +32,7 @@ type cmdConfigBuilder struct {
json bool
hideHeaders bool

svc config.ConfigsService
svcFn func(path string) config.Service
}

func (b *cmdConfigBuilder) cmd() *cobra.Command {
Expand Down Expand Up @@ -65,6 +64,7 @@ func (b *cmdConfigBuilder) cmd() *cobra.Command {
https://v2.docs.influxdata.com/v2.0/reference/cli/influx/config
`

b.registerFilepath(cmd)
cmd.AddCommand(
b.cmdCreate(),
b.cmdDelete(),
Expand All @@ -75,8 +75,10 @@ func (b *cmdConfigBuilder) cmd() *cobra.Command {
}

func (b *cmdConfigBuilder) cmdSwitchActiveRunEFn(cmd *cobra.Command, args []string) error {
svc := b.newConfigSVC()

if len(args) > 0 {
cfg, err := b.svc.SwitchActive(args[0])
cfg, err := svc.SwitchActive(args[0])
if err != nil {
return err
}
Expand All @@ -86,7 +88,7 @@ func (b *cmdConfigBuilder) cmdSwitchActiveRunEFn(cmd *cobra.Command, args []stri
})
}

configs, err := b.svc.ListConfigs()
configs, err := svc.ListConfigs()
if err != nil {
return err
}
Expand Down Expand Up @@ -126,6 +128,7 @@ func (b *cmdConfigBuilder) cmdCreate() *cobra.Command {
and
https://v2.docs.influxdata.com/v2.0/reference/cli/influx/config/create`

b.registerFilepath(cmd)
b.registerPrintFlags(cmd)
b.registerConfigSettingFlags(cmd)
cmd.MarkFlagRequired("token")
Expand All @@ -134,12 +137,14 @@ func (b *cmdConfigBuilder) cmdCreate() *cobra.Command {
}

func (b *cmdConfigBuilder) cmdCreateRunEFn(*cobra.Command, []string) error {
svc := b.newConfigSVC()

host, err := b.getValidHostURL()
if err != nil {
return err
}

cfg, err := b.svc.CreateConfig(config.Config{
cfg, err := svc.CreateConfig(config.Config{
Name: b.name,
Host: host,
Token: b.token,
Expand Down Expand Up @@ -184,13 +189,15 @@ func (b *cmdConfigBuilder) cmdDelete() *cobra.Command {
}

func (b *cmdConfigBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) error {
svc := b.newConfigSVC()

deletedConfigs := make(config.Configs)
for _, name := range append(args, b.name) {
if name == "" {
continue
}

cfg, err := b.svc.DeleteConfig(name)
cfg, err := svc.DeleteConfig(name)
if influxdb.ErrorCode(err) == influxdb.ENotFound {
continue
}
Expand Down Expand Up @@ -241,7 +248,7 @@ func (b *cmdConfigBuilder) cmdUpdateRunEFn(*cobra.Command, []string) error {
host = h
}

cfg, err := b.svc.UpdateConfig(config.Config{
cfg, err := b.newConfigSVC().UpdateConfig(config.Config{
Name: b.name,
Host: host,
Token: b.token,
Expand Down Expand Up @@ -283,7 +290,7 @@ func (b *cmdConfigBuilder) cmdList() *cobra.Command {
}

func (b *cmdConfigBuilder) cmdListRunEFn(*cobra.Command, []string) error {
cfgs, err := b.svc.ListConfigs()
cfgs, err := b.newConfigSVC().ListConfigs()
if err != nil {
return err
}
Expand All @@ -309,6 +316,10 @@ func (b *cmdConfigBuilder) registerConfigSettingFlags(cmd *cobra.Command) {
cmd.Flags().MarkDeprecated("url", "use the --host-url flag")
}

func (b *cmdConfigBuilder) registerFilepath(cmd *cobra.Command) {
b.globalFlags.registerFlags(cmd, "host", "token", "skip-verify", "trace-debug-id")
}

func (b *cmdConfigBuilder) registerPrintFlags(cmd *cobra.Command) {
registerPrintOptions(cmd, &b.hideHeaders, &b.json)
}
Expand Down Expand Up @@ -370,6 +381,14 @@ func (b *cmdConfigBuilder) getValidHostURL() (string, error) {
return u.String(), nil
}

func (b *cmdConfigBuilder) newConfigSVC() config.Service {
return b.svcFn(b.globalFlags.filepath)
}

func newConfigService(path string) config.Service {
return config.NewLocalConfigSVC(path, filepath.Dir(path))
}

type configPrintOpts struct {
delete bool
config config.Config
Expand Down
10 changes: 5 additions & 5 deletions cmd/influx/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var DefaultConfig = Config{
// Configs is map of configs indexed by name.
type Configs map[string]Config

// ConfigsService is the service to list and write configs.
type ConfigsService interface {
// Service is the service to list and write configs.
type Service interface {
CreateConfig(Config) (Config, error)
DeleteConfig(name string) (Config, error)
UpdateConfig(Config) (Config, error)
Expand Down Expand Up @@ -83,10 +83,10 @@ func newConfigsSVC(s store) localConfigsSVC {
}

// NewLocalConfigSVC create a new local config svc.
func NewLocalConfigSVC(Path, Dir string) ConfigsService {
func NewLocalConfigSVC(path, dir string) Service {
return newConfigsSVC(ioStore{
Path: Path,
Dir: Dir,
Path: path,
Dir: dir,
})
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/influx/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ func TestConfigDelete(t *testing.T) {
}
}

func newBufferSVC() (ConfigsService, *bytesStore) {
func newBufferSVC() (Service, *bytesStore) {
store := new(bytesStore)
return newConfigsSVC(store), store
}
Expand Down
Loading