all repos — site @ a3252db9fa2c46d985ee6b7bf66d86bed0f5871a

source for my site, found at icyphox.sh

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	oldStr, newStr := 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, oldStr) {
37				line = strings.ReplaceAll(line, oldStr, newStr)
38				lines[i] = line
39			}
40		}
41		output := strings.Join(lines, "\n")
42		err = os.WriteFile(path, []byte(output), 644)
43		if err != nil {
44			log.Fatalln(err)
45		}
46	}
47}