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

ecctl: Prefix config parsing error #254

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 14 additions & 17 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ third-party software developed by the licenses listed below.

=========================================================================

github.com/davecgh/go-spew 0BSD
github.com/elastic/cloud-sdk-go Apache-2.0
github.com/go-openapi/runtime Apache-2.0
github.com/go-openapi/strfmt Apache-2.0
github.com/marclop/elasticsearch-cli Apache-2.0
github.com/spf13/cobra Apache-2.0
github.com/pkg/errors BSD-2-Clause
github.com/pmezard/go-difflib BSD-3-Clause
github.com/spf13/pflag BSD-3-Clause
golang.org/x/crypto BSD-3-Clause
github.com/asaskevich/govalidator MIT
github.com/blang/semver MIT
github.com/chzyer/test MIT
github.com/spf13/viper MIT
github.com/hashicorp/go-multierror MPL-2.0
github.com/chzyer/logex no license file was found
github.com/ghodss/yaml no license file was found
github.com/davecgh/go-spew 0BSD
github.com/elastic/cloud-sdk-go Apache-2.0
github.com/go-openapi/runtime Apache-2.0
github.com/go-openapi/strfmt Apache-2.0
github.com/spf13/cobra Apache-2.0
github.com/pkg/errors BSD-2-Clause
github.com/pmezard/go-difflib BSD-3-Clause
github.com/spf13/pflag BSD-3-Clause
golang.org/x/crypto BSD-3-Clause
github.com/asaskevich/govalidator MIT
github.com/blang/semver MIT
github.com/spf13/viper MIT
github.com/hashicorp/go-multierror MPL-2.0
github.com/ghodss/yaml no license file was found

=========================================================================
20 changes: 15 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"path/filepath"
"time"

"github.com/elastic/cloud-sdk-go/pkg/multierror"
"github.com/elastic/cloud-sdk-go/pkg/output"
multierror "github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -78,7 +78,16 @@ var RootCmd = &cobra.Command{
return err
}

return initApp(cmd, defaultClient, defaultViper)
err := initApp(cmd, defaultClient, defaultViper)
// When no config file has been read and initApp returns an error, tell
// the user how to initialize the application.
if err != nil && defaultViper.ConfigFileUsed() == "" {
return multierror.NewPrefixed(
`missing ecctl config file, please use "ecctl init" to initialize it`, err,
marclop marked this conversation as resolved.
Show resolved Hide resolved
)
}

return err
},
}

Expand Down Expand Up @@ -187,15 +196,16 @@ func initApp(cmd *cobra.Command, client *http.Client, v *viper.Viper) error {
}

func checkPreRunE(command *cobra.Command) error {
var err = new(multierror.Error)
var err = multierror.NewPrefixed("")
for _, c := range command.Commands() {
if command.HasSubCommands() {
err = multierror.Append(err, checkPreRunE(c))
err = err.Append(checkPreRunE(c))
}
if c.PreRunE == nil {
var message = fmt.Sprintf(messageErrHasNoPreRunCheck, command.Name(), c.Name())
err = multierror.Append(err, errors.New(message))
err = err.Append(errors.New(message))
}
}

return err.ErrorOrNil()
}
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ go 1.12
require (
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a
github.com/blang/semver v3.5.1+incompatible
github.com/chzyer/logex v1.1.10 // indirect
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/elastic/cloud-sdk-go v1.0.0-beta2.0.20200325215536-c64d182f57e4
github.com/elastic/cloud-sdk-go v1.0.0-beta2.0.20200407011203-0fb44caf896d
github.com/ghodss/yaml v1.0.0
github.com/go-openapi/runtime v0.19.15
github.com/go-openapi/strfmt v0.19.5
github.com/hashicorp/go-multierror v1.0.0
github.com/marclop/elasticsearch-cli v0.0.0-20190212133917-c1d1bf9d46e4
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
github.com/spf13/cobra v0.0.7
Expand Down
54 changes: 2 additions & 52 deletions go.sum

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions pkg/ecctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"net/http"
"time"

"github.com/elastic/cloud-sdk-go/pkg/multierror"
"github.com/elastic/cloud-sdk-go/pkg/output"
"github.com/elastic/cloud-sdk-go/pkg/util/slice"
multierror "github.com/hashicorp/go-multierror"
)

