feat: add config and structured logging

This commit is contained in:
Patryk Hegenberg 2025-03-19 21:58:39 +01:00
parent 902a8bb7d1
commit fe83dc1f33
9 changed files with 275 additions and 31 deletions

75
internal/config/config.go Normal file
View file

@ -0,0 +1,75 @@
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)
}