forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_buildpack.go
113 lines (94 loc) · 2.96 KB
/
create_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
package buildpack
import (
"cf"
"cf/api"
"cf/command_metadata"
"cf/errors"
"cf/models"
"cf/requirements"
"cf/terminal"
"github.com/codegangsta/cli"
"strconv"
)
type CreateBuildpack struct {
ui terminal.UI
buildpackRepo api.BuildpackRepository
buildpackBitsRepo api.BuildpackBitsRepository
}
func NewCreateBuildpack(ui terminal.UI, buildpackRepo api.BuildpackRepository, buildpackBitsRepo api.BuildpackBitsRepository) (cmd CreateBuildpack) {
cmd.ui = ui
cmd.buildpackRepo = buildpackRepo
cmd.buildpackBitsRepo = buildpackBitsRepo
return
}
func (command CreateBuildpack) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "create-buildpack",
Description: "Create a buildpack",
Usage: "CF_NAME create-buildpack BUILDPACK PATH POSITION [--enable|--disable]" +
"\n\nTIP:\n" +
" Path should be a zip file, a url to a zip file, or a local directory. Position is an integer, sets priority, and is sorted from lowest to highest.",
Flags: []cli.Flag{
cli.BoolFlag{Name: "enable", Usage: "Enable the buildpack"},
cli.BoolFlag{Name: "disable", Usage: "Disable the buildpack"},
},
}
}
func (cmd CreateBuildpack) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
return
}
func (cmd CreateBuildpack) Run(c *cli.Context) {
if len(c.Args()) != 3 {
cmd.ui.FailWithUsage(c, "create-buildpack")
return
}
buildpackName := c.Args()[0]
cmd.ui.Say("Creating buildpack %s...", terminal.EntityNameColor(buildpackName))
buildpack, err := cmd.createBuildpack(buildpackName, c)
if err != nil {
if err, ok := err.(errors.HttpError); ok && err.ErrorCode() == errors.BUILDPACK_EXISTS {
cmd.ui.Ok()
cmd.ui.Warn("Buildpack %s already exists", buildpackName)
cmd.ui.Say("TIP: use '%s' to update this buildpack", terminal.CommandColor(cf.Name()+" update-buildpack"))
} else {
cmd.ui.Failed(err.Error())
}
return
}
cmd.ui.Ok()
cmd.ui.Say("")
cmd.ui.Say("Uploading buildpack %s...", terminal.EntityNameColor(buildpackName))
dir := c.Args()[1]
err = cmd.buildpackBitsRepo.UploadBuildpack(buildpack, dir)
if err != nil {
cmd.ui.Failed(err.Error())
return
}
cmd.ui.Ok()
}
func (cmd CreateBuildpack) createBuildpack(buildpackName string, c *cli.Context) (buildpack models.Buildpack, apiErr error) {
position, err := strconv.Atoi(c.Args()[2])
if err != nil {
apiErr = errors.NewWithFmt("Invalid position. %s", err.Error())
return
}
enabled := c.Bool("enable")
disabled := c.Bool("disable")
if enabled && disabled {
apiErr = errors.New("Cannot specify both enabled and disabled.")
return
}
var enableOption *bool = nil
if enabled {
enableOption = &enabled
}
if disabled {
disabled = false
enableOption = &disabled
}
buildpack, apiErr = cmd.buildpackRepo.Create(buildpackName, &position, enableOption, nil)
return
}