From 8a0fd66a89e8c7c21fbdab5307813425dc4a5765 Mon Sep 17 00:00:00 2001 From: Fedor Korotkiy Date: Sat, 28 Mar 2020 17:35:01 +0300 Subject: [PATCH] Fix linter --- distbuild/disttest/fixture.go | 6 ++-- distbuild/pkg/artifact/cache.go | 4 +-- distbuild/pkg/dist/schedule.go | 10 +++--- distbuild/pkg/tarstream/stream.go | 6 ++-- distbuild/pkg/worker/job.go | 54 +++++++++++++++---------------- distbuild/pkg/worker/worker.go | 1 + 6 files changed, 43 insertions(+), 38 deletions(-) diff --git a/distbuild/disttest/fixture.go b/distbuild/disttest/fixture.go index 610c9c0..96e8ab4 100644 --- a/distbuild/disttest/fixture.go +++ b/distbuild/disttest/fixture.go @@ -84,10 +84,12 @@ func newEnv(t *testing.T) (e *env, cancel func()) { workerName := fmt.Sprintf("worker%d", i) workerDir := filepath.Join(env.RootDir, workerName) - fileCache, err := filecache.New(filepath.Join(workerDir, "filecache")) + var fileCache *filecache.Cache + fileCache, err = filecache.New(filepath.Join(workerDir, "filecache")) require.NoError(t, err) - artifacts, err := artifact.NewCache(filepath.Join(workerDir, "artifacts")) + var artifacts *artifact.Cache + artifacts, err = artifact.NewCache(filepath.Join(workerDir, "artifacts")) require.NoError(t, err) w := worker.New( diff --git a/distbuild/pkg/artifact/cache.go b/distbuild/pkg/artifact/cache.go index 183d2ca..0d321a6 100644 --- a/distbuild/pkg/artifact/cache.go +++ b/distbuild/pkg/artifact/cache.go @@ -66,7 +66,7 @@ func (c *Cache) readLock(id build.ID) error { return ErrWriteLocked } - c.readLocked[id] += 1 + c.readLocked[id]++ return nil } @@ -74,7 +74,7 @@ func (c *Cache) readUnlock(id build.ID) { c.mu.Lock() defer c.mu.Unlock() - c.readLocked[id] -= 1 + c.readLocked[id]-- if c.readLocked[id] == 0 { delete(c.readLocked, id) } diff --git a/distbuild/pkg/dist/schedule.go b/distbuild/pkg/dist/schedule.go index 0fccf30..16ccf28 100644 --- a/distbuild/pkg/dist/schedule.go +++ b/distbuild/pkg/dist/schedule.go @@ -38,12 +38,12 @@ func (c *Coordinator) scheduleJob(job *build.Job) *scheduledJob { if scheduled, ok := c.scheduledJobs[job.ID]; ok { return scheduled - } else { - scheduled = newScheduledJob(job) - c.scheduledJobs[job.ID] = scheduled - c.queue = append(c.queue, scheduled) - return scheduled } + + scheduled := newScheduledJob(job) + c.scheduledJobs[job.ID] = scheduled + c.queue = append(c.queue, scheduled) + return scheduled } func (c *Coordinator) pickJob() (*build.Job, bool) { diff --git a/distbuild/pkg/tarstream/stream.go b/distbuild/pkg/tarstream/stream.go index 8baadd9..85c3b59 100644 --- a/distbuild/pkg/tarstream/stream.go +++ b/distbuild/pkg/tarstream/stream.go @@ -24,12 +24,14 @@ func Send(dir string, w io.Writer) error { return nil } - if info.IsDir() { + switch { + case info.IsDir(): return tw.WriteHeader(&tar.Header{ Name: rel, Typeflag: tar.TypeDir, }) - } else { + + default: h := &tar.Header{ Typeflag: tar.TypeReg, Name: rel, diff --git a/distbuild/pkg/worker/job.go b/distbuild/pkg/worker/job.go index 2a03cee..d5f1de1 100644 --- a/distbuild/pkg/worker/job.go +++ b/distbuild/pkg/worker/job.go @@ -63,30 +63,30 @@ func executeCmd(ctx context.Context, cmd *build.Cmd) (stdout, stderr []byte, exi if cmd.CatOutput != "" { err = ioutil.WriteFile(cmd.CatOutput, []byte(cmd.CatTemplate), 0666) return - } else { - p := exec.CommandContext(ctx, cmd.Exec[0], cmd.Exec[1:]...) - p.Dir = cmd.WorkingDirectory - p.Env = cmd.Environ - p.Stdout = &stdoutBuf - p.Stderr = &stderrBuf - - err = p.Run() - - stdout = stdoutBuf.Bytes() - stderr = stderrBuf.Bytes() - - if err != nil { - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { - exitCode = exitErr.ExitCode() - err = nil - } - } - return } + + p := exec.CommandContext(ctx, cmd.Exec[0], cmd.Exec[1:]...) + p.Dir = cmd.WorkingDirectory + p.Env = cmd.Environ + p.Stdout = &stdoutBuf + p.Stderr = &stderrBuf + + err = p.Run() + + stdout = stdoutBuf.Bytes() + stderr = stderrBuf.Bytes() + + if err != nil { + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + exitCode = exitErr.ExitCode() + err = nil + } + } + return } -func (w *Worker) prepareSourceDir(sourceDir string, sourceFiles map[build.ID]string) (unlock func(), err error) { +func (w *Worker) prepareSourceDir(sourceDir string, sourceFiles map[build.ID]string) (unlockSources func(), err error) { var unlocks []func() doUnlock := func() { for _, u := range unlocks { @@ -119,12 +119,12 @@ func (w *Worker) prepareSourceDir(sourceDir string, sourceFiles map[build.ID]str } } - unlock = doUnlock + unlockSources = doUnlock doUnlock = nil return } -func (w *Worker) lockDeps(deps []build.ID) (paths map[build.ID]string, unlock func(), err error) { +func (w *Worker) lockDeps(deps []build.ID) (paths map[build.ID]string, unlockDeps func(), err error) { var unlocks []func() doUnlock := func() { for _, u := range unlocks { @@ -150,7 +150,7 @@ func (w *Worker) lockDeps(deps []build.ID) (paths map[build.ID]string, unlock fu paths[id] = filepath.Join(path, outputDirName) } - unlock = doUnlock + unlockDeps = doUnlock doUnlock = nil return } @@ -173,18 +173,18 @@ func (w *Worker) runJob(ctx context.Context, spec *proto.JobSpec) (*proto.JobRes return } - if err := abort(); err != nil { + if err = abort(); err != nil { w.log.Warn("error aborting job", zap.Any("job_id", spec.Job.ID), zap.Error(err)) } }() outputDir := filepath.Join(aRoot, outputDirName) - if err := os.Mkdir(outputDir, 0777); err != nil { + if err = os.Mkdir(outputDir, 0777); err != nil { return nil, err } sourceDir := filepath.Join(aRoot, srcDirName) - if err := os.Mkdir(sourceDir, 0777); err != nil { + if err = os.Mkdir(sourceDir, 0777); err != nil { return nil, err } diff --git a/distbuild/pkg/worker/worker.go b/distbuild/pkg/worker/worker.go index cde1d9b..e55795c 100644 --- a/distbuild/pkg/worker/worker.go +++ b/distbuild/pkg/worker/worker.go @@ -117,6 +117,7 @@ func (w *Worker) Run(ctx context.Context) error { zap.Int("num_jobs", len(rsp.JobsToRun))) for _, spec := range rsp.JobsToRun { + spec := spec result, err := w.runJob(ctx, &spec) if err != nil { errStr := fmt.Sprintf("job %s failed: %v", spec.Job.ID, err)