From c68fcfe439454a0298464fd6e36d5f163fc39c2c Mon Sep 17 00:00:00 2001 From: Elara6331 Date: Wed, 6 Dec 2023 13:28:07 -0800 Subject: [PATCH] Add toml config file --- .gitignore | 3 ++- .goreleaser.yaml | 9 +++++++++ config.go | 27 +++++++++++++++++++++------ owobot.toml | 6 ++++++ 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 owobot.toml diff --git a/.gitignore b/.gitignore index cc40972..fe2e2ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +/dist/ /owobot -/owobot.db \ No newline at end of file +/owobot.db diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 2a52710..b2bbd65 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -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: diff --git a/config.go b/config.go index 130bf3b..52f8018 100644 --- a/config.go +++ b/config.go @@ -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_"}) } diff --git a/owobot.toml b/owobot.toml new file mode 100644 index 0000000..4a6cd51 --- /dev/null +++ b/owobot.toml @@ -0,0 +1,6 @@ +token = "CHANGE ME" +db_path = "/etc/owobot/owobot.db" + +[activity] + type = -1 + name = ""