Skip to content
This repository was archived by the owner on Jan 16, 2021. It is now read-only.

Commit a97c983

Browse files
author
Pavan Kumar
committed
make improvements to 'new' command
* parametrize 'new' command * let default action for existing apps be download latest deployed cloude code * allow download to specific location * allow 'new' to have a mode to create skelton with config only
1 parent 88d6b21 commit a97c983

File tree

12 files changed

+152
-73
lines changed

12 files changed

+152
-73
lines changed

add.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ func (a *addCmd) addSelectedApp(
2525
return stackerr.Newf("Unknown project type: %d.", e.Type)
2626
}
2727

28-
func (a *addCmd) selectApp(e *env) (*app, error) {
28+
func (a *addCmd) selectApp(e *env, appName string) (*app, error) {
2929
apps, err := a.apps.restFetchApps(e)
3030
if err != nil {
3131
return nil, err
3232
}
33+
if appName != "" {
34+
for _, app := range apps {
35+
if app.Name == appName {
36+
return app, nil
37+
}
38+
}
39+
return nil, stackerr.Newf("You are not a collaborator on app: %s", appName)
40+
}
41+
3342
app, err := a.apps.selectApp(apps, "Select an App to add to config: ", e)
3443
if err != nil {
3544
return nil, err
@@ -42,7 +51,7 @@ func (a *addCmd) run(e *env, args []string) error {
4251
if err := a.apps.login.authUser(e); err != nil {
4352
return err
4453
}
45-
app, err := a.selectApp(e)
54+
app, err := a.selectApp(e, "") // TODO (can also make this configurable)
4655
if err != nil {
4756
return err
4857
}

add_cmd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func newAddCmdHarnessWithConfig(t testing.TB) (*Harness, *addCmd, error) {
5858
h, _ := newAddCmdHarness(t)
5959
h.makeEmptyRoot()
6060

61-
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "B"}, false))
61+
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "B"}, false, true))
6262

