-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (68 loc) · 1.53 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
76
77
78
79
80
81
package main
import (
"context"
"errors"
"os"
"github.com/alecthomas/kong"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/run"
"github.com/kakkoyun/subshells/pkg/logger"
)
var (
version string
commit string
date string
builtBy string
)
type Flags struct {
LogLevel string `default:"info" enum:"error,warn,info,debug" help:"log level."`
}
func main() {
flags := &Flags{}
_ = kong.Parse(flags)
var g run.Group
logger := logger.NewLogger(flags.LogLevel, logger.LogFormatLogfmt, "infiniteloop")
level.Debug(logger).Log("msg", "subshells initialized",
"version", version,
"commit", commit,
"date", date,
"builtBy", builtBy,
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
g.Add(func() error {
level.Info(logger).Log("msg", "starting the loop")
for {
select {
case <-ctx.Done():
level.Debug(logger).Log("msg", "context done")
return nil
default:
iter(ctx, logger)
}
}
}, func(err error) {
cancel()
})
g.Add(run.SignalHandler(ctx, os.Interrupt, os.Kill))
if err := g.Run(); err != nil {
var e run.SignalError
if errors.As(err, &e) {
level.Error(logger).Log("msg", "program exited with signal", "err", err, "signal", e.Signal)
} else {
level.Error(logger).Log("msg", "program exited with error", "err", err)
}
os.Exit(1)
}
level.Info(logger).Log("msg", "exited")
}
//go:noinline
func iter(ctx context.Context, logger log.Logger) {
select {
case <-ctx.Done():
return
default:
}
level.Debug(logger).Log("msg", "looping")
}