Skip to content

Commit

Permalink
Add command-line flag parsing for output mode in main function
Browse files Browse the repository at this point in the history
Signed-off-by: devhindo <[email protected]>
  • Loading branch information
devhindo committed Jan 7, 2025
1 parent 0aec3cf commit c38abce
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
3 changes: 3 additions & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ var (

func init() {
flag.StringVar(&OutputMode, "output", "nats", "Output mode: 'file' or 'nats'")

// Parse the command=line flags to get the output mode
flag.Parse()
}
type PipelineConfigs []PipelineConfig

Expand Down
77 changes: 42 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,45 +102,52 @@ func main() {

// check if output mode is nats
if config.OutputMode == "nats" {
connectivityTest(cfg.GetKey(config.BrokerURL), log)
}

// Initialize Broker instance
br, err := nats.New(nats.Options{
URLS: []string{cfg.GetKey(config.BrokerURL)},
ConnectionName: "meshsync",
Username: "",
Password: "",
ReconnectWait: 2 * time.Second,
MaxReconnect: 60,
})
if err != nil {
log.Error(err)
os.Exit(1)
}
log.Info("NATS output mode selected")

chPool := channels.NewChannelPool()
meshsyncHandler, err := meshsync.New(cfg, log, br, chPool)
if err != nil {
log.Error(err)
os.Exit(1)
connectivityTest(cfg.GetKey(config.BrokerURL), log)

// Initialize Broker instance
br, err := nats.New(nats.Options{
URLS: []string{cfg.GetKey(config.BrokerURL)},
ConnectionName: "meshsync",
Username: "",
Password: "",
ReconnectWait: 2 * time.Second,
MaxReconnect: 60,
})
if err != nil {
log.Error(err)
os.Exit(1)
}

chPool := channels.NewChannelPool()
meshsyncHandler, err := meshsync.New(cfg, log, br, chPool)
if err != nil {
log.Error(err)
os.Exit(1)
}

go meshsyncHandler.WatchCRDs()

go meshsyncHandler.Run()
go meshsyncHandler.ListenToRequests()

log.Info("Server started")
// Handle graceful shutdown
signal.Notify(chPool[channels.OS].(channels.OSChannel), syscall.SIGTERM, os.Interrupt)
select {
case <-chPool[channels.OS].(channels.OSChannel):
close(chPool[channels.Stop].(channels.StopChannel))
log.Info("Shutting down")
case <-chPool[channels.Stop].(channels.StopChannel):
close(chPool[channels.Stop].(channels.StopChannel))
log.Info("Shutting down")
}
}

go meshsyncHandler.WatchCRDs()

go meshsyncHandler.Run()
go meshsyncHandler.ListenToRequests()

log.Info("Server started")
// Handle graceful shutdown
signal.Notify(chPool[channels.OS].(channels.OSChannel), syscall.SIGTERM, os.Interrupt)
select {
case <-chPool[channels.OS].(channels.OSChannel):
close(chPool[channels.Stop].(channels.StopChannel))
log.Info("Shutting down")
case <-chPool[channels.Stop].(channels.StopChannel):
close(chPool[channels.Stop].(channels.StopChannel))
log.Info("Shutting down")
if config.OutputMode == "file" {
log.Info("File output mode is not implemented yet")
}
}

Expand Down
3 changes: 0 additions & 3 deletions meshsync/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path/filepath"
"time"
"os"
"flag"

"github.com/layer5io/meshkit/broker"
"github.com/layer5io/meshkit/utils"
Expand Down Expand Up @@ -73,8 +72,6 @@ func (h *Handler) UpdateInformer() error {

func (h *Handler) ListenToRequests() {

// Parse the command=line flags to get the output mode
flag.Parse()


listenerConfigs := make(map[string]config.ListenerConfig, 10)
Expand Down

0 comments on commit c38abce

Please sign in to comment.