/
target_command.go
96 lines (74 loc) · 2.16 KB
/
target_command.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
package bmp
import (
"errors"
"net/url"
clients "github.com/cloudfoundry-community/bosh-softlayer-tools/clients"
cmds "github.com/cloudfoundry-community/bosh-softlayer-tools/cmds"
common "github.com/cloudfoundry-community/bosh-softlayer-tools/common"
config "github.com/cloudfoundry-community/bosh-softlayer-tools/config"
)
type targetCommand struct {
args []string
options cmds.Options
ui common.UI
printer common.Printer
bmpClient clients.BmpClient
}
func NewTargetCommand(options cmds.Options, bmpClient clients.BmpClient) targetCommand {
consoleUi := common.NewConsoleUi(options.Verbose)
return targetCommand{
options: options,
ui: consoleUi,
printer: common.NewDefaultPrinter(consoleUi, options.Verbose),
bmpClient: bmpClient,
}
}
func (cmd targetCommand) Name() string {
return "target"
}
func (cmd targetCommand) Description() string {
return "Set the URL of Bare Metal Provision Server"
}
func (cmd targetCommand) Usage() string {
return "bmp target http://url.to.bmp.server"
}
func (cmd targetCommand) Options() cmds.Options {
return cmd.options
}
func (cmd targetCommand) Validate() (bool, error) {
_, err := cmd.printer.Printf("Validating %s command: args: %#v, options: %#v", cmd.Name(), cmd.args, cmd.options)
if err != nil {
return false, err
}
if cmd.options.Target == "" {
return false, errors.New("cannot have empty target")
}
_, err = url.ParseRequestURI(cmd.options.Target)
if err != nil {
return false, err
}
return true, nil
}
func (cmd targetCommand) Execute(args []string) (int, error) {
_, err := cmd.printer.Printf("Executing %s comamnd: args: %#v, options: %#v", cmd.Name(), args, cmd.options)
if err != nil {
return 1, err
}
validate, err := cmd.Validate()
if validate == false && err == nil {
return 1, errors.New("bmp target validation error")
} else if validate == false && err != nil {
return 1, err
}
configInfo, err := common.CreateConfig(cmd.bmpClient.ConfigPath())
if err != nil {
return 1, err
}
configInfo.TargetUrl = cmd.options.Target
c := config.NewConfig(cmd.bmpClient.ConfigPath())
err = c.SaveConfig(configInfo)
if err != nil {
return 1, err
}
return 0, nil
}