forked from cloudfoundry/bosh-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
106 lines (89 loc) · 2.58 KB
/
app.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package application
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/cloudfoundry/bosh-bootloader/commands"
"github.com/cloudfoundry/bosh-bootloader/storage"
)
type CommandSet map[string]commands.Command
type usage interface {
Print()
PrintCommandUsage(command, message string)
}
type App struct {
commands CommandSet
configuration Configuration
stateStore stateStore
usage usage
}
func New(commands CommandSet, configuration Configuration, stateStore stateStore,
usage usage) App {
return App{
commands: commands,
configuration: configuration,
stateStore: stateStore,
usage: usage,
}
}
func (a App) Run() error {
err := a.execute()
if err != nil {
return err
}
return nil
}
func (a App) getCommand(commandString string) (commands.Command, error) {
command, ok := a.commands[commandString]
if !ok {
a.usage.Print()
return nil, fmt.Errorf("unknown command: %s", commandString)
}
return command, nil
}
func (a App) execute() error {
command, err := a.getCommand(a.configuration.Command)
if err != nil {
return err
}
if a.configuration.SubcommandFlags.ContainsAny("--help", "-h") {
a.usage.PrintCommandUsage(a.configuration.Command, command.Usage())
return nil
}
if a.configuration.Command == "help" && len(a.configuration.SubcommandFlags) != 0 {
commandString := a.configuration.SubcommandFlags[0]
command, err = a.getCommand(commandString)
if err != nil {
return err
}
a.usage.PrintCommandUsage(commandString, command.Usage())
return nil
}
if a.configuration.Command == "--version" || a.configuration.Command == "-v" || a.configuration.SubcommandFlags.ContainsAny("--version", "-v") {
versionCommand, err := a.getCommand(commands.VersionCommand)
if err != nil {
return err
}
return versionCommand.Execute([]string{}, storage.State{})
}
err = command.CheckFastFails(a.configuration.SubcommandFlags, a.configuration.State)
if err != nil {
return err
}
err = command.Execute(a.configuration.SubcommandFlags, a.configuration.State)
if err != nil {
switch err.(type) {
case awserr.RequestFailure:
requestFailure := err.(awserr.RequestFailure)
if requestFailure.StatusCode() == 403 {
return errors.New(fmt.Sprintf(
"The AWS credentials provided have insufficient permissions to perform the operation `bbl %s`.\nPlease refer to the bbl README:\nhttps://github.com/cloudfoundry/bosh-bootloader#configure-aws.\nOriginal error message from AWS:\n\n%s",
a.configuration.Command, requestFailure.Message()))
}
return err
default:
return err
}
}
return nil
}