repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / cmd / scripts / shasum
Antonio Mika · 08 Oct 24

shasum.go

 1package main
 2
 3import (
 4	"log/slog"
 5	"os"
 6
 7	"github.com/picosh/pico/db/postgres"
 8	"github.com/picosh/pico/shared"
 9	"github.com/picosh/utils"
10)
11
12func main() {
13	logger := slog.Default()
14	picoCfg := shared.NewConfigSite()
15	picoCfg.Logger = logger
16	picoCfg.DbURL = os.Getenv("DATABASE_URL")
17	picoDb := postgres.NewDB(picoCfg.DbURL, picoCfg.Logger)
18
19	logger.Info("fetching all posts")
20	posts, err := picoDb.FindPosts()
21	if err != nil {
22		panic(err)
23	}
24
25	empty := 0
26	diff := 0
27	for _, post := range posts {
28		nextShasum := utils.Shasum([]byte(post.Text))
29		if post.Shasum == "" {
30			empty += 1
31		} else if post.Shasum != nextShasum {
32			diff += 1
33		}
34		post.Shasum = nextShasum
35
36		_, err := picoDb.UpdatePost(post)
37		if err != nil {
38			panic(err)
39		}
40	}
41
42	logger.Info("empty, diff", "empty", empty, "diff", diff)
43}