From 319829516a2b2e92435e97d56cea7d8cef17c458 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Fri, 9 Nov 2018 12:23:38 +0200 Subject: [PATCH] Restore go.mod file in case it changes. --- main.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index a527b4f..9cecfb7 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,10 @@ package main import ( + "bytes" "flag" "fmt" + "io/ioutil" "os" "path/filepath" "strings" @@ -15,6 +17,9 @@ type Command interface { Name() string Parse(args []string) error Exec() + + RepoDir() string + Path(args ...string) string } type Common struct { @@ -92,7 +97,7 @@ func main() { fmt.Fprintln(os.Stderr, "invalid args", err) os.Exit(1) } - cmd.Exec() + Exec(cmd) return } } @@ -104,3 +109,20 @@ func main() { } os.Exit(1) } + +func Exec(cmd Command) { + gomodfilename := filepath.Join(cmd.RepoDir(), "go.mod") + + gomod, gomoderr := ioutil.ReadFile(gomodfilename) + defer func() { + if gomoderr != nil { + return + } + gomodnew, gomodnewerr := ioutil.ReadFile(gomodfilename) + if gomodnewerr == nil && !bytes.Equal(gomod, gomodnew) { + ioutil.WriteFile(gomodfilename, gomod, 0644) + } + }() + + cmd.Exec() +}