refactor(error): update error

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2019-03-04 14:42:28 +08:00
parent 86a962988d
commit aa95c51e7f
2 changed files with 14 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -12,11 +13,11 @@ import (
"github.com/appleboy/easyssh-proxy" "github.com/appleboy/easyssh-proxy"
) )
const ( var (
missingHostOrUser = "Error: missing server host or user" missingHost = errors.New("Error: missing server host")
missingPasswordOrKey = "Error: can't connect without a private SSH key or password" missingPasswordOrKey = errors.New("Error: can't connect without a private SSH key or password")
commandTimeOut = "Error: command timeout" commandTimeOut = errors.New("Error: command timeout")
setPasswordandKey = "can't set password and key at the same time" setPasswordandKey = errors.New("can't set password and key at the same time")
) )
type ( type (
@ -117,7 +118,7 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
// command time out // command time out
if !isTimeout { if !isTimeout {
errChannel <- fmt.Errorf(commandTimeOut) errChannel <- commandTimeOut
} }
} }
@ -137,16 +138,16 @@ func (p Plugin) log(host string, message ...interface{}) {
// Exec executes the plugin. // Exec executes the plugin.
func (p Plugin) Exec() error { func (p Plugin) Exec() error {
if len(p.Config.Host) == 0 && len(p.Config.UserName) == 0 { if len(p.Config.Host) == 0 {
return fmt.Errorf(missingHostOrUser) return missingHost
} }
if len(p.Config.Key) == 0 && len(p.Config.Password) == 0 && len(p.Config.KeyPath) == 0 { 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 { if len(p.Config.Key) != 0 && len(p.Config.Password) != 0 {
return fmt.Errorf(setPasswordandKey) return setPasswordandKey
} }
wg := sync.WaitGroup{} wg := sync.WaitGroup{}

View File

@ -16,7 +16,7 @@ func TestMissingHostOrUser(t *testing.T) {
err := plugin.Exec() err := plugin.Exec()
assert.NotNil(t, err) assert.NotNil(t, err)
assert.Equal(t, missingHostOrUser, err.Error()) assert.Equal(t, missingHost, err)
} }
func TestMissingKeyOrPassword(t *testing.T) { func TestMissingKeyOrPassword(t *testing.T) {
@ -31,7 +31,7 @@ func TestMissingKeyOrPassword(t *testing.T) {
err := plugin.Exec() err := plugin.Exec()
assert.NotNil(t, err) assert.NotNil(t, err)
assert.Equal(t, missingPasswordOrKey, err.Error()) assert.Equal(t, missingPasswordOrKey, err)
} }
func TestSetPasswordAndKey(t *testing.T) { func TestSetPasswordAndKey(t *testing.T) {
@ -48,7 +48,7 @@ func TestSetPasswordAndKey(t *testing.T) {
err := plugin.Exec() err := plugin.Exec()
assert.NotNil(t, err) assert.NotNil(t, err)
assert.Equal(t, setPasswordandKey, err.Error()) assert.Equal(t, setPasswordandKey, err)
} }
func TestIncorrectPassword(t *testing.T) { func TestIncorrectPassword(t *testing.T) {