mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
rename cmd
This commit is contained in:
72
cmd/showbridge/main.go
Normal file
72
cmd/showbridge/main.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"github.com/jwetzell/showbridge-go"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cmd := &cli.Command{
|
||||
Name: "showbridge",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "config",
|
||||
Value: "./config.json",
|
||||
},
|
||||
},
|
||||
Action: func(ctx context.Context, c *cli.Command) error {
|
||||
configPath := c.String("config")
|
||||
if configPath == "" {
|
||||
return fmt.Errorf("config value cannot be empty")
|
||||
}
|
||||
|
||||
config, err := readConfig(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(ctx, config)
|
||||
for _, moduleError := range moduleErrors {
|
||||
slog.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
||||
}
|
||||
|
||||
for _, routeError := range routeErrors {
|
||||
slog.Error("problem initializing module", "index", routeError.Index, "error", routeError.Error)
|
||||
}
|
||||
router.Run()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Interrupt)
|
||||
defer cancel()
|
||||
err := cmd.Run(ctx, os.Args)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func readConfig(configPath string) (showbridge.Config, error) {
|
||||
configBytes, err := os.ReadFile(configPath)
|
||||
|
||||
if err != nil {
|
||||
return showbridge.Config{}, err
|
||||
}
|
||||
|
||||
config := showbridge.Config{}
|
||||
|
||||
err = json.Unmarshal(configBytes, &config)
|
||||
if err != nil {
|
||||
return showbridge.Config{}, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
Reference in New Issue
Block a user