From aa95c51e7fce7e862832e48f21829c140ac9e9b7 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 4 Mar 2019 14:42:28 +0800 Subject: [PATCH] refactor(error): update error Signed-off-by: Bo-Yi Wu --- plugin.go | 21 +++++++++++---------- plugin_test.go | 6 +++--- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/plugin.go b/plugin.go index e194772..9f0ef26 100644 --- a/plugin.go +++ b/plugin.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "io" "os" @@ -12,11 +13,11 @@ import ( "github.com/appleboy/easyssh-proxy" ) -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" +var ( + missingHost = errors.New("Error: missing server host") + missingPasswordOrKey = errors.New("Error: can't connect without a private SSH key or password") + commandTimeOut = errors.New("Error: command timeout") + setPasswordandKey = errors.New("can't set password and key at the same time") ) type ( @@ -117,7 +118,7 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) { // command time out if !isTimeout { - errChannel <- fmt.Errorf(commandTimeOut) + errChannel <- commandTimeOut } } @@ -137,16 +138,16 @@ func (p Plugin) log(host string, message ...interface{}) { // Exec executes the plugin. func (p Plugin) Exec() error { - if len(p.Config.Host) == 0 && len(p.Config.UserName) == 0 { - return fmt.Errorf(missingHostOrUser) + if len(p.Config.Host) == 0 { + return missingHost } if len(p.Config.Key) == 0 && len(p.Config.Password) == 0 && len(p.Config.KeyPath) == 0 { - return fmt.Errorf(missingPasswordOrKey) + return missingPasswordOrKey } if len(p.Config.Key) != 0 && len(p.Config.Password) != 0 { - return fmt.Errorf(setPasswordandKey) + return setPasswordandKey } wg := sync.WaitGroup{} diff --git a/plugin_test.go b/plugin_test.go index 3bb1cbf..fcb0738 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -16,7 +16,7 @@ func TestMissingHostOrUser(t *testing.T) { err := plugin.Exec() assert.NotNil(t, err) - assert.Equal(t, missingHostOrUser, err.Error()) + assert.Equal(t, missingHost, err) } func TestMissingKeyOrPassword(t *testing.T) { @@ -31,7 +31,7 @@ func TestMissingKeyOrPassword(t *testing.T) { err := plugin.Exec() assert.NotNil(t, err) - assert.Equal(t, missingPasswordOrKey, err.Error()) + assert.Equal(t, missingPasswordOrKey, err) } func TestSetPasswordAndKey(t *testing.T) { @@ -48,7 +48,7 @@ func TestSetPasswordAndKey(t *testing.T) { err := plugin.Exec() assert.NotNil(t, err) - assert.Equal(t, setPasswordandKey, err.Error()) + assert.Equal(t, setPasswordandKey, err) } func TestIncorrectPassword(t *testing.T) {