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"` Secure bool `toml:"bool" comment:"Whether or not host is secured with TLS (HTTPS)"` Port int16 `toml:"port"` VODDirectory string `toml:"vod_directory" comment:"Directory to be used for VOD management"` 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", Secure: false, Port: 8080, 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 { return nil, fmt.Errorf("failed to open file: %v", err) } var config Config err = toml.Unmarshal(cfgBytes, &config) if err != nil { return nil, 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) }