From b63f275e9e787001b79b792a9864c17d717cf452 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 23 Apr 2017 11:49:40 +0800 Subject: [PATCH] refactor: show errors if set password and key at the same time (#72) --- plugin.go | 9 +++++++-- plugin_test.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/plugin.go b/plugin.go index ed09a01..0718065 100644 --- a/plugin.go +++ b/plugin.go @@ -17,6 +17,7 @@ const ( missingHostOrUser = "Error: missing server host or user" missingPasswordOrKey = "Error: can't connect without a private SSH key or password" commandTimeOut = "Error: command timeout" + setPasswordandKey = "can't set password and key at the same time" ) type ( @@ -46,14 +47,18 @@ func (p Plugin) log(host string, message ...interface{}) { // Exec executes the plugin. func (p Plugin) Exec() error { - if len(p.Config.Host) == 0 && p.Config.UserName == "" { + if len(p.Config.Host) == 0 && len(p.Config.UserName) == 0 { return fmt.Errorf(missingHostOrUser) } - if p.Config.Key == "" && p.Config.Password == "" && p.Config.KeyPath == "" { + if len(p.Config.Key) == 0 && len(p.Config.Password) == 0 && len(p.Config.KeyPath) == 0 { return fmt.Errorf(missingPasswordOrKey) } + if len(p.Config.Key) != 0 && len(p.Config.Password) != 0 { + return fmt.Errorf(setPasswordandKey) + } + wg.Add(len(p.Config.Host)) errChannel := make(chan error, 1) finished := make(chan bool, 1) diff --git a/plugin_test.go b/plugin_test.go index 7ffe617..8372f36 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -30,6 +30,22 @@ func TestMissingKeyOrPassword(t *testing.T) { assert.Equal(t, missingPasswordOrKey, err.Error()) } +func TestSetPasswordAndKey(t *testing.T) { + plugin := Plugin{ + Config{ + Host: []string{"localhost"}, + UserName: "ubuntu", + Password: "1234", + Key: "1234", + }, + } + + err := plugin.Exec() + + assert.NotNil(t, err) + assert.Equal(t, setPasswordandKey, err.Error()) +} + func TestIncorrectPassword(t *testing.T) { plugin := Plugin{ Config: Config{