Last active
May 15, 2019 17:17
-
-
Save whitekid/36e84f18aa5bcf232983fa4a9bb515a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"fmt" | |
"os" | |
"regexp" | |
"strings" | |
) | |
func main() { | |
mod, err := os.Open("go.mod") | |
if err != nil { | |
panic(err) | |
} | |
var buf bytes.Buffer | |
var replace bytes.Buffer | |
replaceRe := regexp.MustCompile(`^replace\s+[a-zA-Z]`) | |
scanner := bufio.NewScanner(mod) | |
for scanner.Scan() { | |
text := scanner.Text() | |
if strings.HasPrefix(text, "//") { | |
continue | |
} | |
if replaceRe.MatchString(text) { | |
replace.WriteString(text[8:]) | |
replace.WriteRune('\n') | |
continue | |
} | |
buf.Write(scanner.Bytes()) | |
buf.WriteRune('\n') | |
} | |
mod.Close() | |
tidy, err := os.Create("go.mod") | |
if err != nil { | |
panic(err) | |
} | |
defer tidy.Close() | |
replaceBytes := replace.Bytes() | |
scanner = bufio.NewScanner(&replace) | |
content := buf.String() | |
for scanner.Scan() { | |
p := strings.Split(scanner.Text(), " ") | |
old := fmt.Sprintf("%s v0.0.0-00010101000000-000000000000", p[0]) | |
new := fmt.Sprintf("%s %s", p[0], p[3]) | |
content = strings.ReplaceAll(content, old, new) | |
} | |
tidy.WriteString("// DO NOT EDIT, auto generated by make dep\n") | |
tidy.WriteString(content) | |
if len(replaceBytes) > 0 { | |
tidy.WriteString("replace (\n") | |
tidy.Write(replaceBytes) | |
tidy.WriteString(")\n") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment