Checkpoint
This commit is contained in:
138 files changed
+1898
-57607
No files matched your search
+36
-148
@@ -1,179 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"runtime"
|
||||
|
||||
"github.com/adrg/xdg"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"periph.io/x/host/v3"
|
||||
|
||||
"git.maze.io/go/harmon/config"
|
||||
"git.maze.io/go/harmon/display"
|
||||
_ "git.maze.io/go/harmon/display/all" // Load all display drivers
|
||||
)
|
||||
"go.maze.io/harmon"
|
||||
"go.maze.io/harmon/config"
|
||||
|
||||
var (
|
||||
configurationPath = append(makeConfigurationPath(), ".")
|
||||
configurationFile string
|
||||
configuration *config.Structure
|
||||
logFile string
|
||||
logDebug bool
|
||||
logTrace bool
|
||||
_ "go.maze.io/harmon/hardware/display/all" // Display drivers
|
||||
_ "go.maze.io/harmon/player/all" // Player drivers
|
||||
)
|
||||
|
||||
func main() {
|
||||
var entrypoint = &cobra.Command{
|
||||
Use: "harmon",
|
||||
Short: "Harmon is a music player frontend for graphic displays",
|
||||
Args: entrypointArgs,
|
||||
Run: entrypoint,
|
||||
}
|
||||
configFlag := flag.String("config", "", "configuration file path (default: auto detect)")
|
||||
debugFlag := flag.Bool("debug", false, "enable debug log messages")
|
||||
traceFlag := flag.Bool("trace", false, "enable trace log messages (very noisy!)")
|
||||
flag.Parse()
|
||||
|
||||
flag := entrypoint.PersistentFlags()
|
||||
flag.StringVarP(&configurationFile, "config", "c", "", "configuration file (default is look in "+strings.Join(configurationPath, ",")+")")
|
||||
flag.BoolVarP(&logDebug, "debug", "D", false, "enable debug log messages")
|
||||
flag.BoolVarP(&logTrace, "trace", "T", false, "enable trace log messages")
|
||||
flag.StringVarP(&logFile, "log", "l", "", "log file (default: use standard output)")
|
||||
entrypoint.MarkFlagRequired("config")
|
||||
|
||||
if err := entrypoint.Execute(); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func fatal(err error) {
|
||||
fmt.Fprintf(os.Stderr, "Fatal error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func setup() {
|
||||
viper.SetConfigFile("harmon.conf")
|
||||
viper.AddConfigPath(".")
|
||||
viper.SetConfigName(".harmon")
|
||||
viper.SetDefault("license", "MIT")
|
||||
}
|
||||
|
||||
func makeConfigurationPath() []string {
|
||||
dirs := make([]string, len(xdg.ConfigDirs))
|
||||
for i, dir := range xdg.ConfigDirs {
|
||||
dirs[i] = filepath.Join(dir, "harmon")
|
||||
}
|
||||
return dirs
|
||||
}
|
||||
|
||||
func entrypointArgs(cmd *cobra.Command, args []string) (err error) {
|
||||
// Setup logger
|
||||
if logFile != "" {
|
||||
logrus.WithField("path", logFile).Info("logging to file")
|
||||
var w io.Writer
|
||||
if w, err = os.OpenFile(logFile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0640); err != nil {
|
||||
return
|
||||
}
|
||||
logrus.SetOutput(w)
|
||||
}
|
||||
if logTrace {
|
||||
if *traceFlag {
|
||||
logrus.SetLevel(logrus.TraceLevel)
|
||||
} else if logDebug {
|
||||
} else if *debugFlag {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
}
|
||||
|
||||
// Load configuration
|
||||
if configurationFile == "" {
|
||||
for _, dir := range configurationPath {
|
||||
if dir, err = filepath.Abs(dir); err != nil {
|
||||
return
|
||||
}
|
||||
logrus.WithField("path", dir).Trace("looking for harmon.conf")
|
||||
if i, ferr := os.Stat(filepath.Join(dir, "harmon.conf")); ferr == nil && !i.IsDir() {
|
||||
if configurationFile, err = filepath.Abs(i.Name()); err != nil {
|
||||
return
|
||||
}
|
||||
logrus.WithField("path", configurationFile).Trace("using for harmon.conf")
|
||||
break
|
||||
}
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"version": harmon.Version,
|
||||
"host_os": runtime.GOOS,
|
||||
"host_arch": runtime.GOARCH,
|
||||
}).Info("Harmon starting")
|
||||
|
||||
if *configFlag == "" {
|
||||
if *configFlag = findConfigFile(); *configFlag == "" {
|
||||
logrus.Fatal("could not auto detect configuration file; supply one with -config")
|
||||
}
|
||||
}
|
||||
if configuration, err = config.Load(configurationFile); err != nil {
|
||||
if config.ExplainError(configurationFile, err) {
|
||||
return errors.New("configuration failed")
|
||||
}
|
||||
return err
|
||||
|
||||
config, err := config.Load(*configFlag)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatal("configuration file contains errors")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func entrypoint(cmd *cobra.Command, args []string) {
|
||||
if len(configuration.Display) == 0 {
|
||||
logrus.Fatal("no displays configured")
|
||||
}
|
||||
|
||||
var (
|
||||
displays = make(map[string]display.Display)
|
||||
err error
|
||||
)
|
||||
|
||||
teardown := func() {
|
||||
for _, d := range displays {
|
||||
logrus.WithField("display", d.String()).Debug("halting...")
|
||||
if d != nil {
|
||||
_ = d.Halt()
|
||||
}
|
||||
}
|
||||
}
|
||||
defer teardown()
|
||||
|
||||
// Configure host
|
||||
if _, err = host.Init(); err != nil {
|
||||
controller, err := harmon.NewController(config)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatal("host setup failed")
|
||||
}
|
||||
|
||||
// Configure displays
|
||||
for _, config := range configuration.Display {
|
||||
var (
|
||||
log = logrus.WithFields(logrus.Fields{
|
||||
"name": config.Name,
|
||||
"type": config.Type,
|
||||
})
|
||||
output display.Display
|
||||
)
|
||||
log.Info("setting up display")
|
||||
if output, err = display.New(config); err != nil {
|
||||
log.WithError(err).Fatal("display setup failed")
|
||||
}
|
||||
|
||||
if err = display.DrawImageFile(output, "harmon-color.png"); err != nil {
|
||||
logrus.
|
||||
WithField("display", config.Name).
|
||||
WithError(err).
|
||||
Fatal("display splash failed")
|
||||
}
|
||||
if err = output.Refresh(); err != nil {
|
||||
logrus.
|
||||
WithField("display", config.Name).
|
||||
WithError(err).
|
||||
Fatal("display refresh failed")
|
||||
}
|
||||
|
||||
displays[config.Name] = output
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
signals := make(chan os.Signal)
|
||||
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)
|
||||
|
||||
fmt.Println("idling...")
|
||||
for {
|
||||
s := <-signals
|
||||
logrus.WithField("signal", s.String()).Info("terminating")
|
||||
return
|
||||
if err = controller.Run(ctx); err != nil && err != context.Canceled {
|
||||
logrus.WithError(err).Fatal("terminating by fatal error")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user