forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
66 lines (56 loc) · 1.39 KB
/
register.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
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package bbmain is the package imported by all rewritten busybox
// command-packages to register themselves.
package bbmain
import (
"errors"
"fmt"
"os"
)
// ErrNotRegistered is returned by Run if the given command is not registered.
var ErrNotRegistered = errors.New("command not registered")
// Noop is a noop function.
var Noop = func() {}
type bbCmd struct {
init, main func()
}
var bbCmds = map[string]bbCmd{}
var defaultCmd *bbCmd
// Register registers an init and main function for name.
func Register(name string, init, main func()) {
if _, ok := bbCmds[name]; ok {
panic(fmt.Sprintf("cannot register two commands with name %q", name))
}
bbCmds[name] = bbCmd{
init: init,
main: main,
}
}
// RegisterDefault registers a default init and main function.
func RegisterDefault(init, main func()) {
defaultCmd = &bbCmd{
init: init,
main: main,
}
}
// Run runs the command with the given name.
//
// If the command's main exits without calling os.Exit, Run will exit with exit
// code 0.
func Run(name string) error {
var cmd *bbCmd
if c, ok := bbCmds[name]; ok {
cmd = &c
} else if defaultCmd != nil {
cmd = defaultCmd
} else {
return ErrNotRegistered
}
cmd.init()
cmd.main()
os.Exit(0)
// Unreachable.
return nil
}