-
Notifications
You must be signed in to change notification settings - Fork 927
/
create_buildpack_command.go
172 lines (144 loc) · 5.9 KB
/
create_buildpack_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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package v6
import (
"io/ioutil"
"os"
"time"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v6/shared"
"code.cloudfoundry.org/cli/util/download"
)
//go:generate counterfeiter . Downloader
type Downloader interface {
Download(string) (string, error)
}
//go:generate counterfeiter . CreateBuildpackActor
type CreateBuildpackActor interface {
CreateBuildpack(name string, position int, enabled bool) (v2action.Buildpack, v2action.Warnings, error)
UploadBuildpack(GUID string, path string, progBar v2action.SimpleProgressBar) (v2action.Warnings, error)
PrepareBuildpackBits(inputPath string, tmpDirPath string, downloader v2action.Downloader) (string, error)
}
type CreateBuildpackCommand struct {
RequiredArgs flag.CreateBuildpackArgs `positional-args:"yes"`
Disable bool `long:"disable" description:"Disable the buildpack from being used for staging"`
Enable bool `long:"enable" description:"Enable the buildpack to be used for staging"`
usage interface{} `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 a positive integer, sets priority, and is sorted from lowest to highest."`
relatedCommands interface{} `related_commands:"buildpacks, push"`
UI command.UI
Actor CreateBuildpackActor
ProgressBar v2action.SimpleProgressBar
SharedActor command.SharedActor
Config command.Config
}
func (cmd *CreateBuildpackCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui)
if err != nil {
return err
}
cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
cmd.ProgressBar = v2action.NewProgressBar()
return nil
}
func (cmd *CreateBuildpackCommand) Execute(args []string) error {
if cmd.Enable && cmd.Disable {
return translatableerror.ArgumentCombinationError{
Args: []string{"--enable", "--disable"},
}
}
err := cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return err
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
cmd.UI.DisplayTextWithFlavor("Creating buildpack {{.Buildpack}} as {{.Username}}...", map[string]interface{}{
"Buildpack": cmd.RequiredArgs.Buildpack,
"Username": user.Name,
})
downloader := download.NewDownloader(time.Second * 30)
tmpDirPath, err := ioutil.TempDir("", "buildpack-dir-")
if err != nil {
return err
}
defer os.RemoveAll(tmpDirPath)
pathToBuildpackBits, err := cmd.Actor.PrepareBuildpackBits(string(cmd.RequiredArgs.Path), tmpDirPath, downloader)
if err != nil {
return err
}
buildpack, warnings, err := cmd.Actor.CreateBuildpack(cmd.RequiredArgs.Buildpack, cmd.RequiredArgs.Position, !cmd.Disable)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return cmd.displayIfNameCollisionError(err)
}
cmd.UI.DisplayOK()
cmd.UI.DisplayTextWithFlavor("Uploading buildpack {{.Buildpack}} as {{.Username}}...", map[string]interface{}{
"Buildpack": cmd.RequiredArgs.Buildpack,
"Username": user.Name,
})
uploadWarnings, err := cmd.Actor.UploadBuildpack(buildpack.GUID, pathToBuildpackBits, cmd.ProgressBar)
if _, ok := err.(ccerror.InvalidAuthTokenError); ok {
cmd.UI.DisplayWarnings([]string{"Failed to upload buildpack due to auth token expiration, retrying..."})
uploadWarnings, err = cmd.Actor.UploadBuildpack(buildpack.GUID, pathToBuildpackBits, cmd.ProgressBar)
}
cmd.UI.DisplayWarnings(uploadWarnings)
if err != nil {
cmd.UI.DisplayNewline()
cmd.UI.DisplayNewline()
if _, ok := err.(actionerror.BuildpackAlreadyExistsForStackError); ok {
cmd.displayNameAndStackCollisionError(err)
return nil
} else if httpErr, ok := err.(download.RawHTTPStatusError); ok {
return translatableerror.HTTPStatusError{Status: httpErr.Status}
}
return err
}
cmd.UI.DisplayNewline()
cmd.UI.DisplayText("Done uploading")
cmd.UI.DisplayOK()
return nil
}
func (cmd CreateBuildpackCommand) displayNameAndStackCollisionError(err error) {
cmd.UI.DisplayWarning(err.Error())
cmd.UI.DisplayTextWithFlavor("TIP: use '{{.CfUpdateBuildpackCommand}}' to update this buildpack",
map[string]interface{}{
"CfUpdateBuildpackCommand": cmd.Config.BinaryName() + " update-buildpack",
})
}
func (cmd CreateBuildpackCommand) displayIfNameCollisionError(err error) error {
if _, ok := err.(actionerror.BuildpackAlreadyExistsWithoutStackError); ok {
cmd.displayAlreadyExistingBuildpackWithoutStack(err)
return nil
} else if _, ok := err.(actionerror.BuildpackNameTakenError); ok {
cmd.displayAlreadyExistingBuildpack(err)
return nil
}
return err
}
func (cmd CreateBuildpackCommand) displayAlreadyExistingBuildpackWithoutStack(err error) {
cmd.UI.DisplayNewline()
cmd.UI.DisplayWarning(err.Error())
cmd.UI.DisplayTextWithFlavor("TIP: use '{{.CfBuildpacksCommand}}' and '{{.CfDeleteBuildpackCommand}}' to delete buildpack {{.BuildpackName}} without a stack",
map[string]interface{}{
"CfBuildpacksCommand": cmd.Config.BinaryName() + " buildpacks",
"CfDeleteBuildpackCommand": cmd.Config.BinaryName() + " delete-buildpack",
"BuildpackName": cmd.RequiredArgs.Buildpack,
})
}
func (cmd CreateBuildpackCommand) displayAlreadyExistingBuildpack(err error) {
cmd.UI.DisplayNewline()
cmd.UI.DisplayWarning(err.Error())
cmd.UI.DisplayTextWithFlavor("TIP: use '{{.CfUpdateBuildpackCommand}}' to update this buildpack",
map[string]interface{}{
"CfUpdateBuildpackCommand": cmd.Config.BinaryName() + " update-buildpack",
})
}