town/cmd/dumps/main.go

97 lines
1.8 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
"git.tilde.town/tildetown/town/models"
"git.tilde.town/tildetown/town/signup"
"github.com/AlecAivazis/survey/v2"
)
func confirmContinue(msg string) {
var serr error
var conf bool
if serr = survey.AskOne(&survey.Confirm{
Message: msg,
Default: false,
}, &conf); serr != nil {
os.Exit(2)
}
if !conf {
os.Exit(1)
}
}
type jsonSignup struct {
Created float64
Email string
Username string
Reasons string
Plans string
Referral string
Socials string
Notes string
}
func main() {
lol, err := os.ReadFile("/town/var/signups.json")
if err != nil {
panic(err)
}
db, err := signup.ConnectDB()
if err != nil {
panic(err)
}
lines := strings.Split(string(lol), "\n")
errs := []error{}
signups := make([]jsonSignup, len(lines))
for i, l := range lines {
l = strings.TrimSpace(l)
if l == "" {
continue
}
s := jsonSignup{}
err := json.Unmarshal([]byte(l), &s)
if err != nil {
fmt.Printf("%s %s", l, err.Error())
errs = append(errs, err)
} else {
signups[i] = s
}
}
if len(errs) > 0 {
confirmContinue(fmt.Sprintf("%d errors found deserializing; continue?", len(errs)))
}
for _, s := range signups {
ts := models.TownSignup{
Created: time.Unix(int64(s.Created), 0),
Email: s.Email,
How: s.Referral,
Why: s.Reasons + "\n" + s.Plans,
Links: s.Socials,
}
if err = ts.Insert(db); err != nil {
confirmContinue(fmt.Sprintf("%#v led to error %s; continue?", ts, err.Error()))
}
if s.Notes != "" {
ts.Notes = []models.SignupNote{
{
Author: "IMPORT",
Content: s.Notes,
},
}
if err = ts.RefreshNotes(db); err != nil {
confirmContinue(fmt.Sprintf("%#v led to error %s; continue?", ts, err.Error()))
}
}
}
}