57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
type Portainer struct {
|
|
URL string `toml:"url"`
|
|
Token string `toml:"token"`
|
|
Endpoint string `toml:"endpoint"` // optional name override, defaults to auto-discover
|
|
}
|
|
|
|
type Config struct {
|
|
Portainer Portainer `toml:"portainer"`
|
|
ServicesPath string `toml:"services_path"` // optional, defaults to git root / services
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
path, err := configPath()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var cfg Config
|
|
if _, err := toml.DecodeFile(path, &cfg); err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil, fmt.Errorf("config file not found at %s — run `knecht init` to create one", path)
|
|
}
|
|
return nil, fmt.Errorf("parsing config: %w", err)
|
|
}
|
|
|
|
if cfg.Portainer.URL == "" {
|
|
return nil, fmt.Errorf("portainer.url is required in %s", path)
|
|
}
|
|
if cfg.Portainer.Token == "" {
|
|
return nil, fmt.Errorf("portainer.token is required in %s", path)
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|
|
|
|
func Path() (string, error) {
|
|
return configPath()
|
|
}
|
|
|
|
func configPath() (string, error) {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(home, ".config", "knecht", "config.toml"), nil
|
|
}
|