town/cmd/dumps/main.go

97 lines
1.8 KiB
Go
Raw Normal View History

2023-03-15 07:53:36 +00:00
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 {
2023-03-15 08:11:55 +00:00
Created float64
Email string
2023-03-15 07:53:36 +00:00
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 {
2023-03-15 08:13:44 +00:00
l = strings.TrimSpace(l)
if l == "" {
continue
}
2023-03-15 07:53:36 +00:00
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),
2023-03-15 08:11:55 +00:00
Email: s.Email,
2023-03-15 07:53:36 +00:00
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()))
}
}
}
}