Skip to content

Commit

Permalink
Restore go.mod file in case it changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
egonelbre committed Nov 9, 2018
1 parent 0f7c432 commit 3198295
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package main

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -15,6 +17,9 @@ type Command interface {
Name() string
Parse(args []string) error
Exec()

RepoDir() string
Path(args ...string) string
}

type Common struct {
Expand Down Expand Up @@ -92,7 +97,7 @@ func main() {
fmt.Fprintln(os.Stderr, "invalid args", err)
os.Exit(1)
}
cmd.Exec()
Exec(cmd)
return
}
}
Expand All @@ -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()
}

0 comments on commit 3198295

Please sign in to comment.