forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_buildpack.go
131 lines (111 loc) · 3.32 KB
/
update_buildpack.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package buildpack
import (
"cf/api"
"cf/command_metadata"
"cf/flag_helpers"
"cf/requirements"
"cf/terminal"
"errors"
"github.com/codegangsta/cli"
)
type UpdateBuildpack struct {
ui terminal.UI
buildpackRepo api.BuildpackRepository
buildpackBitsRepo api.BuildpackBitsRepository
buildpackReq requirements.BuildpackRequirement
}
func NewUpdateBuildpack(ui terminal.UI, repo api.BuildpackRepository, bitsRepo api.BuildpackBitsRepository) (cmd *UpdateBuildpack) {
cmd = new(UpdateBuildpack)
cmd.ui = ui
cmd.buildpackRepo = repo
cmd.buildpackBitsRepo = bitsRepo
return
}
func (command *UpdateBuildpack) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "update-buildpack",
Description: "Update a buildpack",
Usage: "CF_NAME update-buildpack BUILDPACK [-p PATH] [-i POSITION] [--enable|--disable] [--lock|--unlock]",
Flags: []cli.Flag{
flag_helpers.NewIntFlag("i", "Buildpack position among other buildpacks"),
flag_helpers.NewStringFlag("p", "Path to directory or zip file"),
cli.BoolFlag{Name: "enable", Usage: "Enable the buildpack"},
cli.BoolFlag{Name: "disable", Usage: "Disable the buildpack"},
cli.BoolFlag{Name: "lock", Usage: "Lock the buildpack"},
cli.BoolFlag{Name: "unlock", Usage: "Unlock the buildpack"},
},
}
}
func (cmd *UpdateBuildpack) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
err = errors.New("Incorrect Usage")
cmd.ui.FailWithUsage(c, "update-buildpack")
return
}
loginReq := requirementsFactory.NewLoginRequirement()
cmd.buildpackReq = requirementsFactory.NewBuildpackRequirement(c.Args()[0])
reqs = []requirements.Requirement{
loginReq,
cmd.buildpackReq,
}
return
}
func (cmd *UpdateBuildpack) Run(c *cli.Context) {
buildpack := cmd.buildpackReq.GetBuildpack()
cmd.ui.Say("Updating buildpack %s...", terminal.EntityNameColor(buildpack.Name))
updateBuildpack := false
if c.IsSet("i") {
position := c.Int("i")
buildpack.Position = &position
updateBuildpack = true
}
enabled := c.Bool("enable")
disabled := c.Bool("disable")
if enabled && disabled {
cmd.ui.Failed("Cannot specify both enabled and disabled options.")
return
}
if enabled {
buildpack.Enabled = &enabled
updateBuildpack = true
}
if disabled {
disabled = false
buildpack.Enabled = &disabled
updateBuildpack = true
}
lock := c.Bool("lock")
unlock := c.Bool("unlock")
if lock && unlock {
cmd.ui.Failed("Cannot specify both lock and unlock options.")
return
}
dir := c.String("p")
if dir != "" && (lock || unlock) {
cmd.ui.Failed("Cannot specify buildpack bits and lock/unlock.")
}
if lock {
buildpack.Locked = &lock
updateBuildpack = true
}
if unlock {
unlock = false
buildpack.Locked = &unlock
updateBuildpack = true
}
if updateBuildpack {
buildpack, apiErr := cmd.buildpackRepo.Update(buildpack)
if apiErr != nil {
cmd.ui.Failed("Error updating buildpack %s\n%s", terminal.EntityNameColor(buildpack.Name), apiErr.Error())
return
}
}
if dir != "" {
apiErr := cmd.buildpackBitsRepo.UploadBuildpack(buildpack, dir)
if apiErr != nil {
cmd.ui.Failed("Error uploading buildpack %s\n%s", terminal.EntityNameColor(buildpack.Name), apiErr.Error())
return
}
}
cmd.ui.Ok()
}