Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quickstarts download issues [CLI-53] #119

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletions internal/cli/quickstarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"regexp"
"sort"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/prompt"
Expand All @@ -30,14 +30,6 @@ var (
}
return
}()
quickstartTypes = func() []string {
keys := make([]string, 0, len(quickstartsByType))
for k := range quickstartsByType {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}()
)

type quickstart struct {
Expand Down Expand Up @@ -70,29 +62,38 @@ func downloadQuickstart(cli *cli) *cobra.Command {

cmd := &cobra.Command{
Use: "download",
Short: "Download a specific type and tech stack for quick starts",
Short: "Download a quickstart sample app for a specific tech stack",
Long: `auth0 quickstarts download --type <type> --client-id <client-id> --stack <stack>`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cli.api.Client.Read(flags.ClientID)
if err != nil {
return err
if !shouldPrompt(cmd, "client-id") {
return errors.New("This command can only be run on interactive mode")
}

selectedType := flags.Type
selectedStack := flags.Stack
selectedClientID := flags.ClientID

if selectedType == "" {
input := prompt.SelectInput("type", "Quickstart Type:", "Type of quickstart to download", quickstartTypes, true)
if err := prompt.AskOne(input, &selectedType); err != nil {
if selectedClientID == "" {
input := prompt.TextInput("client-id", "Client Id:", "Client Id of an Auth0 application.", true)
if err := prompt.AskOne(input, &selectedClientID); err != nil {
return err
}
}

client, err := cli.api.Client.Read(selectedClientID)
if err != nil {
return err
}

selectedStack := flags.Stack

if selectedStack == "" {
stacks, err := quickstartStacksFromType(selectedType)
stacks, err := quickstartStacksFromType(client.GetAppType())
if err != nil {
return err
}
input := prompt.SelectInput("stack", "Quickstart Stack:", "Tech/Language to use for quickstart", stacks, true)
input := prompt.SelectInput("stack", "Stack:", "Tech/Language of the quickstart sample to download", stacks, true)
if err := prompt.AskOne(input, &selectedStack); err != nil {
return err
}
Expand All @@ -103,35 +104,33 @@ func downloadQuickstart(cli *cli) *cobra.Command {
return err
}

if exists {
// TODO(cyx): prompt for a warning to force overwrite.
// For now, we're just exiting to simplify this first stab.
cli.renderer.Warnf("WARNING: %s already exists. Run with --force to overwrite", target)
return nil
if exists && !cli.force {
if confirmed := prompt.Confirm(fmt.Sprintf("WARNING: %s already exists. Are you sure you want to proceed?", target)); !confirmed {
return nil
}
}

q, err := getQuickstart(selectedType, selectedStack)
q, err := getQuickstart(client.GetAppType(), selectedStack)
if err != nil {
return err
}

err = ansi.Spinner("Downloading quickstart", func() error {
err = ansi.Spinner("Downloading quickstart sample", func() error {
return downloadQuickStart(context.TODO(), cli, client, target, q)
})

if err != nil {
return err
}

cli.renderer.Infof("Quickstart sucessfully downloaded at %s", target)
cli.renderer.Infof("Quickstart sample sucessfully downloaded at %s", target)
return nil
},
}

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.Flags().StringVar(&flags.ClientID, "client-id", "", "ID of the client.")
cmd.Flags().StringVarP(&flags.Type, "type", "t", "", "Type of the quickstart to download.")
cmd.Flags().StringVarP(&flags.Stack, "stack", "s", "", "Tech stack of the quickstart to use.")
cmd.Flags().StringVarP(&flags.ClientID, "client-id", "c", "", "Client Id of an Auth0 application.")
cmd.Flags().StringVarP(&flags.Stack, "stack", "s", "", "Tech/Language of the quickstart sample to download.")
mustRequireFlags(cmd, "client-id")

return cmd
Expand Down Expand Up @@ -219,6 +218,10 @@ func downloadQuickStart(ctx context.Context, cli *cli, client *management.Client
}
defer os.Remove(tmpfile.Name())

if err := os.RemoveAll(target); err != nil {
return err
}

if err := archiver.Unarchive(tmpfile.Name(), target); err != nil {
return err
}
Expand Down