-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
67 lines (54 loc) · 1.19 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
package main
import (
"context"
"fmt"
"hash/maphash"
"io"
"math/rand"
"os"
"path/filepath"
"runtime/trace"
"github.com/frioux/leatherman/internal/selfupdate"
)
//go:generate maint/generate
// Dispatch is the dispatch table that maps command names to functions.
var Dispatch map[string]func([]string, io.Reader) error
// run returns false when an error occurred
func run() bool {
startDebug()
defer stopDebug()
h := &maphash.Hash{}
h.WriteByte(byte(os.Getpid()))
h.WriteByte(byte(os.Getppid()))
if n, err := os.Hostname(); err == nil {
h.WriteString(n)
}
rand.Seed(int64(h.Sum64()))
selfupdate.AutoUpdate()
which := filepath.Base(os.Args[0])
args := os.Args
Dispatch["xyzzy"] = func([]string, io.Reader) error { fmt.Println("nothing happens"); return nil }
if _, ok := Dispatch[which]; !ok && len(args) > 1 {
args = args[1:]
which = args[0]
}
fn, ok := Dispatch[which]
if !ok {
_ = Help(os.Args, os.Stdin)
return false
}
var err error
trace.WithRegion(context.Background(), which, func() {
err = fn(args, os.Stdin)
})
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", which, err)
return false
}
return true
}
func main() {
if !run() {
os.Exit(1)
}
}