rework if/else to simplify main code body.

This commit is contained in:
Megan Marsh 2018-11-12 09:46:02 -08:00
parent ebc7130d77
commit 89184ae384
1 changed files with 42 additions and 37 deletions

View File

@ -54,48 +54,53 @@ func (c *DriverConfig) Prepare(ctx *interpolate.Context) []error {
} }
func (c *DriverConfig) Validate(SkipExport bool) error { func (c *DriverConfig) Validate(SkipExport bool) error {
if c.RemoteType == "esx5" && SkipExport != true { if c.RemoteType == "" || SkipExport == true {
if c.RemotePassword == "" { return nil
return fmt.Errorf("exporting the vm (with ovftool) requires that " + }
"you set a value for remote_password") if c.RemotePassword == "" {
} else if !c.SkipValidateCredentials { return fmt.Errorf("exporting the vm (with ovftool) requires that " +
// check that password is valid by sending a dummy ovftool command "you set a value for remote_password")
// now, so that we don't fail for a simple mistake after a long }
// build if c.SkipValidateCredentials {
ovftool := GetOVFTool() return nil
ovfToolArgs := []string{"--verifyOnly", fmt.Sprintf("vi://" + }
url.QueryEscape(c.RemoteUser) + ":" +
url.QueryEscape(c.RemotePassword) + "@" +
c.RemoteHost)}
var out bytes.Buffer // check that password is valid by sending a dummy ovftool command
cmdCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second) // now, so that we don't fail for a simple mistake after a long
defer cancel() // build
cmd := exec.CommandContext(cmdCtx, ovftool, ovfToolArgs...) ovftool := GetOVFTool()
cmd.Stdout = &out ovfToolArgs := []string{"--verifyOnly", fmt.Sprintf("vi://" +
url.QueryEscape(c.RemoteUser) + ":" +
url.QueryEscape(c.RemotePassword) + "@" +
c.RemoteHost)}
// Need to manually close stdin or else the ofvtool call will hang var out bytes.Buffer
// forever in a situation where the user has provided an invalid cmdCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
// password or username defer cancel()
stdin, _ := cmd.StdinPipe() cmd := exec.CommandContext(cmdCtx, ovftool, ovfToolArgs...)
defer stdin.Close() cmd.Stdout = &out
if err := cmd.Run(); err != nil { // Need to manually close stdin or else the ofvtool call will hang
outString := out.String() // forever in a situation where the user has provided an invalid
// The command *should* fail with this error, if it // password or username
// authenticates properly. stdin, _ := cmd.StdinPipe()
if !strings.Contains(outString, "Found wrong kind of object") { defer stdin.Close()
err := fmt.Errorf("ovftool validation error: %s; %s",
err, outString) if err := cmd.Run(); err != nil {
if strings.Contains(outString, outString := out.String()
"Enter login information for source") { // The command *should* fail with this error, if it
err = fmt.Errorf("The username or password you " + // authenticates properly.
"provided to ovftool is invalid.") if !strings.Contains(outString, "Found wrong kind of object") {
} err := fmt.Errorf("ovftool validation error: %s; %s",
return err err, outString)
} if strings.Contains(outString,
"Enter login information for source") {
err = fmt.Errorf("The username or password you " +
"provided to ovftool is invalid.")
} }
return err
} }
} }
return nil return nil
} }