Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make staticcheck happy #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions fetcher/fetcher_file.go
Expand Up @@ -23,7 +23,7 @@ type File struct {
// Init sets the Path and Interval options
func (f *File) Init() error {
if f.Path == "" {
return fmt.Errorf("Path required")
return errors.New("path required")
}
if f.Interval < 1*time.Second {
f.Interval = 1 * time.Second
Expand Down Expand Up @@ -88,12 +88,12 @@ func (f *File) updateHash() error {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("Open file error: %s", err)
return fmt.Errorf("open file error: %w", err)
}
defer file.Close()
s, err := file.Stat()
if err != nil {
return fmt.Errorf("Get file stat error: %s", err)
return fmt.Errorf("get file stat error: %w", err)
}
f.hash = fmt.Sprintf("%d|%d", s.ModTime().UnixNano(), s.Size())
return nil
Expand Down
17 changes: 9 additions & 8 deletions fetcher/fetcher_github.go
Expand Up @@ -3,6 +3,7 @@ package fetcher
import (
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -46,10 +47,10 @@ func (h *Github) defaultAsset(filename string) bool {
func (h *Github) Init() error {
//apply defaults
if h.User == "" {
return fmt.Errorf("User required")
return errors.New("user required")
}
if h.Repo == "" {
return fmt.Errorf("Repo required")
return errors.New("repo required")
}
if h.Asset == nil {
h.Asset = h.defaultAsset
Expand All @@ -73,7 +74,7 @@ func (h *Github) Fetch() (io.Reader, error) {
//check release status
resp, err := http.Get(h.releaseURL)
if err != nil {
return nil, fmt.Errorf("release info request failed (%s)", err)
return nil, fmt.Errorf("release info request failed (%w)", err)
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
Expand All @@ -82,7 +83,7 @@ func (h *Github) Fetch() (io.Reader, error) {
//clear assets
h.latestRelease.Assets = nil
if err := json.NewDecoder(resp.Body).Decode(&h.latestRelease); err != nil {
return nil, fmt.Errorf("invalid request info (%s)", err)
return nil, fmt.Errorf("invalid request info (%w)", err)
}
resp.Body.Close()
//find appropriate asset
Expand All @@ -100,7 +101,7 @@ func (h *Github) Fetch() (io.Reader, error) {
req, _ := http.NewRequest("HEAD", assetURL, nil)
resp, err = http.DefaultTransport.RoundTrip(req)
if err != nil {
return nil, fmt.Errorf("release location request failed (%s)", err)
return nil, fmt.Errorf("release location request failed (%w)", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusFound {
Expand All @@ -110,12 +111,12 @@ func (h *Github) Fetch() (io.Reader, error) {
//pseudo-HEAD request
req, err = http.NewRequest("GET", s3URL, nil)
if err != nil {
return nil, fmt.Errorf("release location url error (%s)", err)
return nil, fmt.Errorf("release location url error (%w)", err)
}
req.Header.Set("Range", "bytes=0-0") // HEAD not allowed so we request for 1 byte
resp, err = http.DefaultTransport.RoundTrip(req)
if err != nil {
return nil, fmt.Errorf("release location request failed (%s)", err)
return nil, fmt.Errorf("release location request failed (%w)", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusPartialContent {
Expand All @@ -128,7 +129,7 @@ func (h *Github) Fetch() (io.Reader, error) {
//get binary request
resp, err = http.Get(s3URL)
if err != nil {
return nil, fmt.Errorf("release binary request failed (%s)", err)
return nil, fmt.Errorf("release binary request failed (%w)", err)
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
Expand Down
4 changes: 2 additions & 2 deletions fetcher/fetcher_http.go
Expand Up @@ -51,7 +51,7 @@ func (h *HTTP) Fetch() (io.Reader, error) {
//status check using HEAD
resp, err := http.Head(h.URL)
if err != nil {
return nil, fmt.Errorf("HEAD request failed (%s)", err)
return nil, fmt.Errorf("HEAD request failed (%w)", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
Expand All @@ -74,7 +74,7 @@ func (h *HTTP) Fetch() (io.Reader, error) {
//binary fetch using GET
resp, err = http.Get(h.URL)
if err != nil {
return nil, fmt.Errorf("GET request failed (%s)", err)
return nil, fmt.Errorf("GET request failed (%w)", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("GET request failed (status code %d)", resp.StatusCode)
Expand Down
5 changes: 2 additions & 3 deletions fetcher/fetcher_s3.go
Expand Up @@ -34,7 +34,6 @@ type S3 struct {
//GetTimeout defaults to 5 minutes
GetTimeout time.Duration
//interal state
client *http.Client
delay bool
lastETag string
}
Expand Down Expand Up @@ -94,7 +93,7 @@ func (s *S3) Fetch() (io.Reader, error) {
c.Timeout = s.HeadTimeout
resp, err := c.Do(req)
if err != nil {
return nil, fmt.Errorf("HEAD request failed (%s)", err)
return nil, fmt.Errorf("HEAD request failed (%w)", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HEAD request failed (%s)", resp.Status)
Expand All @@ -112,7 +111,7 @@ func (s *S3) Fetch() (io.Reader, error) {
c.Timeout = s.GetTimeout
resp, err = c.Do(req)
if err != nil {
return nil, fmt.Errorf("GET request failed (%s)", err)
return nil, fmt.Errorf("GET request failed (%w)", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("GET request failed (%s)", resp.Status)
Expand Down
4 changes: 2 additions & 2 deletions overseer.go
Expand Up @@ -99,9 +99,9 @@ func Run(c Config) {
err := runErr(&c)
if err != nil {
if c.Required {
log.Fatalf("[overseer] %s", err)
log.Fatalf("[overseer] %+v", err)
} else if c.Debug || !c.NoWarn {
log.Printf("[overseer] disabled. run failed: %s", err)
log.Printf("[overseer] disabled. run failed: %+v", err)
}
c.Program(DisabledState)
return
Expand Down
49 changes: 23 additions & 26 deletions proc_master.go
Expand Up @@ -14,7 +14,6 @@ import (
"os/signal"
"path/filepath"
"strconv"
"sync"
"syscall"
"time"
)
Expand All @@ -27,10 +26,9 @@ type master struct {
slaveID int
slaveCmd *exec.Cmd
slaveExtraFiles []*os.File
binPath, tmpBinPath string
binPath string
binPerms os.FileMode
binHash []byte
restartMux sync.Mutex
restarting bool
restartedAt time.Time
restarted chan bool
Expand All @@ -47,7 +45,7 @@ func (mp *master) run() error {
}
if mp.Config.Fetcher != nil {
if err := mp.Config.Fetcher.Init(); err != nil {
mp.warnf("fetcher init failed (%s). fetcher disabled.", err)
mp.warnf("fetcher init failed (%w). fetcher disabled.", err)
mp.Config.Fetcher = nil
}
}
Expand All @@ -67,11 +65,11 @@ func (mp *master) checkBinary() error {
//get path to binary and confirm its writable
binPath, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to find binary path (%s)", err)
return fmt.Errorf("failed to find binary path (%w)", err)
}
mp.binPath = binPath
if info, err := os.Stat(binPath); err != nil {
return fmt.Errorf("failed to stat binary (%s)", err)
return fmt.Errorf("failed to stat binary (%w)", err)
} else if info.Size() == 0 {
return fmt.Errorf("binary file is empty")
} else {
Expand All @@ -80,7 +78,7 @@ func (mp *master) checkBinary() error {
}
f, err := os.Open(binPath)
if err != nil {
return fmt.Errorf("cannot read binary (%s)", err)
return fmt.Errorf("cannot read binary (%w)", err)
}
//initial hash of file
hash := sha1.New()
Expand All @@ -90,10 +88,10 @@ func (mp *master) checkBinary() error {
//test bin<->tmpbin moves
if mp.Config.Fetcher != nil {
if err := move(tmpBinPath, mp.binPath); err != nil {
return fmt.Errorf("cannot move binary (%s)", err)
return fmt.Errorf("cannot move binary (%w)", err)
}
if err := move(mp.binPath, tmpBinPath); err != nil {
return fmt.Errorf("cannot move binary back (%s)", err)
return fmt.Errorf("cannot move binary back (%w)", err)
}
}
return nil
Expand Down Expand Up @@ -146,7 +144,7 @@ func (mp *master) handleSignal(s os.Signal) {
func (mp *master) sendSignal(s os.Signal) {
if mp.slaveCmd != nil && mp.slaveCmd.Process != nil {
if err := mp.slaveCmd.Process.Signal(s); err != nil {
mp.debugf("signal failed (%s), assuming slave process died unexpectedly", err)
mp.debugf("signal failed (%w), assuming slave process died unexpectedly", err)
os.Exit(1)
}
}
Expand All @@ -157,18 +155,18 @@ func (mp *master) retreiveFileDescriptors() error {
for i, addr := range mp.Config.Addresses {
a, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return fmt.Errorf("Invalid address %s (%s)", addr, err)
return fmt.Errorf("invalid address %s (%w)", addr, err)
}
l, err := net.ListenTCP("tcp", a)
if err != nil {
return err
}
f, err := l.File()
if err != nil {
return fmt.Errorf("Failed to retreive fd for: %s (%s)", addr, err)
return fmt.Errorf("failed to retreive fd for: %s (%w)", addr, err)
}
if err := l.Close(); err != nil {
return fmt.Errorf("Failed to close listener for: %s (%s)", addr, err)
return fmt.Errorf("failed to close listener for: %s (%w)", addr, err)
}
mp.slaveExtraFiles[i] = f
}
Expand All @@ -183,7 +181,7 @@ func (mp *master) fetchLoop() {
t0 := time.Now()
mp.fetch()
//duration fetch of fetch
diff := time.Now().Sub(t0)
diff := time.Since(t0)
if diff < min {
delay := min - diff
//ensures at least MinFetchInterval delay.
Expand All @@ -202,7 +200,7 @@ func (mp *master) fetch() {
}
reader, err := mp.Fetcher.Fetch()
if err != nil {
mp.debugf("failed to get latest version: %s", err)
mp.debugf("failed to get latest version: %w", err)
return
}
if reader == nil {
Expand All @@ -220,7 +218,7 @@ func (mp *master) fetch() {
}
tmpBin, err := os.OpenFile(tmpBinPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
mp.warnf("failed to open temp binary: %s", err)
mp.warnf("failed to open temp binary: %w", err)
return
}
defer func() {
Expand All @@ -233,7 +231,7 @@ func (mp *master) fetch() {
//write to a temp file
_, err = io.Copy(tmpBin, reader)
if err != nil {
mp.warnf("failed to write temp binary: %s", err)
mp.warnf("failed to write temp binary: %w", err)
return
}
//compare hash
Expand All @@ -244,25 +242,25 @@ func (mp *master) fetch() {
}
//copy permissions
if err := chmod(tmpBin, mp.binPerms); err != nil {
mp.warnf("failed to make temp binary executable: %s", err)
mp.warnf("failed to make temp binary executable: %w", err)
return
}
if err := chown(tmpBin, uid, gid); err != nil {
mp.warnf("failed to change owner of binary: %s", err)
mp.warnf("failed to change owner of binary: %w", err)
return
}
if _, err := tmpBin.Stat(); err != nil {
mp.warnf("failed to stat temp binary: %s", err)
mp.warnf("failed to stat temp binary: %w", err)
return
}
tmpBin.Close()
if _, err := os.Stat(tmpBinPath); err != nil {
mp.warnf("failed to stat temp binary by path: %s", err)
mp.warnf("failed to stat temp binary by path: %w", err)
return
}
if mp.Config.PreUpgrade != nil {
if err := mp.Config.PreUpgrade(tmpBinPath); err != nil {
mp.warnf("user cancelled upgrade: %s", err)
mp.warnf("user cancelled upgrade: %w", err)
return
}
}
Expand All @@ -284,7 +282,7 @@ func (mp *master) fetch() {
tokenOut, err := cmd.CombinedOutput()
returned = true
if err != nil {
mp.warnf("failed to run temp binary: %s (%s) output \"%s\"", err, tmpBinPath, tokenOut)
mp.warnf("failed to run temp binary: %w (%s) output \"%s\"", err, tmpBinPath, tokenOut)
return
}
if tokenIn != string(tokenOut) {
Expand All @@ -293,7 +291,7 @@ func (mp *master) fetch() {
}
//overwrite!
if err := move(mp.binPath, tmpBinPath); err != nil {
mp.warnf("failed to overwrite binary: %s", err)
mp.warnf("failed to overwrite binary: %w", err)
return
}
mp.debugf("upgraded binary (%x -> %x)", mp.binHash[:12], newHash[:12])
Expand All @@ -303,7 +301,6 @@ func (mp *master) fetch() {
mp.triggerRestart()
}
//and keep fetching...
return
}

func (mp *master) triggerRestart() {
Expand Down Expand Up @@ -363,7 +360,7 @@ func (mp *master) fork() error {
//include socket files
cmd.ExtraFiles = mp.slaveExtraFiles
if err := cmd.Start(); err != nil {
return fmt.Errorf("Failed to start slave process: %s", err)
return fmt.Errorf("failed to start slave process: %w", err)
}
//was scheduled to restart, notify success
if mp.restarting {
Expand Down
6 changes: 0 additions & 6 deletions proc_slave.go
Expand Up @@ -145,9 +145,3 @@ func (sp *slave) debugf(f string, args ...interface{}) {
log.Printf("[overseer slave#"+sp.id+"] "+f, args...)
}
}

func (sp *slave) warnf(f string, args ...interface{}) {
if sp.Config.Debug || !sp.Config.NoWarn {
log.Printf("[overseer slave#"+sp.id+"] "+f, args...)
}
}
2 changes: 1 addition & 1 deletion proc_slave_others.go
Expand Up @@ -13,7 +13,7 @@ func (sp *slave) watchParent() error {
sp.masterPid = os.Getppid()
proc, err := os.FindProcess(sp.masterPid)
if err != nil {
return fmt.Errorf("master process: %s", err)
return fmt.Errorf("master process: %w", err)
}
sp.masterProc = proc
go func() {
Expand Down
5 changes: 3 additions & 2 deletions proc_slave_windows.go
Expand Up @@ -5,9 +5,10 @@ package overseer
import (
"context"
"fmt"
"github.com/StackExchange/wmi"
"os"
"time"

"github.com/StackExchange/wmi"
)

var (
Expand Down Expand Up @@ -57,7 +58,7 @@ func (sp *slave) watchParent() error {
sp.masterPid = os.Getppid()
proc, err := os.FindProcess(sp.masterPid)
if err != nil {
return fmt.Errorf("master process: %s", err)
return fmt.Errorf("master process: %w", err)
}
sp.masterProc = proc
go func() {
Expand Down
2 changes: 1 addition & 1 deletion sys_windows.go
Expand Up @@ -34,7 +34,7 @@ func move(dst, src string) error {
R := func(s string) string { return replShellMeta.Replace(syscall.EscapeArg(s)) }
cmd := exec.Command("cmd", "/c", `move /y `+R(src)+` `+R(dst))
if b, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("%v: %q: %v", cmd.Args, bytes.TrimSpace(b), err)
return fmt.Errorf("%v: %q: %w", cmd.Args, bytes.TrimSpace(b), err)
}
return nil
}
Expand Down