more customisation, more QoL improvements
an all-around good time!
This commit is contained in:
parent
2954689784
commit
45db651388
9 changed files with 315 additions and 147 deletions
|
|
@ -5,21 +5,27 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
type (
|
||||
Config struct {
|
||||
Google GoogleConfig `toml:"google"`
|
||||
GoogleConfig struct {
|
||||
ApiKey string `toml:"api_key"`
|
||||
ClientID string `toml:"client_id"`
|
||||
ClientSecret string `toml:"client_secret"`
|
||||
}
|
||||
|
||||
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>",
|
||||
|
|
@ -38,19 +44,21 @@ func ReadConfig(filename string) (*Config, error) {
|
|||
return nil, fmt.Errorf("failed to open file: %v", err)
|
||||
}
|
||||
|
||||
config := Config{}
|
||||
var 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)
|
||||
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(defaultConfig)
|
||||
if err != nil { return err }
|
||||
|
||||
return nil
|
||||
err = toml.NewEncoder(file).Encode(cfg)
|
||||
return err
|
||||
}
|
||||
|
||||
func GenerateConfig(filename string) error {
|
||||
return WriteConfig(&defaultConfig, filename)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue