bin/replace.go (view raw)
1// replace -- in-place edit a post
2// usage: replace [old string] [new string]
3
4package main
5
6import (
7 "fmt"
8 "log"
9 "os"
10 "strings"
11)
12
13const dir = "pages/blog/"
14
15func main() {
16 if len(os.Args) != 3 {
17 fmt.Println("usage: replace [old string] [new string]")
18 os.Exit(1)
19 }
20
21 oldLine, newLine := os.Args[1], os.Args[2]
22
23 posts, err := os.ReadDir(dir)
24 if err != nil {
25 log.Fatalln(err)
26 }
27 for _, p := range posts {
28 path := dir + p.Name()
29 input, err := os.ReadFile(path)
30 if err != nil {
31 log.Fatalln(err)
32 }
33
34 lines := strings.Split(string(input), "\n")
35 for i, line := range lines {
36 if strings.Contains(line, oldLine) {
37 lines[i] = newLine
38 }
39 }
40 output := strings.Join(lines, "\n")
41 err = os.WriteFile(path, []byte(output), 644)
42 if err != nil {
43 log.Fatalln(err)
44 }
45 }
46}