forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
50 lines (40 loc) · 1007 Bytes
/
runner.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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/errors"
biui "github.com/cloudfoundry/bosh-init/ui"
)
type Runner struct {
factory Factory
}
func NewRunner(factory Factory) *Runner {
return &Runner{
factory: factory,
}
}
func (r *Runner) Run(stage biui.Stage, args ...string) error {
args = r.processArgs(args)
commandName := args[0]
cmd, err := r.factory.CreateCommand(commandName)
if err != nil {
return err
}
err = cmd.Run(stage, args[1:])
if err != nil {
return bosherr.WrapErrorf(err, "Command '%s' failed", commandName)
}
return nil
}
func (r *Runner) processArgs(args []string) []string {
if len(args) == 0 {
return []string{"help"}
}
for i, arg := range args {
if arg == "help" || arg == "-h" || arg == "--help" {
return append(append([]string{"help"}, args[:i]...), args[i+1:]...)
}
if arg == "version" || arg == "-v" || arg == "--version" {
return []string{"version"}
}
}
return args
}