-
Notifications
You must be signed in to change notification settings - Fork 687
/
busy.go
92 lines (78 loc) · 2.25 KB
/
busy.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
82
83
84
85
86
87
88
89
90
91
92
// Package busy implements a dispatcher for BusyBox-style multi-call binaries.
package busy
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"github.com/sirupsen/logrus"
"github.com/datawire/dlib/dlog"
)
type Command struct {
Setup func()
Run func(ctx context.Context, version string, args ...string) error
}
var logrusLogger *logrus.Logger
func init() {
logrusLogger = logrus.New()
logrusFormatter := &logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
}
logrusLogger.SetFormatter(logrusFormatter)
logrusLogger.SetReportCaller(true)
}
func SetLogLevel(lvl logrus.Level) {
logrusLogger.SetLevel(lvl)
}
func GetLogLevel() logrus.Level {
return logrusLogger.GetLevel()
}
var rootLogger dlog.Logger
func GetRootLogger() dlog.Logger {
return rootLogger
}
func Main(binName, humanName string, version string, cmds map[string]Command) {
name := filepath.Base(os.Args[0])
if name == binName && len(os.Args) > 1 {
name = os.Args[1]
os.Args = os.Args[1:]
}
cmd, cmdOk := cmds[name]
if cmdOk {
cmd.Setup()
}
rootLogger = dlog.WrapLogrus(logrusLogger).
WithField("PID", os.Getpid()).
WithField("CMD", name)
ctx := dlog.WithLogger(context.Background(), rootLogger)
dlog.SetFallbackLogger(rootLogger.WithField("oops-i-did-not-pass-context-correctly", true))
if cmdOk {
if err := cmd.Run(ctx, version, os.Args[1:]...); err != nil {
dlog.Errorf(ctx, "shut down with error error: %v", err)
os.Exit(1)
}
} else {
fmt.Printf("The %s main program is a multi-call binary that combines various\n", humanName)
fmt.Println("support programs into one executable.")
fmt.Println()
fmt.Printf("Usage: %s <PROGRAM> [arguments]...\n", binName)
fmt.Println(" or: <PROGRAM> [arguments]...")
fmt.Println()
cmdnames := make([]string, 0, len(cmds))
for cmdname := range cmds {
cmdnames = append(cmdnames, cmdname)
}
sort.Strings(cmdnames)
fmt.Println("Available programs:", cmdnames)
fmt.Println()
fmt.Printf("Unknown program %q\n", name)
// POSIX says the shell should set $?=127 for "command
// not found", so non-shell programs that just run a
// command for you (including busybox) tend to mimic
// that and use exit code 127 to indicate "command not
// found".
os.Exit(127)
}
}