simplify some code

This commit is contained in:
Matthew Hooker 2017-03-28 18:29:55 -07:00
parent 0149f679de
commit 79287d7e47
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
13 changed files with 27 additions and 47 deletions

View File

@ -66,7 +66,7 @@ func (c *AccessConfig) Config() (*aws.Config, error) {
func (c *AccessConfig) Region() (string, error) {
if c.RawRegion != "" {
if !c.SkipValidation {
if valid := ValidateRegion(c.RawRegion); valid == false {
if valid := ValidateRegion(c.RawRegion); !valid {
return "", fmt.Errorf("Not a valid region: %s", c.RawRegion)
}
}
@ -85,7 +85,7 @@ func (c *AccessConfig) Region() (string, error) {
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error
if c.RawRegion != "" && !c.SkipValidation {
if valid := ValidateRegion(c.RawRegion); valid == false {
if valid := ValidateRegion(c.RawRegion); !valid {
errs = append(errs, fmt.Errorf("Unknown region: %s", c.RawRegion))
}
}

View File

@ -48,7 +48,7 @@ func (c *AMIConfig) Prepare(ctx *interpolate.Context) []error {
if !c.AMISkipRegionValidation {
// Verify the region is real
if valid := ValidateRegion(region); valid == false {
if valid := ValidateRegion(region); !valid {
errs = append(errs, fmt.Errorf("Unknown region: %s", region))
continue
}

View File

@ -90,10 +90,7 @@ func (c *CLIConfig) Prepare(name string) error {
c.SourceProfile = c.ProfileName
}
c.profileCred, err = credsFromName(c.SourceProfile)
if err != nil {
return err
}
return nil
return err
}
func (c *CLIConfig) getSessionName(rawName string) (string, error) {

View File

@ -85,7 +85,7 @@ func (s *StepSourceAMIInfo) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt
}
if len(imageResp.Images) > 1 && s.AmiFilters.MostRecent == false {
if len(imageResp.Images) > 1 && !s.AmiFilters.MostRecent {
err := fmt.Errorf("Your query returned more than one result. Please try a more specific search, or set most_recent to true.")
state.Put("error", err)
ui.Error(err.Error())

View File

@ -50,7 +50,7 @@ func (c *AwsAccessConfig) config(region string) (*aws.Config, error) {
// or an error.
func (c *AwsAccessConfig) EcrGetLogin(ecrUrl string) (string, string, error) {
exp := regexp.MustCompile("(?:http://|https://|)([0-9]*)\\.dkr\\.ecr\\.(.*)\\.amazonaws\\.com.*")
exp := regexp.MustCompile(`(?:http://|https://|)([0-9]*)\.dkr\.ecr\.(.*)\.amazonaws\.com.*`)
splitUrl := exp.FindStringSubmatch(ecrUrl)
if len(splitUrl) != 3 {
return "", "", fmt.Errorf("Failed to parse the ECR URL: %s it should be on the form <account number>.dkr.ecr.<region>.amazonaws.com", ecrUrl)

View File

@ -2,9 +2,10 @@ package common
import (
"fmt"
"time"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"time"
)
// This step starts the virtual machine.
@ -29,7 +30,7 @@ func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
ui.Say("Starting the virtual machine...")
guiArgument := "gui"
if s.Headless == true {
if s.Headless {
vrdpIpRaw, vrdpIpOk := state.GetOk("vrdpIp")
vrdpPortRaw, vrdpPortOk := state.GetOk("vrdpPort")

View File

@ -205,7 +205,7 @@ func (d *DownloadClient) VerifyChecksum(path string) (bool, error) {
log.Printf("Verifying checksum of %s", path)
d.config.Hash.Reset()
io.Copy(d.config.Hash, f)
return bytes.Compare(d.config.Hash.Sum(nil), d.config.Checksum) == 0, nil
return bytes.Equal(d.config.Hash.Sum(nil), d.config.Checksum), nil
}
// HTTPDownloader is an implementation of Downloader that downloads

View File

@ -23,7 +23,7 @@ func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error {
}
for _, path := range c.FloppyFiles {
if strings.IndexAny(path, "*?[") >= 0 {
if strings.ContainsAny(path, "*?[") {
_, err = filepath.Glob(path)
} else {
_, err = os.Stat(path)
@ -38,7 +38,7 @@ func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error {
}
for _, path := range c.FloppyDirectories {
if strings.IndexAny(path, "*?[") >= 0 {
if strings.ContainsAny(path, "*?[") {
_, err = filepath.Glob(path)
} else {
_, err = os.Stat(path)

View File

@ -2,10 +2,6 @@ package common
import (
"fmt"
"github.com/mitchellh/go-fs"
"github.com/mitchellh/go-fs/fat"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"io"
"io/ioutil"
"log"
@ -13,6 +9,11 @@ import (
"path"
"path/filepath"
"strings"
"github.com/mitchellh/go-fs"
"github.com/mitchellh/go-fs/fat"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
// StepCreateFloppy will create a floppy disk with the given files.
@ -96,7 +97,7 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction {
// Utility functions for walking through a directory grabbing all files flatly
globFiles := func(files []string, list chan string) {
for _, filename := range files {
if strings.IndexAny(filename, "*?[") >= 0 {
if strings.ContainsAny(filename, "*?[") {
matches, _ := filepath.Glob(filename)
if err != nil {
continue
@ -173,7 +174,7 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction {
ui.Message("Collecting paths from floppy_dirs")
var pathqueue []string
for _, filename := range s.Directories {
if strings.IndexAny(filename, "*?[") >= 0 {
if strings.ContainsAny(filename, "*?[") {
matches, err := filepath.Glob(filename)
if err != nil {
state.Put("error", fmt.Errorf("Error adding path %s to floppy: %s", filename, err))

View File

@ -620,14 +620,10 @@ func (c *comm) scpDownloadSession(path string, output io.Writer) error {
fmt.Fprint(w, "\x00")
if err := checkSCPStatus(stdoutR); err != nil {
return err
}
return nil
return checkSCPStatus(stdoutR)
}
if strings.Index(path, " ") == -1 {
if !strings.Contains(path, " ") {
return c.scpSession("scp -vf "+path, scpFunc)
}
return c.scpSession("scp -vf "+strconv.Quote(path), scpFunc)
@ -805,11 +801,7 @@ func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader, fi *
}
fmt.Fprint(w, "\x00")
if err := checkSCPStatus(r); err != nil {
return err
}
return nil
return checkSCPStatus(r)
}
func scpUploadDirProtocol(name string, w io.Writer, r *bufio.Reader, f func() error, fi os.FileInfo) error {
@ -830,11 +822,7 @@ func scpUploadDirProtocol(name string, w io.Writer, r *bufio.Reader, f func() er
}
fmt.Fprintln(w, "E")
if err != nil {
return err
}
return nil
return err
}
func scpUploadDir(root string, fs []os.FileInfo, w io.Writer, r *bufio.Reader) error {

View File

@ -155,13 +155,8 @@ func (c *config) discover(path string) error {
return err
}
err = c.discoverSingle(
return c.discoverSingle(
filepath.Join(path, "packer-provisioner-*"), &c.Provisioners)
if err != nil {
return err
}
return nil
}
func (c *config) discoverSingle(glob string, m *map[string]string) error {

View File

@ -183,9 +183,7 @@ func (m *muxBroker) timeoutWait(id uint32, p *muxBrokerPending) {
// If we timed out, then check if we have a channel in the buffer,
// and if so, close it.
if timeout {
select {
case s := <-p.ch:
s.Close()
}
s := <-p.ch
s.Close()
}
}

View File

@ -112,7 +112,7 @@ func (p *Provisioner) ProvisionDownload(ui packer.Ui, comm packer.Communicator)
}
}
// if the src was a dir, download the dir
if strings.HasSuffix(src, "/") || strings.IndexAny(src, "*?[") >= 0 {
if strings.HasSuffix(src, "/") || strings.ContainsAny(src, "*?[") {
return comm.DownloadDir(src, dst, nil)
}