Add toml config file
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Elara 2023-12-06 13:28:07 -08:00
parent 96a6360629
commit c68fcfe439
4 changed files with 38 additions and 7 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
/dist/
/owobot
/owobot.db
/owobot.db

View File

@ -17,6 +17,7 @@ builds:
archives:
- files:
- owobot.service
- owobot.toml
nfpms:
- id: owobot
description: "Your server's guardian and entertainer"
@ -34,6 +35,9 @@ nfpms:
contents:
- src: owobot.service
dst: /etc/systemd/system/owobot.service
- src: owobot.toml
dst: /etc/owobot/config.toml
type: "config|noreplace"
aurs:
- name: owobot-bin
homepage: 'https://gitea.elara.ws/owobot/owobot'
@ -47,10 +51,15 @@ aurs:
- owobot
conflicts:
- owobot
backup:
- etc/owobot/config.toml
package: |-
# binaries
install -Dm755 ./owobot "${pkgdir}/usr/bin/owobot"
# configs
install -Dm644 ./owobot.toml "${pkgdir}/etc/owobot/config.toml"
# services
install -Dm644 ./owobot.service "${pkgdir}/etc/systemd/system/owobot.service"
release:

View File

@ -27,20 +27,33 @@ import (
)
type Config struct {
Token string `env:"TOKEN,notEmpty" toml:"token"`
DBPath string `env:"DB_PATH" envDefault:"owobot.db" toml:"db_path"`
Token string `env:"TOKEN" toml:"token"`
DBPath string `env:"DB_PATH" toml:"db_path"`
Activity Activity `envPrefix:"ACTIVITY_" toml:"activity"`
}
type Activity struct {
Type discordgo.ActivityType `env:"TYPE" envDefault:"-1" toml:"type"`
Name string `env:"NAME" envDefault:"" toml:"name"`
Type discordgo.ActivityType `env:"TYPE" toml:"type"`
Name string `env:"NAME" toml:"name"`
}
func loadConfig() (*Config, error) {
cfg := &Config{}
// Create a new config struct with default values
cfg := &Config{
Token: "",
DBPath: "owobot.db",
Activity: Activity{
Type: -1,
Name: "",
},
}
fl, err := os.Open("/etc/owobot.toml")
configPath := os.Getenv("OWOBOT_CONFIG_PATH")
if configPath == "" {
configPath = "/etc/owobot/config.toml"
}
fl, err := os.Open(configPath)
if err == nil {
err = toml.NewDecoder(fl).Decode(cfg)
if err != nil {
@ -49,5 +62,7 @@ func loadConfig() (*Config, error) {
fl.Close()
}
println(cfg.Activity.Type, cfg.Activity.Name)
return cfg, env.ParseWithOptions(cfg, env.Options{Prefix: "OWOBOT_"})
}

6
owobot.toml Normal file
View File

@ -0,0 +1,6 @@
token = "CHANGE ME"
db_path = "/etc/owobot/owobot.db"
[activity]
type = -1
name = ""