vodular/config/config.go

57 lines
1.1 KiB
Go
Raw Normal View History

2026-01-28 12:50:11 +00:00
package config
import (
"fmt"
"os"
"github.com/pelletier/go-toml/v2"
)
type (
Config struct {
Google GoogleConfig `toml:"google"`
}
GoogleConfig struct {
ApiKey string `toml:"api_key"`
ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"`
}
)
var defaultConfig = Config{
Google: GoogleConfig{
ApiKey: "<your API key here>",
ClientID: "<your client ID here>",
ClientSecret: "<your client secret here>",
},
}
const CONFIG_FILENAME = "config.toml"
func ReadConfig(filename string) (*Config, error) {
cfgBytes, err := os.ReadFile(filename)
if err != nil {
if err == os.ErrNotExist {
return nil, nil
}
return nil, fmt.Errorf("failed to open file: %v", err)
}
config := Config{}
err = toml.Unmarshal(cfgBytes, &config)
if err != nil { return &config, fmt.Errorf("failed to parse: %v", err) }
return &config, nil
}
func GenerateConfig(filename string) error {
file, err := os.OpenFile(filename, os.O_CREATE, 0644)
if err != nil { return err }
err = toml.NewEncoder(file).Encode(defaultConfig)
if err != nil { return err }
return nil
}