From 79287d7e477437024d7bc2a8c31ea33dfe2b31ca Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 28 Mar 2017 18:29:55 -0700 Subject: [PATCH] simplify some code --- builder/amazon/common/access_config.go | 4 ++-- builder/amazon/common/ami_config.go | 2 +- builder/amazon/common/cli_config.go | 5 +---- builder/amazon/common/step_source_ami_info.go | 2 +- builder/docker/ecr_login.go | 2 +- builder/virtualbox/common/step_run.go | 5 +++-- common/download.go | 2 +- common/floppy_config.go | 4 ++-- common/step_create_floppy.go | 13 ++++++------ communicator/ssh/communicator.go | 20 ++++--------------- config.go | 7 +------ packer/rpc/mux_broker.go | 6 ++---- provisioner/file/provisioner.go | 2 +- 13 files changed, 27 insertions(+), 47 deletions(-) diff --git a/builder/amazon/common/access_config.go b/builder/amazon/common/access_config.go index 046631755..5e11fcd32 100644 --- a/builder/amazon/common/access_config.go +++ b/builder/amazon/common/access_config.go @@ -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)) } } diff --git a/builder/amazon/common/ami_config.go b/builder/amazon/common/ami_config.go index c7674dbbe..a60d0d788 100644 --- a/builder/amazon/common/ami_config.go +++ b/builder/amazon/common/ami_config.go @@ -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 } diff --git a/builder/amazon/common/cli_config.go b/builder/amazon/common/cli_config.go index fc4d36fd7..d37c1d14e 100644 --- a/builder/amazon/common/cli_config.go +++ b/builder/amazon/common/cli_config.go @@ -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) { diff --git a/builder/amazon/common/step_source_ami_info.go b/builder/amazon/common/step_source_ami_info.go index bd03fcdf8..2346502fc 100644 --- a/builder/amazon/common/step_source_ami_info.go +++ b/builder/amazon/common/step_source_ami_info.go @@ -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()) diff --git a/builder/docker/ecr_login.go b/builder/docker/ecr_login.go index 6d2df0877..737107589 100644 --- a/builder/docker/ecr_login.go +++ b/builder/docker/ecr_login.go @@ -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 .dkr.ecr..amazonaws.com", ecrUrl) diff --git a/builder/virtualbox/common/step_run.go b/builder/virtualbox/common/step_run.go index 9958bf2a2..487f31430 100644 --- a/builder/virtualbox/common/step_run.go +++ b/builder/virtualbox/common/step_run.go @@ -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") diff --git a/common/download.go b/common/download.go index e4d9d4048..86dea34d1 100644 --- a/common/download.go +++ b/common/download.go @@ -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 diff --git a/common/floppy_config.go b/common/floppy_config.go index 5a68816ed..ecaa5eddb 100644 --- a/common/floppy_config.go +++ b/common/floppy_config.go @@ -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) diff --git a/common/step_create_floppy.go b/common/step_create_floppy.go index eb0a6057e..d00bc4da8 100644 --- a/common/step_create_floppy.go +++ b/common/step_create_floppy.go @@ -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)) diff --git a/communicator/ssh/communicator.go b/communicator/ssh/communicator.go index e3082bac1..9cef308fb 100644 --- a/communicator/ssh/communicator.go +++ b/communicator/ssh/communicator.go @@ -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 { diff --git a/config.go b/config.go index 801d73437..a62e28323 100644 --- a/config.go +++ b/config.go @@ -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 { diff --git a/packer/rpc/mux_broker.go b/packer/rpc/mux_broker.go index 7af76f640..aeb72afbf 100644 --- a/packer/rpc/mux_broker.go +++ b/packer/rpc/mux_broker.go @@ -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() } } diff --git a/provisioner/file/provisioner.go b/provisioner/file/provisioner.go index 19e5c4bea..433c9f23a 100644 --- a/provisioner/file/provisioner.go +++ b/provisioner/file/provisioner.go @@ -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) }