diff --git a/cmd/sidecar.go b/cmd/sidecar.go index 0785ace..b9982ea 100644 --- a/cmd/sidecar.go +++ b/cmd/sidecar.go @@ -16,15 +16,20 @@ import ( "github.com/spf13/viper" ) -// StartSidecarCmd creates and returns the sidecar command +// StartSidecarCmd creates and returns the sidecar command. +// It monitors database changes and manages container configurations. func StartSidecarCmd() *cobra.Command { startServer := &cobra.Command{ Use: "sidecar", Short: "Start sidecar", Long: `Starts sidecar to listen for changes in the database and recreate the containers`, - // Initialize configuration before running PreRun: func(cmd *cobra.Command, args []string) { - config.InitConfig() + configPath, err := cmd.Flags().GetString("config") + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + config.InitConfig(configPath) }, RunE: func(cmd *cobra.Command, args []string) error { // Create a cancellable context @@ -66,6 +71,6 @@ func StartSidecarCmd() *cobra.Command { return nil }, } - + startServer.PersistentFlags().StringP("config", "c", "", "Path of the configuration file") return startServer } diff --git a/internal/config/config.go b/internal/config/config.go index 28f9608..e77c59c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,12 +6,19 @@ import ( "github.com/spf13/viper" ) -// InitConfig initializes the application configuration using viper -// It looks for a configuration file named 'local.yaml' in the config directory -func InitConfig() { - viper.AddConfigPath("config") // path to look for the config file in - viper.SetConfigName("local") // name of the config file (without extension) - viper.SetConfigType("yaml") // type of the config file +// InitConfig initializes the application configuration using viper. +// If configPath is provided, it will use that specific file, +// otherwise it will look for 'local.yaml' in the config directory +func InitConfig(configPath string) { + if configPath != "" { + // Use specified config file + viper.SetConfigFile(configPath) + } else { + // Default config location + viper.AddConfigPath("config") + viper.SetConfigName("local") + } + viper.SetConfigType("yaml") // Enable automatic environment variable binding viper.AutomaticEnv()