Skip to content

Commit e3acd4a

Browse files
committed
chore(builds): fetch support preview and save to local
1 parent 5e86155 commit e3acd4a

7 files changed

Lines changed: 77 additions & 51 deletions

File tree

cmd/builds.go

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,6 @@ func (d *DryccCmd) BuildsInfo(appID string, version int) error {
3434
table.Append([]string{"Updated:", build.Updated})
3535
table.Render()
3636

37-
if len(build.Dockerfile) != 0 {
38-
table = d.getDefaultFormatTable([]string{})
39-
table.Append([]string{"Dockerfile:"})
40-
table.Append([]string{d.indentString(build.Dockerfile, 2)})
41-
table.Render()
42-
}
43-
44-
if len(build.Procfile) != 0 {
45-
table = d.getDefaultFormatTable([]string{})
46-
table.Append([]string{"Procfile:"})
47-
table.Append([]string{d.indentString(d.toYamlString(build.Procfile, 2), 2)})
48-
table.Render()
49-
}
50-
51-
if len(build.Dryccfile) != 0 {
52-
table = d.getDefaultFormatTable([]string{})
53-
table.Append([]string{"Dryccfile:"})
54-
table.Append([]string{d.indentString(d.toYamlString(build.Dryccfile, 2), 2)})
55-
table.Render()
56-
}
5737
return nil
5838
}
5939

@@ -102,7 +82,7 @@ func (d *DryccCmd) BuildsCreate(appID, image, stack, procfile, dryccpath, confir
10282
return nil
10383
}
10484

