86 lines
2.1 KiB
Go
86 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"runtime"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli/v3"
|
|
|
|
"go.maze.io/harmon"
|
|
"go.maze.io/harmon/config"
|
|
|
|
_ "go.maze.io/harmon/hardware/display/all" // Display drivers
|
|
_ "go.maze.io/harmon/player/all" // Player drivers
|
|
)
|
|
|
|
func main() {
|
|
cmd := &cli.Command{
|
|
Name: "harmon",
|
|
Usage: "Harmon is a playful music player frontend",
|
|
Action: runFunc,
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{Name: "config", Usage: "configuration file path"},
|
|
&cli.BoolFlag{Name: "debug", Usage: "enable debug log messages"},
|
|
&cli.BoolFlag{Name: "trace", Usage: "enable trace log messages (noisy!)"},
|
|
},
|
|
Commands: []*cli.Command{
|
|
scanCommand,
|
|
},
|
|
}
|
|
|
|
if err := cmd.Run(context.Background(), os.Args); err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func runFunc(ctx context.Context, cmd *cli.Command) error {
|
|
if cmd.Bool("trace") {
|
|
logrus.SetLevel(logrus.TraceLevel)
|
|
} else if cmd.Bool("debug") {
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
}
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
"version": harmon.Version,
|
|
"host_os": runtime.GOOS,
|
|
"host_arch": runtime.GOARCH,
|
|
}).Info("Harmon starting")
|
|
|
|
configFile := cmd.String("config")
|
|
if configFile == "" {
|
|
if configFile = findConfigFile(); configFile == "" {
|
|
logrus.Fatal("could not auto detect configuration file; supply one with -config")
|
|
}
|
|
}
|
|
|
|
config, err := config.Load(configFile)
|
|
if err != nil {
|
|
logrus.WithError(err).Fatal("configuration file contains errors")
|
|
}
|
|
|
|
controller, err := harmon.NewController(config)
|
|
if err != nil {
|
|
logrus.WithError(err).Fatal("host setup failed")
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
signal.Notify(signals, os.Interrupt)
|
|
signal.Notify(signals, os.Kill)
|
|
go func(signals <-chan os.Signal, cancel func()) {
|
|
defer cancel()
|
|
signal := <-signals
|
|
logrus.WithField("signal", signal).Warn("terminating by signal")
|
|
}(signals, cancel)
|
|
|
|
if err = controller.Run(ctx); err != nil && err != context.Canceled {
|
|
//logrus.WithError(err).Fatal("terminating by fatal error")
|
|
return err
|
|
}
|
|
return nil
|
|
}
|