Skip to content

Commit

Permalink
fix(evpn-bridge): revisions after review
Browse files Browse the repository at this point in the history
Signed-off-by: Atul Patel <Atul.Patel@intel.com>
  • Loading branch information
atulpatel261194 authored and sandersms committed Apr 25, 2024
1 parent 522b415 commit 0ad2a00
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
22 changes: 9 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var rootCmd = &cobra.Command{

err := infradb.NewInfraDB(config.GlobalConfig.DBAddress, config.GlobalConfig.Database)
if err != nil {
exit(err)
log.Panicf("Error: %v", err)
}
go runGatewayServer(config.GlobalConfig.GRPCPort, config.GlobalConfig.HTTPPort)

Expand All @@ -72,12 +72,12 @@ var rootCmd = &cobra.Command{
ci_linux.Init()
frr.Init()
default:
log.Fatal(" ERROR: Could not find Build env ")
log.Panic(" ERROR: Could not find Build env ")
}

// Create GRD VRF configuration during startup
if err := createGrdVrf(); err != nil {
exit(err)
log.Panicf("Error: %v", err)
}

runGrpcServer(config.GlobalConfig.GRPCPort, config.GlobalConfig.TLSFiles)
Expand All @@ -96,6 +96,7 @@ func initialize() error {
rootCmd.PersistentFlags().StringVar(&config.GlobalConfig.FRRAddress, "frraddress", "127.0.0.1", "Frr address in ip_address format, no port")
rootCmd.PersistentFlags().StringVar(&config.GlobalConfig.Database, "database", "redis", "Database connection string")

// Bind command-line flags to config fields
if err := viper.GetViper().BindPFlags(rootCmd.PersistentFlags()); err != nil {
log.Printf("Error binding flags to Viper: %v\n", err)
return err
Expand All @@ -114,17 +115,12 @@ func setupLogger(filename string) {
filename = filepath.Clean(filename)
out, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
log.Fatal(err)
log.Panic(err)
}
logger = log.New(io.MultiWriter(out, os.Stdout), "", log.Lshortfile|log.LstdFlags)
logger = log.New(io.MultiWriter(out), "", log.Lshortfile|log.LstdFlags)
log.SetOutput(logger.Writer())
}

func exit(e error) {
log.Println("error while running opi-evpn-bridge", e.Error())
os.Exit(1)
}

// main function
func main() {
// setup file and console logger
Expand All @@ -133,17 +129,17 @@ func main() {
// initialize cobra config
if err := initialize(); err != nil {
// log.Println(err)
exit(err)
log.Panicf("Error in initialize(): %v", err)
}

// start the main cmd
if err := rootCmd.Execute(); err != nil {
exit(err)
log.Panicf("Error in Execute(): %v", err)
}

defer func() {
if err := infradb.Close(); err != nil {
exit(err)
log.Panicf("Error in close(): %v", err)
}
}()
}
Expand Down
18 changes: 15 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,30 @@ func Initcfg() {
viper.SetConfigType("yaml")
viper.SetConfigName("config.yaml")
}

if err := LoadConfig(); err != nil {
log.Fatal(err)
log.Panic(err)
}
}

// LoadConfig loads the config from yaml file
func LoadConfig() error {
if err := viper.ReadInConfig(); err != nil {
log.Println("Using config file:", viper.ConfigFileUsed())
return err
// Handle errors
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Cfg file not found
fmt.Println("Config file not found in default paths. Using defaults.")
} else if _, ok := err.(viper.ConfigParseError); ok {
// Cfg file parsing error
log.Panicf("Error parsing config file: %v", err)
} else {
// Other errors
log.Panicf("Error reading config file: %v", err)
}
}

fmt.Println("Using config file:", viper.ConfigFileUsed())

if err := viper.Unmarshal(&GlobalConfig); err != nil {
log.Println(err)
return err
Expand Down

0 comments on commit 0ad2a00

Please sign in to comment.