-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (60 loc) · 1.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"flag"
"fmt"
"github.com/wwwil/launchpoint/pkg/gpio"
"github.com/wwwil/launchpoint/pkg/launchpoint"
"log"
"os"
"os/signal"
"sync"
"syscall"
)
// Version is the version of the app. This is injected during build.
var Version = "development"
// Commit is the commit hash of the build. This is injected during build.
var Commit string
// BuildDate is the date of the build. This is injected during build.
var BuildDate string
// GoVersion is the Go version used for the build. This is injected during build.
var GoVersion string
// Platform is the target platform for this build. This is injected during build.
var Platform string
// Variables set by argument flags.
var configFilePath = flag.String("config", "./launchpoint.yaml", "Path to the configuration file")
func main() {
// Don't show time and date on log messages.
log.SetFlags(0)
// Print build information.
versionString := "Launchpoint"
versionString = fmt.Sprintf("%s\n Version: %s %s", versionString, Version, Platform)
versionString = fmt.Sprintf("%s\n Commit: %s", versionString, Commit)
versionString = fmt.Sprintf("%s\n Built: %s", versionString, BuildDate)
versionString = fmt.Sprintf("%s\n Go: %s", versionString, GoVersion)
log.Println(versionString)
// Parse command line flags.
flag.Parse()
config, err := launchpoint.LoadConfigFromFile(*configFilePath)
if err != nil {
log.Fatal(err)
}
log.Printf("using configuration file: %s\n", *configFilePath)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
// Start listeners.
wg.Add(1)
go gpio.Run(ctx, &wg, config)
// Capture exit signals to ensure resources are released on exit.
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(quit)
// Run until told to quit.
<-quit
log.Println("Launchpoint will now quit.")
// Cancel the context to stop the other threads.
cancel()
// Wait for other threads to finish.
wg.Wait()
}