feat: check missing host or user.

This commit is contained in:
Bo-Yi Wu 2017-01-23 14:24:27 +08:00
parent 18d0ff3a20
commit c46dd8eadf
2 changed files with 20 additions and 1 deletions

View File

@ -12,6 +12,11 @@ import (
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
const (
missingHostOrUser = "Error: missing server host or user"
missingPasswordOrKey = "Error: can't connect without a private SSH key or password"
)
type ( type (
// Config for the plugin. // Config for the plugin.
Config struct { Config struct {
@ -33,8 +38,12 @@ type (
// Exec executes the plugin. // Exec executes the plugin.
func (p Plugin) Exec() error { func (p Plugin) Exec() error {
if len(p.Config.Host) == 0 && p.Config.User == "" {
return fmt.Errorf(missingHostOrUser)
}
if p.Config.Key == "" && p.Config.Password == "" { if p.Config.Key == "" && p.Config.Password == "" {
return fmt.Errorf("Error: can't connect without a private SSH key or password") return fmt.Errorf(missingPasswordOrKey)
} }
for i, host := range p.Config.Host { for i, host := range p.Config.Host {

View File

@ -6,6 +6,15 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestMissingHostOrUser(t *testing.T) {
plugin := Plugin{}
err := plugin.Exec()
assert.NotNil(t, err)
assert.Equal(t, missingHostOrUser, err.Error())
}
func TestMissingKeyOrPassword(t *testing.T) { func TestMissingKeyOrPassword(t *testing.T) {
plugin := Plugin{ plugin := Plugin{
Config{ Config{
@ -17,4 +26,5 @@ 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())
} }