tidying up a *lot*; add QoL options
This commit is contained in:
parent
84de96df31
commit
2954689784
6 changed files with 261 additions and 154 deletions
56
config/config.go
Normal file
56
config/config.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue