Added bento :)
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Connection struct {
|
||||
Name string `yaml:"name"`
|
||||
URL string `yaml:"url"`
|
||||
Username string `yaml:"username"`
|
||||
Token string `yaml:"token"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Connections []Connection `yaml:"connections"`
|
||||
}
|
||||
|
||||
func GetConfigPath() (string, error) {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dir := filepath.Dir(exe)
|
||||
wrappedDir := filepath.Join(dir, ".wrapped")
|
||||
if err := os.MkdirAll(wrappedDir, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(wrappedDir, "config.yaml"), nil
|
||||
}
|
||||
|
||||
func GetResultsDir() (string, error) {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dir := filepath.Dir(exe)
|
||||
resultsDir := filepath.Join(dir, ".wrapped", "results")
|
||||
if err := os.MkdirAll(resultsDir, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return resultsDir, nil
|
||||
}
|
||||
|
||||
func LoadConfig() (*Config, error) {
|
||||
configPath, err := GetConfigPath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return &Config{Connections: []Connection{}}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func (c *Config) Save() error {
|
||||
configPath, err := GetConfigPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := yaml.Marshal(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(configPath, data, 0o600)
|
||||
}
|
||||
|
||||
func (c *Config) GetConnection(name string) *Connection {
|
||||
for i := range c.Connections {
|
||||
if c.Connections[i].Name == name {
|
||||
return &c.Connections[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) AddConnection(conn Connection) {
|
||||
c.Connections = append(c.Connections, conn)
|
||||
}
|
||||
Reference in New Issue
Block a user