First working version
This commit is contained in:
82
internal/config/config.go
Normal file
82
internal/config/config.go
Normal file
@ -0,0 +1,82 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
// Config represents the application configuration
|
||||
type Config struct {
|
||||
DBHost string `toml:"db_host"`
|
||||
DBPort int `toml:"db_port"`
|
||||
DBName string `toml:"db_name"`
|
||||
DBSchema string `toml:"db_schema"`
|
||||
DBUser string `toml:"db_user"`
|
||||
DBPassword string `toml:"db_password"`
|
||||
DBTable string `toml:"db_table"`
|
||||
OutputDir string `toml:"output_dir"`
|
||||
EntityName string `toml:"entity_name"`
|
||||
}
|
||||
|
||||
// DefaultConfig returns a new config with default values
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
DBHost: "localhost",
|
||||
DBPort: 5432,
|
||||
DBSchema: "public",
|
||||
DBUser: "postgres",
|
||||
DBPassword: "postgres",
|
||||
}
|
||||
}
|
||||
|
||||
// Load reads the configuration file from ~/.config/entity-maker.toml
|
||||
func Load() (*Config, error) {
|
||||
configPath := getConfigPath()
|
||||
|
||||
config := DefaultConfig()
|
||||
|
||||
// Check if config file exists
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// Read and decode the config file
|
||||
if _, err := toml.DecodeFile(configPath, config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// Save writes the configuration to ~/.config/entity-maker.toml
|
||||
func (c *Config) Save() error {
|
||||
configPath := getConfigPath()
|
||||
|
||||
// Create config directory if it doesn't exist
|
||||
configDir := filepath.Dir(configPath)
|
||||
if err := os.MkdirAll(configDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create or truncate the config file
|
||||
f, err := os.Create(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// Encode and write the config
|
||||
encoder := toml.NewEncoder(f)
|
||||
return encoder.Encode(c)
|
||||
}
|
||||
|
||||
// getConfigPath returns the full path to the config file
|
||||
func getConfigPath() string {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
homeDir = "."
|
||||
}
|
||||
return filepath.Join(homeDir, ".config", "entity-maker.toml")
|
||||
}
|
||||
Reference in New Issue
Block a user