Refactor configuration system with centralized type-safe config package
- Create internal/config package with unified config structs and validation - Abstract viper dependency behind config.Loader interface for better testability - Replace manual config parsing and type assertions with type-safe loading - Consolidate AuthConfig, SiteConfig, and DiscoveryConfig into single package - Add comprehensive validation with clear error messages - Remove ~200 lines of duplicate config handling code - Maintain backward compatibility with existing config files
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/insertr/insertr/internal/content"
|
||||
"github.com/insertr/insertr/internal/db"
|
||||
@@ -34,9 +33,6 @@ var (
|
||||
|
||||
func init() {
|
||||
enhanceCmd.Flags().StringVarP(&outputDir, "output", "o", "./dist", "Output directory for enhanced files")
|
||||
|
||||
// Bind flags to viper
|
||||
viper.BindPFlag("cli.output", enhanceCmd.Flags().Lookup("output"))
|
||||
}
|
||||
|
||||
func runEnhance(cmd *cobra.Command, args []string) {
|
||||
@@ -59,20 +55,26 @@ func runEnhance(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
}
|
||||
|
||||
// Get configuration values
|
||||
dbPath := viper.GetString("database.path")
|
||||
apiURL := viper.GetString("api.url")
|
||||
apiKey := viper.GetString("api.key")
|
||||
siteID := viper.GetString("cli.site_id")
|
||||
outputDir := viper.GetString("cli.output")
|
||||
// Load configuration
|
||||
cfg, err := loadConfig()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load configuration: %v", err)
|
||||
}
|
||||
|
||||
// Override with flags if provided
|
||||
if outputDir != "" {
|
||||
// No output config in main config, use the flag value directly
|
||||
} else {
|
||||
outputDir = "./dist" // default
|
||||
}
|
||||
|
||||
// Auto-derive site_id for demo paths or validate for production
|
||||
if strings.Contains(inputPath, "/demos/") || strings.Contains(inputPath, "./demos/") {
|
||||
// Auto-derive site_id from demo path
|
||||
siteID = content.DeriveOrValidateSiteID(inputPath, siteID)
|
||||
cfg.CLI.SiteID = content.DeriveOrValidateSiteID(inputPath, cfg.CLI.SiteID)
|
||||
} else {
|
||||
// Validate site_id for non-demo paths
|
||||
if siteID == "" || siteID == "demo" {
|
||||
if cfg.CLI.SiteID == "" || cfg.CLI.SiteID == "demo" {
|
||||
log.Fatalf(`❌ site_id must be explicitly configured for non-demo sites.
|
||||
|
||||
💡 Examples:
|
||||
@@ -92,12 +94,12 @@ func runEnhance(cmd *cobra.Command, args []string) {
|
||||
|
||||
// Create content client
|
||||
var client engine.ContentClient
|
||||
if apiURL != "" {
|
||||
fmt.Printf("🌐 Using content API: %s\n", apiURL)
|
||||
client = content.NewHTTPClient(apiURL, apiKey)
|
||||
} else if dbPath != "" {
|
||||
fmt.Printf("🗄️ Using database: %s\n", dbPath)
|
||||
database, err := db.NewDatabase(dbPath)
|
||||
if cfg.API.URL != "" {
|
||||
fmt.Printf("🌐 Using content API: %s\n", cfg.API.URL)
|
||||
client = content.NewHTTPClient(cfg.API.URL, cfg.API.Key)
|
||||
} else if cfg.Database.Path != "" {
|
||||
fmt.Printf("🗄️ Using database: %s\n", cfg.Database.Path)
|
||||
database, err := db.NewDatabase(cfg.Database.Path)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize database: %v", err)
|
||||
}
|
||||
@@ -121,41 +123,24 @@ func runEnhance(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
// Override with site-specific discovery config if available
|
||||
if siteConfigs := viper.Get("server.sites"); siteConfigs != nil {
|
||||
if configs, ok := siteConfigs.([]interface{}); ok {
|
||||
for _, configInterface := range configs {
|
||||
if configMap, ok := configInterface.(map[string]interface{}); ok {
|
||||
if configSiteID, ok := configMap["site_id"].(string); ok && configSiteID == siteID {
|
||||
// Found matching site config, load discovery settings
|
||||
if discoveryMap, ok := configMap["discovery"].(map[string]interface{}); ok {
|
||||
if enabled, ok := discoveryMap["enabled"].(bool); ok {
|
||||
enhancementConfig.Discovery.Enabled = enabled
|
||||
fmt.Printf("🔧 Site '%s': discovery.enabled=%v\n", siteID, enabled)
|
||||
}
|
||||
if aggressive, ok := discoveryMap["aggressive"].(bool); ok {
|
||||
enhancementConfig.Discovery.Aggressive = aggressive
|
||||
}
|
||||
if containers, ok := discoveryMap["containers"].(bool); ok {
|
||||
enhancementConfig.Discovery.Containers = containers
|
||||
}
|
||||
if individual, ok := discoveryMap["individual"].(bool); ok {
|
||||
enhancementConfig.Discovery.Individual = individual
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, site := range cfg.Server.Sites {
|
||||
if site.SiteID == cfg.CLI.SiteID && site.Discovery != nil {
|
||||
enhancementConfig.Discovery.Enabled = site.Discovery.Enabled
|
||||
enhancementConfig.Discovery.Aggressive = site.Discovery.Aggressive
|
||||
enhancementConfig.Discovery.Containers = site.Discovery.Containers
|
||||
enhancementConfig.Discovery.Individual = site.Discovery.Individual
|
||||
fmt.Printf("🔧 Site '%s': discovery.enabled=%v\n", cfg.CLI.SiteID, site.Discovery.Enabled)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Create enhancer with loaded configuration
|
||||
enhancer := content.NewEnhancer(client, siteID, enhancementConfig)
|
||||
enhancer := content.NewEnhancer(client, cfg.CLI.SiteID, enhancementConfig)
|
||||
|
||||
fmt.Printf("🚀 Starting enhancement process...\n")
|
||||
fmt.Printf("📁 Input: %s\n", inputPath)
|
||||
fmt.Printf("📁 Output: %s\n", outputDir)
|
||||
fmt.Printf("🏷️ Site ID: %s\n\n", siteID)
|
||||
fmt.Printf("🏷️ Site ID: %s\n\n", cfg.CLI.SiteID)
|
||||
|
||||
// Enhance based on input type
|
||||
if isFile {
|
||||
|
||||
Reference in New Issue
Block a user