Skip to content

Commit

Permalink
make improvements to 'new' command
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Pavan Kumar committed Sep 18, 2015
1 parent 88d6b21 commit a97c983
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 73 deletions.
13 changes: 11 additions & 2 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@ func (a *addCmd) addSelectedApp(
return stackerr.Newf("Unknown project type: %d.", e.Type)
}

func (a *addCmd) selectApp(e *env) (*app, error) {
func (a *addCmd) selectApp(e *env, appName string) (*app, error) {
apps, err := a.apps.restFetchApps(e)
if err != nil {
return nil, err
}
if appName != "" {
for _, app := range apps {
if app.Name == appName {
return app, nil
}
}
return nil, stackerr.Newf("You are not a collaborator on app: %s", appName)
}

app, err := a.apps.selectApp(apps, "Select an App to add to config: ", e)
if err != nil {
return nil, err
Expand All @@ -42,7 +51,7 @@ func (a *addCmd) run(e *env, args []string) error {
if err := a.apps.login.authUser(e); err != nil {
return err
}
app, err := a.selectApp(e)
app, err := a.selectApp(e, "") // TODO (can also make this configurable)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions add_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func newAddCmdHarnessWithConfig(t testing.TB) (*Harness, *addCmd, error) {
h, _ := newAddCmdHarness(t)
h.makeEmptyRoot()

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

return h, &a, nil
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func newLegacyAddCmdHarnessWithConfig(t testing.TB) (*Harness, *addCmd, error) {
h, _ := newAddCmdHarness(t)
h.makeEmptyRoot()

ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "B"}, false))
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "B"}, false, true))
ensure.Nil(t, os.MkdirAll(filepath.Join(h.env.Root, configDir), 0755))
ensure.Nil(t,
ioutil.WriteFile(
Expand Down
14 changes: 10 additions & 4 deletions apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (a *apps) restCreateApp(e *env, appName string) (*app, error) {
return &res, nil
}

func (a *apps) createApp(e *env) (*app, error) {
func (a *apps) createApp(e *env, givenName string) (*app, error) {
apps, err := a.restFetchApps(e)
if err != nil {
return nil, err
Expand All @@ -232,10 +232,16 @@ func (a *apps) createApp(e *env) (*app, error) {
appNames[app.Name] = struct{}{}
}
for i := 0; i < numRetries; i++ {
appName, err := a.getAppName(e)
if err != nil {
return nil, err
var appName string
if givenName != "" {
appName = givenName
} else {
appName, err = a.getAppName(e)
if err != nil {
return nil, err
}
}

if _, ok := appNames[appName]; ok {
fmt.Fprintf(e.Out,
`Hey! you already created an app named %q.
Expand Down
2 changes: 1 addition & 1 deletion apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func TestCreateNewApp(t *testing.T) {

a := defaultApps
h.env.In = ioutil.NopCloser(strings.NewReader("D"))
app, err := a.createApp(h.env)
app, err := a.createApp(h.env, "")
ensure.Nil(t, err)

ensure.Nil(t, a.printApp(h.env, app))
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestConfigFromDirFormatType(t *testing.T) {
h.makeEmptyRoot()
defer h.Stop()

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

c, err := configFromDir(h.env.Root)
ensure.Nil(t, err)
Expand Down
4 changes: 2 additions & 2 deletions default_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newDefaultCmdHarness(t testing.TB) (*Harness, *defaultCmd, *parseConfig) {
h := newHarness(t)
h.makeEmptyRoot()

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

config := newDefaultParseConfig(t, h)
h.Out.Reset()
Expand Down Expand Up @@ -97,7 +97,7 @@ func newLegacyDefaultCmdHarness(t testing.TB) (*Harness, *defaultCmd, *parseConf
h := newHarness(t)
h.makeEmptyRoot()

ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false))
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false, true))
ensure.Nil(t, os.MkdirAll(filepath.Join(h.env.Root, configDir), 0755))
ensure.Nil(t, ioutil.WriteFile(filepath.Join(h.env.Root, legacyConfigFile),
[]byte("{}"),
Expand Down
52 changes: 31 additions & 21 deletions download_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
)

type downloadCmd struct {
force bool
release *deployInfo
release *deployInfo
destination string
force bool
}

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

func (d *downloadCmd) moveFiles(
e *env,
tempDir string,
destination string,
release *deployInfo) error {
var wg errgroup.Group

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

var numErrors int32
moveFile := func(tempDir, kind, file, checksum string) {
moveFile := func(destination, kind, file, checksum string) {
defer func() {
wg.Done()
<-maxParallel
Expand All @@ -70,7 +71,7 @@ func (d *downloadCmd) moveFiles(
}

err = os.Rename(
filepath.Join(tempDir, kind, file),
filepath.Join(destination, kind, file),
filepath.Join(e.Root, kind, file),
)
if err != nil {
Expand All @@ -93,7 +94,7 @@ func (d *downloadCmd) moveFiles(
for file, checksum := range release.Checksums.Cloud {
maxParallel <- struct{}{}
go moveFile(
tempDir,
destination,
cloudDir,
file,
checksum,
Expand All @@ -102,7 +103,7 @@ func (d *downloadCmd) moveFiles(
for file, checksum := range release.Checksums.Public {
maxParallel <- struct{}{}
go moveFile(
tempDir,
destination,
hostingDir,
file,
checksum,
Expand Down Expand Up @@ -141,7 +142,7 @@ the temporary download location.
return nil
}

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

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

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

func (d *downloadCmd) run(e *env, c *context) error {
if d.release == nil {
latestRelease, err := (&deployCmd{}).getPrevDeplInfo(e)
var err error

latestRelease := d.release
if latestRelease == nil {
latestRelease, err = (&deployCmd{}).getPrevDeplInfo(e)
if err != nil {
return err
}
d.release = latestRelease
}

tempDir, err := ioutil.TempDir("", "parse_code_")
if err != nil {
return stackerr.Wrap(err)
destination := d.destination
if destination == "" {
destination, err = ioutil.TempDir("", "parse_code_")
if err != nil {
return stackerr.Wrap(err)
}
}

err = d.download(e, tempDir, d.release)
err = d.download(e, destination, latestRelease)
if err != nil {
fmt.Fprintln(e.Err, "Failed to download Cloud Code.")
return stackerr.Wrap(err)
}
if !d.force {
fmt.Fprintf(e.Out, "Successfully downloaded Cloud Code to %q.\n", tempDir)
fmt.Fprintf(e.Out, "Successfully downloaded Cloud Code to %q.\n", destination)
return nil
}

return stackerr.Wrap(d.moveFiles(e, tempDir, d.release))
return stackerr.Wrap(d.moveFiles(e, destination, latestRelease))
}

func newDownloadCmd(e *env) *cobra.Command {
d := &downloadCmd{}
cmd := &cobra.Command{
Use: "download [app]",
Short: "Downloads the Cloud Code project",
Long: "Downloads the Cloud Code project at a temporary location.",
Run: runWithClient(e, d.run),
Long: `Downloads the Cloud Code project at a given location,
or at a temporary location if nothing is explicitly provided through the -l flag.
`,
Run: runWithClient(e, d.run),
}
cmd.Flags().BoolVarP(&d.force, "force", "f", d.force,
"Force will overwrite any files in the current project directory")
cmd.Flags().StringVarP(&d.destination, "location", "l", d.destination,
"Download Cloud Code project at the given location.")
return cmd
}
2 changes: 1 addition & 1 deletion jssdk_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newJsSdkHarnessWithConfig(t testing.TB) (*Harness, *context) {
h := newJsSdkHarness(t)
h.makeEmptyRoot()

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

c, err := configFromDir(h.env.Root)
Expand Down
2 changes: 1 addition & 1 deletion list_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ func TestPrintListNoApps(t *testing.T) {
h.makeEmptyRoot()
defer h.Stop()
n := &newCmd{}
n.cloneSampleCloudCode(h.env, &app{Name: "test"}, false)
n.cloneSampleCloudCode(h.env, &app{Name: "test"}, false, true)
ensure.Nil(t, l.printListOfApps(h.env))
}
Loading

0 comments on commit a97c983

Please sign in to comment.