105-
func (d *DryccCmd) BuildsFetch(appID string, version int, procfile, dryccpath, confirm string) error {
85+
func (d *DryccCmd) BuildsFetch(appID string, version int, procfile, dryccpath, confirm string, save bool) error {
10686
s, appID, err := load(d.ConfigFile, appID)
10787
if err != nil {
10888
return err
@@ -113,33 +93,66 @@ func (d *DryccCmd) BuildsFetch(appID string, version int, procfile, dryccpath, c
11393
return err
11494
}
11595
// Confirm again
116-
err = buildFetchConfirmAction(confirm, procfile, dryccpath)
96+
dockerfile := "Dockerfile"
97+
err = buildFetchConfirmAction(confirm, dockerfile, procfile, dryccpath, save)
11798
if err != nil {
11899
return err
119100
}
120-
os.Remove(procfile)
121-
os.ReadDir(dryccpath)
101+
if len(build.Dockerfile) != 0 {
102+
err := writeDockerfileToPath(d, dockerfile, build.Dockerfile, save)
103+
if err != nil {
104+
return fmt.Errorf("failed to write Dockerfile: %w", err)
105+
}
106+
}
107+
122108
if len(build.Procfile) != 0 {
123-
err := os.WriteFile(procfile, []byte(d.toYamlString(build.Procfile, 2)), 0755)
109+
err := writeProcfileToPath(d, procfile, build.Procfile, save)
124110
if err != nil {
125111
return fmt.Errorf("failed to write Procfile: %w", err)
126112
}
127113
}
128114

129115
if len(build.Dryccfile) != 0 {
130-
err := writeDryccfileToPath(dryccpath, build.Dryccfile)
116+
err := writeDryccfileToPath(d, dryccpath, build.Dryccfile, save)
131117
if err != nil {
132118
return fmt.Errorf("failed to write Dryccfile: %w", err)
133119
}
134120
}
135-
d.Println("done")
121+
if save {
122+
d.Println("done")
123+
}
124+
return nil
125+
}
136126

127+
func writeDockerfileToPath(d *DryccCmd, dockerfile string, dockerinfo string, save bool) error {
128+
if save {
129+
os.Remove(dockerfile)
130+
err := os.WriteFile(dockerfile, []byte(dockerinfo), 0664)
131+
return err
132+
}
133+
d.Println("# Source:", dockerfile)
134+
d.Println(dockerinfo)
137135
return nil
138136

139137
}
140138

141-
func writeDryccfileToPath(dryccpath string, dryccfile map[string]interface{}) error {
139+
func writeProcfileToPath(d *DryccCmd, procfile string, Procinfo map[string]string, save bool) error {
140+
if save {
141+
os.Remove(procfile)
142+
err := os.WriteFile(procfile, []byte(d.toYamlString(Procinfo, 2)), 0664)
143+
return err
144+
}
145+
d.Println("# Source:", procfile)
146+
d.Println(d.toYamlString(Procinfo, 2))
147+
return nil
148+
}
149+
150+
func writeDryccfileToPath(d *DryccCmd, dryccpath string, dryccfile map[string]interface{}, save bool) error {
142151
// Create the directory if it doesn't exist
152+
if save {
153+
os.Remove(dryccpath)
154+
}
155+
os.ReadDir(dryccpath)
143156
err := os.MkdirAll(dryccpath, os.ModePerm)
144157
if err != nil {
145158
return fmt.Errorf("failed to create directory: %w", err)
@@ -161,9 +174,14 @@ func writeDryccfileToPath(dryccpath string, dryccfile map[string]interface{}) er
161174
content += fmt.Sprintf("%s=%v\n", k, v)
162175
}
163176
// Write accumulated content once
164-
err := os.WriteFile(envFilePath, []byte(content), 0755)
165-
if err != nil {
166-
return fmt.Errorf("failed to write env file: %w", err)
177+
if save {
178+
err := os.WriteFile(envFilePath, []byte(content), 0664)
179+
if err != nil {
180+
return fmt.Errorf("failed to write env file: %w", err)
181+
}
182+
} else {
183+
d.Println("# Source:", envFilePath)
184+
d.Println(content)
167185
}
168186
}
169187
}
@@ -179,9 +197,14 @@ func writeDryccfileToPath(dryccpath string, dryccfile map[string]interface{}) er
179197
return fmt.Errorf("failed to marshal pipeline config: %w", err)
180198
}
181199
yamlContent := buf.Bytes()
182-
err = os.WriteFile(filePath, yamlContent, 0755)
183-
if err != nil {
184-
return fmt.Errorf("failed to write pipeline file: %w", err)
200+
if save {
201+
err = os.WriteFile(filePath, yamlContent, 0664)
202+
if err != nil {
203+
return fmt.Errorf("failed to write pipeline file: %w", err)
204+
}
205+
} else {
206+
d.Println("# Source:", filePath)
207+
d.Println(string(yamlContent))
185208
}
186209
}
187210
}
@@ -215,13 +238,13 @@ func buildConfirmAction(c *drycc.Client, appID string, procfileMap map[string]st
215238
return nil
216239
}
217240

218-
func buildFetchConfirmAction(confirm, procfile, dryccpath string) error {
241+
func buildFetchConfirmAction(confirm, dockerfile, procfile, dryccpath string, save bool) error {
219242

220-
if confirm == "" || confirm != "yes" {
243+
if save && (confirm == "" || confirm != "yes") {
221244
// hint
222245
msg := fmt.Sprintf(" ! WARNING: Potentially Build Fetch Action\n"+
223-
" ! This operation will first rm the current \x1b[1m%s\x1b[0m and \x1b[1m%s\x1b[0m locally\n"+
224-
" ! To proceed, type \"yes\" !\n\n> ", procfile, dryccpath)
246+
" ! This operation will overwrite the current \x1b[1m%s\x1b[0m, \x1b[1m%s\x1b[0m or \x1b[1m%s\x1b[0m locally\n"+
247+
" ! To proceed, type \"yes\" !\n\n> ", dockerfile, procfile, dryccpath)
225248

226249
fmt.Print(msg)
227250
fmt.Scanln(&confirm)

cmd/builds_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func TestBuildsFetch(t *testing.T) {
266266
w.Write([]byte("yes\n"))
267267
}()
268268

269-
err := cmdr.BuildsFetch("testapp", 0, procfilePath, dryccpath, "")
269+
err := cmdr.BuildsFetch("testapp", 0, procfilePath, dryccpath, "", true)
270270
if err != nil {
271271
t.Fatalf("expected no error, got %v", err)
272272
}
@@ -319,7 +319,7 @@ func TestBuildsFetch(t *testing.T) {
319319
w.Write([]byte("no\n"))
320320
}()
321321

322-
err := cmdr.BuildsFetch("testapp", 0, procfilePath, dryccpath, "")
322+
err := cmdr.BuildsFetch("testapp", 0, procfilePath, dryccpath, "", true)
323323
if err == nil || err.Error() != "cancel the build create fetch" {
324324
t.Fatalf("expected cancellation error, got %v", err)
325325
}

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Commander interface {
3636
TokensRemove(string, string) error
3737
BuildsInfo(string, int) error
3838
BuildsCreate(string, string, string, string, string, string) error
39-
BuildsFetch(string, int, string, string, string) error
39+
BuildsFetch(string, int, string, string, string, bool) error
4040
CertsList(string, int) error
4141
CertAdd(string, string, string, string) error
4242
CertRemove(string, string) error

cmd/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ func (d *DryccCmd) ConfigPull(appID, ptype, group, fileName string, interactive
224224
configMap[value.Name] = value.Value
225225
}
226226
}
227-
return os.WriteFile(fileName, []byte(formatEnv(configMap)), 0755)
227+
return os.WriteFile(fileName, []byte(formatEnv(configMap)), 0664)
228228
}
229-
return os.WriteFile(fileName, []byte(formatConfig(configValues)), 0755)
229+
return os.WriteFile(fileName, []byte(formatConfig(configValues)), 0664)
230230
}
231231

232232
// ConfigPush pushes an app's config from a file.

cmd/keys_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestListKeys(t *testing.T) {
8888

8989
folder := filepath.Join(name, ".ssh")
9090

91-
err = os.Mkdir(folder, 0755)
91+
err = os.Mkdir(folder, 0600)
9292
assert.NoError(t, err)
9393

9494
toWrite := []byte("ssh-rsa abc test@example.com")
@@ -302,7 +302,7 @@ func TestKeyAdd(t *testing.T) {
302302
assert.NoError(t, err)
303303
settings.SetHome(name)
304304
folder := filepath.Join(name, ".ssh")
305-
err = os.Mkdir(folder, 0755)
305+
err = os.Mkdir(folder, 0600)
306306
assert.NoError(t, err)
307307

308308
cf, server, err := testutil.NewTestServerAndClient()
@@ -348,7 +348,7 @@ func TestKeyAddName(t *testing.T) {
348348
assert.NoError(t, err)
349349
settings.SetHome(name)
350350
folder := filepath.Join(name, ".ssh")
351-
err = os.Mkdir(folder, 0755)
351+
err = os.Mkdir(folder, 0600)
352352
assert.NoError(t, err)
353353

354354
cf, server, err := testutil.NewTestServerAndClient()

parser/builds.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Options:
114114

115115
func buildsFetch(argv []string, cmdr cmd.Commander) error {
116116
usage := `
117-
Print information about a specific build.
117+
Print process info about a specific build.
118118
119119
Usage: drycc builds:fetch [options]
120120
@@ -124,11 +124,13 @@ Options:
124124
-v --version=<version>
125125
the version for which the build info needs to be fetched.
126126
-p --procfile=<procfile>
127-
a YAML file used to supply a Procfile to the application.
127+
the filename of the procfile saved locally, default is 'Procfile'.
128128
-d --dryccpath=<dryccpath>
129-
drycc config path to the application, default is '.drycc'.
129+
the folder name of the dryccfile saved locally, default is '.drycc'.
130130
--confirm=yes
131131
to proceed, type "yes".
132+
--save
133+
save process info to the local.
132134
`
133135

134136
args, err := docopt.ParseArgs(usage, argv, "")
@@ -147,7 +149,8 @@ Options:
147149

148150
procfile := safeGetValue(args, "--procfile", "Procfile")
149151
dryccpath := safeGetValue(args, "--dryccpath", ".drycc")
152+
save := args["--save"].(bool)
150153
confirm := safeGetString(args, "--confirm")
151154

152-
return cmdr.BuildsFetch(app, version, procfile, dryccpath, confirm)
155+
return cmdr.BuildsFetch(app, version, procfile, dryccpath, confirm, save)
153156
}

parser/builds_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (d FakeDryccCmd) BuildsCreate(string, string, string, string, string, strin
1717
return errors.New("builds:create")
1818
}
1919

20-
func (d FakeDryccCmd) BuildsFetch(string, int, string, string, string) error {
20+
func (d FakeDryccCmd) BuildsFetch(string, int, string, string, string, bool) error {
2121
return errors.New("builds:fetch")
2222
}
2323

0 commit comments

Comments
 (0)