39 lines
853 B
Go
39 lines
853 B
Go
package domain
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// Config struct for ~/.redmine-tree.json
|
|
type Config struct {
|
|
Host string `json:"host"`
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
// Load configuration from ~/.redmine-tree.json
|
|
func LoadConfig() (*Config, error) {
|
|
homeDir, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not get user home directory: %w", err)
|
|
}
|
|
|
|
configPath := fmt.Sprintf("%s/.redmine-tree.json", homeDir)
|
|
file, err := os.Open(configPath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil, nil // File does not exist, not an error
|
|
}
|
|
return nil, fmt.Errorf("could not open config file: %w", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
var config Config
|
|
if err := json.NewDecoder(file).Decode(&config); err != nil {
|
|
return nil, fmt.Errorf("could not decode config file: %w", err)
|
|
}
|
|
|
|
return &config, nil
|
|
}
|