forked from bmatcuk/go-vagrant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_box_add.go
101 lines (86 loc) · 2.4 KB
/
command_box_add.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
package vagrant
import "errors"
type CheckSumType string
const (
MD5 CheckSumType = "md5"
SHA1 CheckSumType = "sha1"
SHA256 CheckSumType = "sha256"
)
// A BoxAddCommand specifies the options and output of vagrant box add.
type BoxAddCommand struct {
BaseCommand
ErrorResponse
// Clean any temporary download files. This will prevent resuming a
// previously started download.
Clean bool
// Overwrite an existing box if it exists
Force bool
// Name, url, or path of the box.
Location string
// Name of the box. Only has to be set if the box is provided as a path or
// url without metadata.
Name string
// Checksum for the box
Checksum string
// Type of the supplied Checksum. Allowed values are vagrant.MD5,
// vagrant.SHA1, vagrant.SHA256
CheckSumType CheckSumType
}
// BoxAdd adds a vagrant box to your local boxes. Its parameter location is
// the name, url, or path of the box. After setting options as appropriate, you
// must call Run() or Start() followed by Wait() to execute. Errors will be
// recorded in Error.
func (client *VagrantClient) BoxAdd(location string) *BoxAddCommand {
return &BoxAddCommand{
Location: location,
BaseCommand: newBaseCommand(client),
ErrorResponse: newErrorResponse(),
}
}
func (cmd *BoxAddCommand) buildArguments() ([]string, error) {
args := []string{"add"}
if cmd.Clean {
args = append(args, "-c")
}
if cmd.Force {
args = append(args, "-f")
}
if cmd.Name != "" {
args = append(args, "--name", cmd.Name)
}
if cmd.Checksum != "" {
args = append(args, "--checksum", cmd.Checksum)
}
if cmd.CheckSumType != "" {
if cmd.Checksum == "" {
return nil, errors.New("box add: no checksum set even though checksum type was set")
}
args = append(args, "--checksum-type", string(cmd.CheckSumType))
}
if cmd.Location == "" {
return nil, errors.New("box add: location must be provided")
}
args = append(args, cmd.Location)
return args, nil
}
func (cmd *BoxAddCommand) init() error {
args, err := cmd.buildArguments()
if err != nil {
return err
}
return cmd.BaseCommand.init(&cmd.ErrorResponse, "box", args...)
}
// Run the command
func (cmd *BoxAddCommand) Run() error {
if err := cmd.Start(); err != nil {
return err
}
return cmd.Wait()
}
// Start the command. You must call Wait() to complete execution.
func (cmd *BoxAddCommand) Start() error {
if err := cmd.init(); err != nil {
return err
}
return cmd.BaseCommand.Start()
}