vodular/config/config.go
ari melody 6adbd3e0f1
All checks were successful
/ build-linux-amd64 (push) Successful in 1m12s
check if fullvod exists; store config in UserConfigDir
2026-01-31 02:28:09 +00:00

64 lines
1.5 KiB
Go

package config
import (
"fmt"
"os"
"github.com/pelletier/go-toml/v2"
"golang.org/x/oauth2"
)
type (
GoogleConfig struct {
ApiKey string `toml:"api_key"`
ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"`
}
Config struct {
Host string `toml:"host" comment:"Address to host OAuth2 redirect flow"`
RedirectUri string `toml:"redirect_uri" comment:"URI to use in Google OAuth2 flow"`
Google GoogleConfig `toml:"google"`
Token *oauth2.Token `toml:"token" comment:"This section is filled in automatically on a successful authentication flow."`
}
)
var defaultConfig = Config{
Host: "localhost:8090",
RedirectUri: "http://localhost:8090",
Google: GoogleConfig{
ApiKey: "<your API key here>",
ClientID: "<your client ID here>",
ClientSecret: "<your client secret here>",
},
}
var CONFIG_FILENAME string = "config.toml"
func ReadConfig(filename string) (*Config, error) {
cfgBytes, err := os.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("failed to open file: %v", err)
}
var config Config
err = toml.Unmarshal(cfgBytes, &config)
if err != nil { return &config, fmt.Errorf("failed to parse: %v", err) }
return &config, nil
}
func WriteConfig(cfg *Config, filename string) error {
file, err := os.OpenFile(filename, os.O_CREATE | os.O_RDWR, 0644)
if err != nil { return err }
err = toml.NewEncoder(file).Encode(cfg)
return err
}
func GenerateConfig(filename string) error {
return WriteConfig(&defaultConfig, filename)
}