Skip to content

Commit

Permalink
Add support for custom config file path
Browse files Browse the repository at this point in the history
  • Loading branch information
spilin committed Dec 18, 2024
1 parent 6491f1a commit 04e976f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
13 changes: 9 additions & 4 deletions cmd/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,6 +71,6 @@ func StartSidecarCmd() *cobra.Command {
return nil
},
}

startServer.PersistentFlags().StringP("config", "c", "", "Path of the configuration file")
return startServer
}
19 changes: 13 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 04e976f

Please sign in to comment.