all repos — paprika @ 4f5ac048596e44147f9d9db0731b987a3e92d1bf

go rewrite of taigabot

config/helpers.go (view raw)

 1package config
 2
 3import (
 4	"io/ioutil"
 5	"log"
 6	"os"
 7	"path"
 8	"strings"
 9)
10
11func SplitChannelList(channels []string) string {
12	lineSize := 0
13	first := true
14
15	var ret strings.Builder
16
17	// Splits configured list of channels into a safe set of commands
18	for _, channel := range channels {
19		if lineSize+len(channel) > 510 {
20			lineSize = 0
21			first = true
22			ret.WriteByte('\r')
23			ret.WriteByte('\n')
24		}
25
26		if !first {
27			ret.WriteByte(',')
28			lineSize += 1
29		} else {
30			ret.WriteString("JOIN ")
31			lineSize += 5
32			first = false
33		}
34		ret.WriteString(channel)
35		lineSize += len(channel)
36	}
37	ret.WriteByte('\r')
38	ret.WriteByte('\n')
39
40	return ret.String()
41}
42
43func getDbPath() string {
44	// https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RuntimeDirectory=
45	systemdStateDir := os.Getenv("STATE_DIRECTORY")
46	xdgDataDir := os.Getenv("XDG_DATA_HOME")
47	if systemdStateDir == "" && xdgDataDir == "" {
48		log.Print("Warning: Nowhere to store state!")
49		log.Print("- Please add StateDirectory= to your systemd unit or add db-path to your config.")
50
51		// TODO: change this to os.MkdirTemp when 1.17 is more common
52		dir, err := ioutil.TempDir("", "paprika-bot")
53		if err != nil {
54			log.Fatalf("Failed making temporary area: %s", err)
55		}
56		log.Printf("Warning: Generated a temporary path: %s", dir)
57		return dir
58	} else if xdgDataDir != "" {
59		return path.Join(xdgDataDir, "paprika")
60	} else {
61		return systemdStateDir
62	}
63}
64
65func usage() {
66	println("Usage: paprika [init] [-c config]\n")
67	println("  init       Initialize configuration for the bot.")
68	println("  -c config  Use config given on the command line.\n")
69	os.Exit(1)
70}