forked from aptly-dev/aptly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
47 lines (38 loc) · 819 Bytes
/
run.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
package cmd
import (
"fmt"
"os"
ctx "github.com/aptly-dev/aptly/context"
"github.com/smira/commander"
)
// Run runs single command starting from root cmd with args, optionally initializing context
func Run(cmd *commander.Command, cmdArgs []string, initContext bool) (returnCode int) {
defer func() {
if r := recover(); r != nil {
fatal, ok := r.(*ctx.FatalError)
if !ok {
panic(r)
}
fmt.Fprintln(os.Stderr, "ERROR:", fatal.Message)
returnCode = fatal.ReturnCode
}
}()
returnCode = 0
flags, args, err := cmd.ParseFlags(cmdArgs)
if err != nil {
ctx.Fatal(err)
}
if initContext {
err = InitContext(flags)
if err != nil {
ctx.Fatal(err)
}
defer ShutdownContext()
}
context.UpdateFlags(flags)
err = cmd.Dispatch(args)
if err != nil {
ctx.Fatal(err)
}
return
}