Jonathan Apodaca dac1a06e1a
All checks were successful
CI / build (push) Successful in 27s
add README
2025-05-11 18:42:51 -06:00

67 lines
1.7 KiB
Go

package config
import (
"os"
"path/filepath"
"time"
"git.jrop.me/jonathan/envvault/internal"
"github.com/BurntSushi/toml"
)
// Config represents the application configuration
type Config struct {
Daemon DaemonConfig `toml:"daemon"`
}
// DaemonConfig contains daemon-specific configuration
type DaemonConfig struct {
Timeout string `toml:"timeout"`
}
// GetDaemonTimeout returns the daemon timeout from config or the default value
func GetDaemonTimeout(defaultTimeout time.Duration) time.Duration {
config := loadConfig()
if config.Daemon.Timeout == "" {
internal.Log.Printf("No daemon timeout in config, using default: %s", defaultTimeout)
return defaultTimeout
}
duration, err := time.ParseDuration(config.Daemon.Timeout)
if err != nil {
internal.Log.Printf("Invalid timeout format in config: %s, using default: %s",
config.Daemon.Timeout, defaultTimeout)
return defaultTimeout
}
internal.Log.Printf("Using daemon timeout from config: %s", duration)
return duration
}
// loadConfig loads the configuration from the config file
func loadConfig() Config {
homeDir, err := os.UserHomeDir()
if err != nil {
internal.Log.Printf("Failed to get user home directory: %v", err)
return Config{}
}
configPath := filepath.Join(homeDir, ".config/envvault/envvault.toml")
// Check if config file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
internal.Log.Printf("🔍 Config file not found at %s", configPath)
return Config{}
}
internal.Log.Printf("📂 Loading config from %s", configPath)
var config Config
if _, err := toml.DecodeFile(configPath, &config); err != nil {
internal.Log.Printf("Failed to decode config file: %v", err)
return Config{}
}
return config
}