etchosts: also use errors.Wrap as rest of project

This commit is contained in:
Leo Antunes 2020-05-10 13:07:40 +02:00
parent 20888b1a9b
commit 4f169bc270
1 changed files with 8 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import (
"path" "path"
"strings" "strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -42,14 +43,14 @@ func (eh *EtcHosts) WriteEntries(ipsToNames map[string][]string) error {
// We do not want to create the hosts file; if it's not there, we probably have the wrong path. // We do not want to create the hosts file; if it's not there, we probably have the wrong path.
etcHosts, err := os.OpenFile(hostsPath, os.O_RDWR, 0644) etcHosts, err := os.OpenFile(hostsPath, os.O_RDWR, 0644)
if err != nil { if err != nil {
return fmt.Errorf("could not open %s for reading: %s", hostsPath, err) return errors.Wrapf(err, "could not open %s for reading", hostsPath)
} }
defer etcHosts.Close() defer etcHosts.Close()
// create tmpfile in same folder as // create tmpfile in same folder as
tmp, err := ioutil.TempFile(path.Dir(hostsPath), "etchosts") tmp, err := ioutil.TempFile(path.Dir(hostsPath), "etchosts")
if err != nil { if err != nil {
return fmt.Errorf("could not create tempfile") return errors.Wrap(err, "could not create tempfile")
} }
// remove tempfile; this might fail if we managed to move it, which is ok // remove tempfile; this might fail if we managed to move it, which is ok
@ -98,7 +99,7 @@ func (eh *EtcHosts) writeEntries(orig io.Reader, dest io.Writer, ipsToNames map[
} }
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading hosts file: %s", err) return errors.Wrap(err, "error reading hosts file")
} }
// append remaining entries to file // append remaining entries to file
@ -117,7 +118,7 @@ func (eh *EtcHosts) writeEntryWithBanner(tmp io.Writer, banner, ip string, names
eh.Logger.Printf("writing entry for %s (%s)", ip, names) eh.Logger.Printf("writing entry for %s (%s)", ip, names)
} }
if _, err := fmt.Fprintf(tmp, "%s\t%s\t%s\n", ip, strings.Join(names, " "), banner); err != nil { if _, err := fmt.Fprintf(tmp, "%s\t%s\t%s\n", ip, strings.Join(names, " "), banner); err != nil {
return fmt.Errorf("error writing entry for %s: %s", ip, err) return errors.Wrapf(err, "error writing entry for %s", ip)
} }
} }
return nil return nil
@ -125,12 +126,12 @@ func (eh *EtcHosts) writeEntryWithBanner(tmp io.Writer, banner, ip string, names
func (eh *EtcHosts) movePreservePerms(src, dst *os.File) error { func (eh *EtcHosts) movePreservePerms(src, dst *os.File) error {
if err := src.Sync(); err != nil { if err := src.Sync(); err != nil {
return fmt.Errorf("could not sync changes to %s: %s", src.Name(), err) return errors.Wrapf(err, "could not sync changes to %s", src.Name())
} }
etcHostsInfo, err := dst.Stat() etcHostsInfo, err := dst.Stat()
if err != nil { if err != nil {
return fmt.Errorf("could not stat %s: %s", dst.Name(), err) return errors.Wrapf(err, "could not stat %s", dst.Name())
} }
if err = os.Rename(src.Name(), dst.Name()); err != nil { if err = os.Rename(src.Name(), dst.Name()); err != nil {
@ -152,7 +153,7 @@ func (eh *EtcHosts) movePreservePerms(src, dst *os.File) error {
// ensure we're not running with some umask that might break things // ensure we're not running with some umask that might break things
if err := src.Chmod(etcHostsInfo.Mode()); err != nil { if err := src.Chmod(etcHostsInfo.Mode()); err != nil {
return fmt.Errorf("could not chmod %s: %s", src.Name(), err) return errors.Wrapf(err, "could not chmod %s", src.Name())
} }
// TODO: also keep user? // TODO: also keep user?