Skip to content

Commit d0b49d7

Browse files
committed
chore(workflow-cli): build create hint if has large change
1 parent 84fab3d commit d0b49d7

5 files changed

Lines changed: 73 additions & 36 deletions

File tree

cmd/builds.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"os"
56

67
yaml "gopkg.in/yaml.v3"
78

9+
drycc "github.com/drycc/controller-sdk-go"
810
"github.com/drycc/controller-sdk-go/builds"
911
)
1012

@@ -54,7 +56,7 @@ func (d *DryccCmd) BuildsInfo(appID string, version int) error {
5456
}
5557

5658
// BuildsCreate creates a build for an app.
57-
func (d *DryccCmd) BuildsCreate(appID, image, stack, procfile string, dryccfile string) error {
59+
func (d *DryccCmd) BuildsCreate(appID, image, stack, procfile, dryccfile, confirm string) error {
5860
s, appID, err := load(d.ConfigFile, appID)
5961

6062
if err != nil {
@@ -84,7 +86,11 @@ func (d *DryccCmd) BuildsCreate(appID, image, stack, procfile string, dryccfile
8486
return err
8587
}
8688
}
87-
89+
// check procfileMap dryccfileMap stack is exist
90+
err = buildConfirmAction(s.Client, appID, procfileMap, dryccfileMap, confirm)
91+
if err != nil {
92+
return err
93+
}
8894
d.Print("Creating build... ")
8995
quit := progress(d.WOut)
9096
_, err = builds.New(s.Client, appID, image, stack, procfileMap, dryccfileMap)
@@ -108,3 +114,25 @@ func parseDryccfile(dryccfile []byte) (map[string]interface{}, error) {
108114
dryccfileMap := make(map[string]interface{})
109115
return dryccfileMap, yaml.Unmarshal(dryccfile, &dryccfileMap)
110116
}
117+
118+
func buildConfirmAction(c *drycc.Client, appID string, procfileMap map[string]string,
119+
dryccfileMap map[string]interface{}, confirm string) error {
120+
121+
build, _ := builds.Get(c, appID, 0)
122+
123+
if ((len(build.Procfile) != 0 && len(procfileMap) == 0) || (len(build.Dryccfile) != 0 && len(dryccfileMap) == 0)) && (confirm == "" || confirm != "yes") {
124+
// hint
125+
fmt.Printf(` ! WARNING: Potentially Build Create Action
126+
! The Procfile or drycc.yaml is empty, not last time
127+
! To proceed, type "yes" !
128+
129+
> `)
130+
131+
fmt.Scanln(&confirm)
132+
if confirm != "yes" {
133+
return fmt.Errorf("cancel the build create action")
134+
}
135+
return nil
136+
}
137+
return nil
138+
}

cmd/builds_test.go

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,32 @@ func TestBuildsCreate(t *testing.T) {
8484

8585
server.Mux.HandleFunc("/v2/apps/enterprise/build/", func(w http.ResponseWriter, r *http.Request) {
8686
testutil.SetHeaders(w)
87-
testutil.AssertBody(t, api.CreateBuildRequest{
88-
Image: "ncc/1701:A",
89-
Stack: "container",
90-
}, r)
91-
w.WriteHeader(http.StatusCreated)
92-
fmt.Fprintf(w, "{}")
87+
if r.Method == "POST" {
88+
testutil.AssertBody(t, api.CreateBuildRequest{
89+
Image: "ncc/1701:A",
90+
Stack: "container",
91+
}, r)
92+
w.WriteHeader(http.StatusCreated)
93+
fmt.Fprintf(w, "{}")
94+
}
9395
})
9496

95-
err = cmdr.BuildsCreate("enterprise", "ncc/1701:A", "container", "", "")
97+
err = cmdr.BuildsCreate("enterprise", "ncc/1701:A", "container", "", "", "yes")
9698
assert.NoError(t, err)
9799
assert.Equal(t, testutil.StripProgress(b.String()), "Creating build... done\n", "output")
98100

99101
server.Mux.HandleFunc("/v2/apps/bradbury/build/", func(w http.ResponseWriter, r *http.Request) {
100102
testutil.SetHeaders(w)
101-
testutil.AssertBody(t, api.CreateBuildRequest{
102-
Image: "nx/72307:latest",
103-
Stack: "container",
104-
Procfile: map[string]string{
105-
"web": "./drive",
106-
"warp": "./warp 8",
107-
},
108-
}, r)
103+
if r.Method == "POST" {
104+
testutil.AssertBody(t, api.CreateBuildRequest{
105+
Image: "nx/72307:latest",
106+
Stack: "container",
107+
Procfile: map[string]string{
108+
"web": "./drive",
109+
"warp": "./warp 8",
110+
},
111+
}, r)
112+
}
109113

110114
w.WriteHeader(http.StatusCreated)
111115
fmt.Fprintf(w, "{}")
@@ -127,28 +131,30 @@ warp: ./warp 8`
127131
}
128132
}()
129133

130-
err = cmdr.BuildsCreate("bradbury", "nx/72307:latest", "container", tmpDir+"/Procfile", "")
134+
err = cmdr.BuildsCreate("bradbury", "nx/72307:latest", "container", tmpDir+"/Procfile", "", "yes")
131135
assert.NoError(t, err)
132136
assert.Equal(t, testutil.StripProgress(b.String()), "Creating build... done\n", "output")
133137

134138
server.Mux.HandleFunc("/v2/apps/franklin/build/", func(w http.ResponseWriter, r *http.Request) {
135139
testutil.SetHeaders(w)
136-
testutil.AssertBody(t, api.CreateBuildRequest{
137-
Image: "nx/326:latest",
138-
Stack: "container",
139-
Procfile: map[string]string{
140-
"web": "./drive",
141-
"warp": "./warp 8",
142-
},
143-
Dryccfile: map[string]interface{}{
144-
"deploy": map[string]interface{}{
145-
"web": map[string]interface{}{
146-
"command": []string{"bash", "-c"},
147-
"args": []string{"bundle exec puma -C config/puma.rb"},
140+
if r.Method == "POST" {
141+
testutil.AssertBody(t, api.CreateBuildRequest{
142+
Image: "nx/326:latest",
143+
Stack: "container",
144+
Procfile: map[string]string{
145+
"web": "./drive",
146+
"warp": "./warp 8",
147+
},
148+
Dryccfile: map[string]interface{}{
149+
"deploy": map[string]interface{}{
150+
"web": map[string]interface{}{
151+
"command": []string{"bash", "-c"},
152+
"args": []string{"bundle exec puma -C config/puma.rb"},
153+
},
148154
},
149155
},
150-
},
151-
}, r)
156+
}, r)
157+
}
152158

153159
w.WriteHeader(http.StatusCreated)
154160
fmt.Fprintf(w, "{}")
@@ -171,7 +177,7 @@ deploy:
171177
`), os.ModePerm)
172178
assert.NoError(t, err)
173179

174-
err = cmdr.BuildsCreate("franklin", "nx/326:latest", "container", "Procfile", "drycc.yaml")
180+
err = cmdr.BuildsCreate("franklin", "nx/326:latest", "container", "Procfile", "drycc.yaml", "yes")
175181
assert.NoError(t, err)
176182
assert.Equal(t, testutil.StripProgress(b.String()), "Creating build... done\n", "output")
177183

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Commander interface {
3535
TokensAdd(*drycc.Client, string, string, string, string, bool) (*api.AuthTokenResponse, error)
3636
TokensRemove(string, string) error
3737
BuildsInfo(string, int) error
38-
BuildsCreate(string, string, string, string, string) error
38+
BuildsCreate(string, string, string, string, string, string) error
3939
CertsList(string, int) error
4040
CertAdd(string, string, string, string) error
4141
CertRemove(string, string) error

parser/builds.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Options:
8888
A YAML file used to supply a Procfile to the application.
8989
-d --dryccfile=<dryccfile>
9090
A YAML file used to supply a drycc.yaml to the application.
91+
--confirm=yes
92+
To proceed, type "yes".
9193
`
9294

9395
args, err := docopt.ParseArgs(usage, argv, "")
@@ -99,11 +101,12 @@ Options:
99101
app := safeGetString(args, "--app")
100102
image := safeGetString(args, "<image>")
101103
stack := safeGetString(args, "--stack")
104+
confirm := safeGetString(args, "--confirm")
102105
if stack == "" {
103106
stack = "container"
104107
}
105108
procfile := safeGetValue(args, "--procfile", "Procfile")
106109
dryccfile := safeGetValue(args, "--dryccfile", "drycc.yaml")
107110

108-
return cmdr.BuildsCreate(app, image, stack, procfile, dryccfile)
111+
return cmdr.BuildsCreate(app, image, stack, procfile, dryccfile, confirm)
109112
}

parser/builds_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (d FakeDryccCmd) BuildsInfo(string, int) error {
1313
return errors.New("builds:info")
1414
}
1515

16-
func (d FakeDryccCmd) BuildsCreate(string, string, string, string, string) error {
16+
func (d FakeDryccCmd) BuildsCreate(string, string, string, string, string, string) error {
1717
return errors.New("builds:create")
1818
}
1919

0 commit comments

Comments
 (0)