75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
ProjectsPath string `mapstructure:"projects_path"`
|
|
LogLevel string `mapstructure:"log_level"`
|
|
LogFilePath string `mapstructure:"log_file_path"`
|
|
DeploymentPath string `mapstructure:"deployment_path"`
|
|
}
|
|
|
|
func LoadConfig() (*Config, error) {
|
|
configDir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
configPath := filepath.Join(configDir, "jws_gui")
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigType("toml")
|
|
viper.AddConfigPath(configPath)
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
// Config file not found, create default config
|
|
return createDefaultConfig(configPath)
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
var config Config
|
|
if err := viper.Unmarshal(&config); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &config, nil
|
|
}
|
|
|
|
func createDefaultConfig(configPath string) (*Config, error) {
|
|
configDir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
homeDir, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defaultConfig := Config{
|
|
ProjectsPath: filepath.Join(configDir, "jws_gui", "projects"),
|
|
DeploymentPath: filepath.Join(homeDir, "Projects"),
|
|
LogLevel: "info",
|
|
LogFilePath: filepath.Join(configDir, "jws_gui", "logs", "jws_gui.log"),
|
|
}
|
|
|
|
if err := os.MkdirAll(configPath, 0755); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
viper.Set("projects_path", defaultConfig.ProjectsPath)
|
|
viper.Set("deployment_path", defaultConfig.DeploymentPath)
|
|
viper.Set("log_file_path", defaultConfig.LogFilePath)
|
|
viper.Set("log_level", defaultConfig.LogLevel)
|
|
|
|
if err := viper.WriteConfigAs(filepath.Join(configPath, "config.toml")); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &defaultConfig, nil
|
|
// ... (Rest der Funktion bleibt gleich)
|
|
}
|