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: "", ClientID: "", ClientSecret: "", }, } 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) }