Skip to content

Instantly share code, notes, and snippets.

@dlintw
Created June 1, 2019 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlintw/6edcebf0b2d0341eb97e7ea08cdb141b to your computer and use it in GitHub Desktop.
Save dlintw/6edcebf0b2d0341eb97e7ea08cdb141b to your computer and use it in GitHub Desktop.
replace dbg: as dbg:<line_number>
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"regexp"
"strings"
)
var check = func(err error) {
if err != nil {
panic(err)
}
}
func usage() {
fmt.Fprintln(os.Stderr, "Usage:", path.Base(os.Args[0]), "<file>")
flag.PrintDefaults()
os.Exit(1)
}
var re = regexp.MustCompile(`^(.*dbg:)([0-9]+)(.*)$`)
func main() {
log.SetFlags(log.Lshortfile)
flag.Usage = usage
flag.Parse()
if flag.NArg() != 1 {
usage()
}
updated := false
filename := flag.Arg(0)
input, err := ioutil.ReadFile(filename)
check(err)
lines := strings.Split(string(input), "\n")
for i, line := range lines {
newline := re.ReplaceAllString(line, fmt.Sprintf(`${1}%d$3`, i+1))
if newline != line {
lines[i] = newline
updated = true
}
}
if updated {
fi, err := os.Lstat(filename)
check(err)
fmt.Printf("permissions: %#o\n", fi.Mode().Perm())
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(filename, []byte(output), fi.Mode().Perm())
check(err)
fmt.Println("patched", filename)
} else {
fmt.Println("skip", filename)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment