Skip to content

Commit

Permalink
Add istidy check
Browse files Browse the repository at this point in the history
  • Loading branch information
egonelbre committed Nov 9, 2018
1 parent 3198295 commit 1883010
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
27 changes: 27 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,33 @@ func (cmd Common) VendorModules() {
ErrFatalf(err, "go mod vendor failed: %v\n", err)
}

func (cmd Common) Tidy() {
fmt.Fprintf(os.Stderr, "# Tidying modules\n")

workdir, err := os.Getwd()
ErrFatalf(err, "unable to get working directory: %v\n", err)

defer func() {
err = os.Chdir(workdir)
ErrFatalf(err, "unable to change directory: %v\n", err)
}()

err = os.Chdir(cmd.RepoDir())
ErrFatalf(err, "unable to change directory: %v\n", err)

for repeat := 2; repeat > 0; repeat-- {
gomod := exec.Command("go", "mod", "tidy", "-v")
gomod.Env = append(os.Environ(), "GO111MODULE=on")
gomod.Stdout, gomod.Stderr = os.Stderr, os.Stderr
err = gomod.Run()
Errf(err, "go mod tidy failed, retrying: %v\n", err)
if err == nil {
break
}
}
ErrFatalf(err, "go mod tidy failed: %v\n", err)
}

func (cmd Common) DeleteVendor() {
fmt.Fprintf(os.Stderr, "# Deleting vendor\n")

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func main() {
&Update{Common: common},
// &Cache{Common: common},

&IsTidy{Common: common},
&Hash{Common: common},
&ZipVendor{Common: common},
&UnzipVendor{Common: common},
Expand Down
39 changes: 39 additions & 0 deletions mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.

package main

import (
"flag"
"fmt"
"os"
"path/filepath"
)

type IsTidy struct {
Common
}

func (cmd *IsTidy) Name() string { return "istidy" }

func (cmd *IsTidy) Parse(args []string) error {
set := flag.NewFlagSet(cmd.Name(), flag.ExitOnError)
return set.Parse(args)
}

func (cmd *IsTidy) Exec() {
repodir := cmd.RepoDir()

before, err := HashFiles(filepath.Join(repodir, "go.mod"))
ErrFatal(err)

cmd.Tidy()

after, err := HashFiles(filepath.Join(repodir, "go.mod"))
ErrFatal(err)

if before != after {
fmt.Fprintln(os.Stderr, "go.mod is not tidy")
os.Exit(1)
}
}

0 comments on commit 1883010

Please sign in to comment.