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:
47
cmd/root.go
47
cmd/root.go
@@ -4,16 +4,17 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/insertr/insertr/internal/config"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
cfgFile string
|
||||
dbPath string
|
||||
apiURL string
|
||||
apiKey string
|
||||
siteID string
|
||||
configFile string
|
||||
dbPath string
|
||||
apiURL string
|
||||
apiKey string
|
||||
siteID string
|
||||
loader config.Loader
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
@@ -35,40 +36,22 @@ func Execute() {
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
loader = config.NewLoader()
|
||||
|
||||
// Global flags
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./insertr.yaml)")
|
||||
rootCmd.PersistentFlags().StringVar(&dbPath, "db", "./insertr.db", "database path (SQLite file or PostgreSQL connection string)")
|
||||
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is ./insertr.yaml)")
|
||||
rootCmd.PersistentFlags().StringVar(&dbPath, "db", "", "database path (SQLite file or PostgreSQL connection string)")
|
||||
rootCmd.PersistentFlags().StringVar(&apiURL, "api-url", "", "content API URL")
|
||||
rootCmd.PersistentFlags().StringVar(&apiKey, "api-key", "", "API key for authentication")
|
||||
rootCmd.PersistentFlags().StringVarP(&siteID, "site-id", "s", "demo", "site ID for content lookup")
|
||||
|
||||
// Bind flags to viper
|
||||
viper.BindPFlag("database.path", rootCmd.PersistentFlags().Lookup("db"))
|
||||
viper.BindPFlag("api.url", rootCmd.PersistentFlags().Lookup("api-url"))
|
||||
viper.BindPFlag("api.key", rootCmd.PersistentFlags().Lookup("api-key"))
|
||||
viper.BindPFlag("cli.site_id", rootCmd.PersistentFlags().Lookup("site-id"))
|
||||
rootCmd.PersistentFlags().StringVarP(&siteID, "site-id", "s", "", "site ID for content lookup")
|
||||
|
||||
rootCmd.AddCommand(enhanceCmd)
|
||||
rootCmd.AddCommand(serveCmd)
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
viper.AddConfigPath(".")
|
||||
viper.SetConfigName("insertr")
|
||||
viper.SetConfigType("yaml")
|
||||
}
|
||||
|
||||
// Environment variables
|
||||
viper.SetEnvPrefix("INSERTR")
|
||||
viper.AutomaticEnv()
|
||||
|
||||
// Read config file
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
func loadConfig() (*config.Config, error) {
|
||||
if configFile != "" || dbPath != "" || apiURL != "" || apiKey != "" || siteID != "" {
|
||||
return loader.LoadWithFlags(dbPath, apiURL, apiKey, siteID)
|
||||
}
|
||||
return loader.Load(configFile)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user