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
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{}

View File

@ -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) {