const (
Expand Down Expand Up @@ -64,6 +64,7 @@ type Config struct {
Verbose bool `json:"verbose,omitempty"`
Force bool `json:"force,omitempty"`
Insecure bool `json:"insecure,omitempty"`

// SkipLogin skips loging in when user and pass are set.
SkipLogin bool `json:"-"`

Expand All @@ -73,31 +74,31 @@ type Config struct {

// Validate checks that the application config is a valid one
func (c *Config) Validate() error {
var err = new(multierror.Error)
var err = multierror.NewPrefixed("invalid configuration options specified")
if !slice.HasString([]string{JSONOutput, TextOutput}, c.Output) {
err = multierror.Append(err, errInvalidOutputFormat)
err = err.Append(errInvalidOutputFormat)
}

var allCreds = c.APIKey != "" && (c.User != "" || c.Pass != "")
if allCreds {
err = multierror.Append(err, errInvalidBothAuthenticaitonSettings)
err = err.Append(errInvalidBothAuthenticaitonSettings)
}

var emptyCreds = c.APIKey == "" && (c.User == "" || c.Pass == "")
if emptyCreds {
err = multierror.Append(err, errInvalidEmptyAuthenticaitonSettings)
err = err.Append(errInvalidEmptyAuthenticaitonSettings)
}

if c.Output == JSONOutput && c.Format != "" {
err = multierror.Append(err, errCannotSpecifyJSONOutputAndCustomFormat)
err = err.Append(errCannotSpecifyJSONOutputAndCustomFormat)
}

if c.OutputDevice == nil {
err = multierror.Append(err, errInvalidOutputDevice)
err = err.Append(errInvalidOutputDevice)
}

if c.ErrorDevice == nil {
err = multierror.Append(err, errInvalidErrorDevice)
err = err.Append(errInvalidErrorDevice)
}

return err.ErrorOrNil()
Expand Down
18 changes: 9 additions & 9 deletions pkg/ecctl/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"reflect"
"testing"

"github.com/elastic/cloud-sdk-go/pkg/multierror"
"github.com/elastic/cloud-sdk-go/pkg/output"
multierror "github.com/hashicorp/go-multierror"
)

func TestConfigValidate(t *testing.T) {
Expand Down Expand Up @@ -54,11 +54,11 @@ func TestConfigValidate(t *testing.T) {
Output: "INVALID OUTPUT",
APIKey: "dummy",
},
err: &multierror.Error{Errors: []error{
err: multierror.NewPrefixed("invalid configuration options specified",
errInvalidOutputFormat,
errInvalidOutputDevice,
errInvalidErrorDevice,
}},
),
},
{
name: "Validate fails when output = json and custom format",
Expand All @@ -67,11 +67,11 @@ func TestConfigValidate(t *testing.T) {
Format: "{{ .Field }}",
APIKey: "dummy",
},
err: &multierror.Error{Errors: []error{
err: multierror.NewPrefixed("invalid configuration options specified",
errCannotSpecifyJSONOutputAndCustomFormat,
errInvalidOutputDevice,
errInvalidErrorDevice,
}},
),
},
{
name: "Validate fails due to specifying both user / pass and APIKey",
Expand All @@ -83,10 +83,10 @@ func TestConfigValidate(t *testing.T) {
Pass: "dummypass",
OutputDevice: output.NewDevice(new(bytes.Buffer)),
},
err: &multierror.Error{Errors: []error{
err: multierror.NewPrefixed("invalid configuration options specified",
errInvalidBothAuthenticaitonSettings,
errInvalidErrorDevice,
}},
),
},
{
name: "Validate fails due to empty credentials",
Expand All @@ -95,10 +95,10 @@ func TestConfigValidate(t *testing.T) {
Region: "ece-region",
OutputDevice: output.NewDevice(new(bytes.Buffer)),
},
err: &multierror.Error{Errors: []error{
err: multierror.NewPrefixed("invalid configuration options specified",
errInvalidEmptyAuthenticaitonSettings,
errInvalidErrorDevice,
}},
),
},
{
name: "Validate succeeds",
Expand Down
22 changes: 11 additions & 11 deletions pkg/ecctl/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import (
"strconv"
"strings"

"github.com/elastic/ecctl/pkg/deployment"
"github.com/elastic/ecctl/pkg/user"

"github.com/elastic/cloud-sdk-go/pkg/input"
"github.com/elastic/cloud-sdk-go/pkg/multierror"
"github.com/elastic/cloud-sdk-go/pkg/output"
multierror "github.com/hashicorp/go-multierror"
"github.com/spf13/viper"

"github.com/elastic/ecctl/pkg/deployment"
"github.com/elastic/ecctl/pkg/user"
)

const (
Expand Down Expand Up @@ -234,29 +234,29 @@ type InitConfigParams struct {

// Validate ensures the parameters are usable.
func (params InitConfigParams) Validate() error {
var merr = new(multierror.Error)
var merr = multierror.NewPrefixed("invalid init configuration")
if params.Viper == nil {
merr = multierror.Append(merr, errors.New("init: viper instance cannot be nil"))
merr = merr.Append(errors.New("viper instance cannot be nil"))
}

if params.Reader == nil {
merr = multierror.Append(merr, errors.New("init: input reader cannot be nil"))
merr = merr.Append(errors.New("input reader cannot be nil"))
}

if params.Writer == nil {
merr = multierror.Append(merr, errors.New("init: output writer cannot be nil"))
merr = merr.Append(errors.New("output writer cannot be nil"))
}

if params.ErrWriter == nil {
merr = multierror.Append(merr, errors.New("init: error writer cannot be nil"))
merr = merr.Append(errors.New("error writer cannot be nil"))
}

if params.PasswordReadFunc == nil {
merr = multierror.Append(merr, errors.New("init: password read function cannot be nil"))
merr = merr.Append(errors.New("password read function cannot be nil"))
}

if params.Client == nil {
merr = multierror.Append(merr, errors.New("init: http client cannot be nil"))
merr = merr.Append(errors.New("http client cannot be nil"))
}

return merr.ErrorOrNil()
Expand Down
19 changes: 9 additions & 10 deletions pkg/ecctl/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ import (

"github.com/elastic/cloud-sdk-go/pkg/api/mock"
"github.com/elastic/cloud-sdk-go/pkg/models"
"github.com/elastic/cloud-sdk-go/pkg/multierror"
"github.com/elastic/cloud-sdk-go/pkg/util/ec"
"github.com/spf13/viper"

multierror "github.com/hashicorp/go-multierror"
)

var emptyPassFunc = func(int) ([]byte, error) { return nil, nil }
Expand Down Expand Up @@ -158,14 +157,14 @@ func TestInitConfig(t *testing.T) {
}{
{
name: "fail on parameter validation",
err: &multierror.Error{Errors: []error{
errors.New("init: viper instance cannot be nil"),
errors.New("init: input reader cannot be nil"),
errors.New("init: output writer cannot be nil"),
errors.New("init: error writer cannot be nil"),
errors.New("init: password read function cannot be nil"),
errors.New("init: http client cannot be nil"),
}},
err: multierror.NewPrefixed("invalid init configuration",
errors.New("viper instance cannot be nil"),
errors.New("input reader cannot be nil"),
errors.New("output writer cannot be nil"),
errors.New("error writer cannot be nil"),
errors.New("password read function cannot be nil"),
errors.New("http client cannot be nil"),
),
},
{
name: "doesn't find a config file and user skips the creation of one",
Expand Down