-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_buildpack.go
124 lines (103 loc) · 3.77 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
114
115
116
117
118
119
120
121
122
123
124
package buildpack
import (
"path/filepath"
"strconv"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
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 (cmd CreateBuildpack) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "create-buildpack",
Description: T("Create a buildpack"),
Usage: T("CF_NAME create-buildpack BUILDPACK PATH POSITION [--enable|--disable]") +
T("\n\nTIP:\n") + T(" Path should be a zip file, a url to a zip file, or a local directory. Position is a positive integer, sets priority, and is sorted from lowest to highest."),
Flags: []cli.Flag{
cli.BoolFlag{Name: "enable", Usage: T("Enable the buildpack to be used for staging")},
cli.BoolFlag{Name: "disable", Usage: T("Disable the buildpack from being used for staging")},
},
TotalArgs: 3,
}
}
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)
return
}
buildpackName := c.Args()[0]
cmd.ui.Say(T("Creating buildpack {{.BuildpackName}}...", map[string]interface{}{"BuildpackName": terminal.EntityNameColor(buildpackName)}))
buildpack, err := cmd.createBuildpack(buildpackName, c)
if err != nil {
if httpErr, ok := err.(errors.HttpError); ok && httpErr.ErrorCode() == errors.BUILDPACK_EXISTS {
cmd.ui.Ok()
cmd.ui.Warn(T("Buildpack {{.BuildpackName}} already exists", map[string]interface{}{"BuildpackName": buildpackName}))
cmd.ui.Say(T("TIP: use '{{.CfUpdateBuildpackCommand}}' to update this buildpack", map[string]interface{}{"CfUpdateBuildpackCommand": terminal.CommandColor(cf.Name() + " " + "update-buildpack")}))
} else {
cmd.ui.Failed(err.Error())
}
return
}
cmd.ui.Ok()
cmd.ui.Say("")
cmd.ui.Say(T("Uploading buildpack {{.BuildpackName}}...", map[string]interface{}{"BuildpackName": terminal.EntityNameColor(buildpackName)}))
dir, err := filepath.Abs(c.Args()[1])
if err != nil {
cmd.ui.Failed(err.Error())
return
}
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(T("Invalid position. {{.ErrorDescription}}", map[string]interface{}{"ErrorDescription": err.Error()}))
return
}
enabled := c.Bool("enable")
disabled := c.Bool("disable")
if enabled && disabled {
apiErr = errors.New(T("Cannot specify both {{.Enabled}} and {{.Disabled}}.", map[string]interface{}{
"Enabled": "enabled",
"Disabled": "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
}