6363
return h, &a, nil
6464
}
@@ -115,7 +115,7 @@ func newLegacyAddCmdHarnessWithConfig(t testing.TB) (*Harness, *addCmd, error) {
115115
h, _ := newAddCmdHarness(t)
116116
h.makeEmptyRoot()
117117

118-
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "B"}, false))
118+
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "B"}, false, true))
119119
ensure.Nil(t, os.MkdirAll(filepath.Join(h.env.Root, configDir), 0755))
120120
ensure.Nil(t,
121121
ioutil.WriteFile(

apps.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (a *apps) restCreateApp(e *env, appName string) (*app, error) {
222222
return &res, nil
223223
}
224224

225-
func (a *apps) createApp(e *env) (*app, error) {
225+
func (a *apps) createApp(e *env, givenName string) (*app, error) {
226226
apps, err := a.restFetchApps(e)
227227
if err != nil {
228228
return nil, err
@@ -232,10 +232,16 @@ func (a *apps) createApp(e *env) (*app, error) {
232232
appNames[app.Name] = struct{}{}
233233
}
234234
for i := 0; i < numRetries; i++ {
235-
appName, err := a.getAppName(e)
236-
if err != nil {
237-
return nil, err
235+
var appName string
236+
if givenName != "" {
237+
appName = givenName
238+
} else {
239+
appName, err = a.getAppName(e)
240+
if err != nil {
241+
return nil, err
242+
}
238243
}
244+
239245
if _, ok := appNames[appName]; ok {
240246
fmt.Fprintf(e.Out,
241247
`Hey! you already created an app named %q.

apps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func TestCreateNewApp(t *testing.T) {
234234

235235
a := defaultApps
236236
h.env.In = ioutil.NopCloser(strings.NewReader("D"))
237-
app, err := a.createApp(h.env)
237+
app, err := a.createApp(h.env, "")
238238
ensure.Nil(t, err)
239239

240240
ensure.Nil(t, a.printApp(h.env, app))

config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestConfigFromDirFormatType(t *testing.T) {
2727
h.makeEmptyRoot()
2828
defer h.Stop()
2929

30-
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false))
30+
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false, true))
3131

3232
c, err := configFromDir(h.env.Root)
3333
ensure.Nil(t, err)

default_cmd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func newDefaultCmdHarness(t testing.TB) (*Harness, *defaultCmd, *parseConfig) {
2727
h := newHarness(t)
2828
h.makeEmptyRoot()
2929

30-
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false))
30+
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false, true))
3131

3232
config := newDefaultParseConfig(t, h)
3333
h.Out.Reset()
@@ -97,7 +97,7 @@ func newLegacyDefaultCmdHarness(t testing.TB) (*Harness, *defaultCmd, *parseConf
9797
h := newHarness(t)
9898
h.makeEmptyRoot()
9999

100-
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false))
100+
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false, true))
101101
ensure.Nil(t, os.MkdirAll(filepath.Join(h.env.Root, configDir), 0755))
102102
ensure.Nil(t, ioutil.WriteFile(filepath.Join(h.env.Root, legacyConfigFile),
103103
[]byte("{}"),

download_cmd.go

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717
)
1818

1919
type downloadCmd struct {
20-
force bool
21-
release *deployInfo
20+
release *deployInfo
21+
destination string
22+
force bool
2223
}
2324

2425
func (d *downloadCmd) verifyChecksum(path, checksum string) error {
@@ -44,7 +45,7 @@ func (d *downloadCmd) verifyChecksum(path, checksum string) error {
4445

4546
func (d *downloadCmd) moveFiles(
4647
e *env,
47-
tempDir string,
48+
destination string,
4849
release *deployInfo) error {
4950
var wg errgroup.Group
5051

@@ -53,7 +54,7 @@ func (d *downloadCmd) moveFiles(
5354
wg.Add(numFiles)
5455

5556
var numErrors int32
56-
moveFile := func(tempDir, kind, file, checksum string) {
57+
moveFile := func(destination, kind, file, checksum string) {
5758
defer func() {
5859
wg.Done()
5960
<-maxParallel
@@ -70,7 +71,7 @@ func (d *downloadCmd) moveFiles(
7071
}
7172

7273
err = os.Rename(
73-
filepath.Join(tempDir, kind, file),
74+
filepath.Join(destination, kind, file),
7475
filepath.Join(e.Root, kind, file),
7576
)
7677
if err != nil {
@@ -93,7 +94,7 @@ func (d *downloadCmd) moveFiles(
9394
for file, checksum := range release.Checksums.Cloud {
9495
maxParallel <- struct{}{}
9596
go moveFile(
96-
tempDir,
97+
destination,
9798
cloudDir,
9899
file,
99100
checksum,
@@ -102,7 +103,7 @@ func (d *downloadCmd) moveFiles(
102103
for file, checksum := range release.Checksums.Public {
103104
maxParallel <- struct{}{}
104105
go moveFile(
105-
tempDir,
106+
destination,
106107
hostingDir,
107108
file,
108109
checksum,
@@ -141,7 +142,7 @@ the temporary download location.
141142
return nil
142143
}
143144

144-
func (d *downloadCmd) download(e *env, tempDir string, release *deployInfo) error {
145+
func (d *downloadCmd) download(e *env, destination string, release *deployInfo) error {
145146
var wg errgroup.Group
146147
maxParallel := make(chan struct{}, maxOpenFD)
147148
wg.Add(len(release.Versions.Cloud) + len(release.Versions.Public))
@@ -166,7 +167,7 @@ func (d *downloadCmd) download(e *env, tempDir string, release *deployInfo) erro
166167
return
167168
}
168169

169-
path := path.Join(tempDir, hostingDir, file)
170+
path := path.Join(destination, hostingDir, file)
170171
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
171172
wg.Error(stackerr.Wrap(err))
172173
return
@@ -201,7 +202,7 @@ func (d *downloadCmd) download(e *env, tempDir string, release *deployInfo) erro
201202
return
202203
}
203204

204-
path := path.Join(tempDir, cloudDir, file)
205+
path := path.Join(destination, cloudDir, file)
205206
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
206207
wg.Error(stackerr.Wrap(err))
207208
return
@@ -234,41 +235,50 @@ func (d *downloadCmd) download(e *env, tempDir string, release *deployInfo) erro
234235
}
235236

236237
func (d *downloadCmd) run(e *env, c *context) error {
237-
if d.release == nil {
238-
latestRelease, err := (&deployCmd{}).getPrevDeplInfo(e)
238+
var err error
239+
240+
latestRelease := d.release
241+
if latestRelease == nil {
242+
latestRelease, err = (&deployCmd{}).getPrevDeplInfo(e)
239243
if err != nil {
240244
return err
241245
}
242-
d.release = latestRelease
243246
}
244247

245-
tempDir, err := ioutil.TempDir("", "parse_code_")
246-
if err != nil {
247-
return stackerr.Wrap(err)
248+
destination := d.destination
249+
if destination == "" {
250+
destination, err = ioutil.TempDir("", "parse_code_")
251+
if err != nil {
252+
return stackerr.Wrap(err)
253+
}
248254
}
249255

250-
err = d.download(e, tempDir, d.release)
256+
err = d.download(e, destination, latestRelease)
251257
if err != nil {
252258
fmt.Fprintln(e.Err, "Failed to download Cloud Code.")
253259
return stackerr.Wrap(err)
254260
}
255261
if !d.force {
256-
fmt.Fprintf(e.Out, "Successfully downloaded Cloud Code to %q.\n", tempDir)
262+
fmt.Fprintf(e.Out, "Successfully downloaded Cloud Code to %q.\n", destination)
257263
return nil
258264
}
259265

260-
return stackerr.Wrap(d.moveFiles(e, tempDir, d.release))
266+
return stackerr.Wrap(d.moveFiles(e, destination, latestRelease))
261267
}
262268

263269
func newDownloadCmd(e *env) *cobra.Command {
264270
d := &downloadCmd{}
265271
cmd := &cobra.Command{
266272
Use: "download [app]",
267273
Short: "Downloads the Cloud Code project",
268-
Long: "Downloads the Cloud Code project at a temporary location.",
269-
Run: runWithClient(e, d.run),
274+
Long: `Downloads the Cloud Code project at a given location,
275+
or at a temporary location if nothing is explicitly provided through the -l flag.
276+
`,
277+
Run: runWithClient(e, d.run),
270278
}
271279
cmd.Flags().BoolVarP(&d.force, "force", "f", d.force,
272280
"Force will overwrite any files in the current project directory")
281+
cmd.Flags().StringVarP(&d.destination, "location", "l", d.destination,
282+
"Download Cloud Code project at the given location.")
273283
return cmd
274284
}

jssdk_cmd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newJsSdkHarnessWithConfig(t testing.TB) (*Harness, *context) {
4444
h := newJsSdkHarness(t)
4545
h.makeEmptyRoot()
4646

47-
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false))
47+
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false, true))
4848
h.Out.Reset()
4949

5050
c, err := configFromDir(h.env.Root)

list_cmd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ func TestPrintListNoApps(t *testing.T) {
6767
h.makeEmptyRoot()
6868
defer h.Stop()
6969
n := &newCmd{}
70-
n.cloneSampleCloudCode(h.env, &app{Name: "test"}, false)
70+
n.cloneSampleCloudCode(h.env, &app{Name: "test"}, false, true)
7171
ensure.Nil(t, l.printListOfApps(h.env))
7272
}

0 commit comments

Comments
 (0)