ampelmaennchen.git

ref: efe1b69f2c6a1bca388e37716a1e5931bbfcc101

./main.go


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main

import (
	"fmt"
	"log"
	"os"

	"apiote.xyz/p/ampelmaennchen/config"
	"apiote.xyz/p/ampelmaennchen/db"
	"apiote.xyz/p/ampelmaennchen/matrix"
	"git.sr.ht/~sircmpwn/getopt"
)

var (
	recreateRegistration bool
	command              string
)

func parseArgs() {
	opts, optind, err := getopt.Getopts(os.Args, "r")
	if err != nil {
		log.Panicf("while getopts: %v", err)
	}
	for _, opt := range opts {
		switch opt.Option {
		case 'r':
			recreateRegistration = true
		}
	}
	for i, arg := range os.Args[optind:] {
		if i == 0 {
			command = arg
		}
	}
}

func main() {
	parseArgs()
	cfg, err := config.Load("./config.toml")
	if err != nil {
		log.Panicf("while loading config: %v", err)
	}

	err = db.Connect(cfg.Database.Uri())
	if err != nil {
		log.Panicf("while connecting to db: %v", err)
	}
	defer db.Close()
	err = db.Migrate()
	if err != nil {
		log.Panicf("while migrating: %v", err)
	}

	registrationExists, err := matrix.RegistrationExists()
	if err != nil {
		log.Panicf("while checking existing registration: %v", err)
	}

	as, err := matrix.CreateAppService(cfg.Matrix, recreateRegistration)
	if err != nil {
		log.Panicf("while creating app service: %v", err)
	}

	if !registrationExists || recreateRegistration {
		registration, err := as.Registration.YAML()
		if err != nil {
			log.Panicf("while generating registration yaml: %v", err)
		}
		err = os.WriteFile("registration.yml", []byte(registration), 0o644)
		if err != nil {
			log.Panicf("while saving registration yaml: %v", err)
		}
		fmt.Println("Registration has been saved to registration.yml file")
		return
	}

	err = matrix.SetDisplayName(as, cfg.Matrix)
	log.Println(err)

	spaceID, err := matrix.CreateSpace(as, cfg.Matrix)
	log.Println(err)
	err = matrix.CreatePublicRoom(as, cfg.Matrix, spaceID)
	log.Println(err)

	// TODO create rooms
	// TODO add rooms to space
	// TODO serve
}