From 75c3dcd1dce3a72b23697ea50591166c9817098a Mon Sep 17 00:00:00 2001 From: Ronen Lubin <63970571+ronenlu@users.noreply.github.com> Date: Thu, 19 Oct 2023 16:33:56 +0300 Subject: [PATCH] dont require migration dirs from github to run extenstion (#323) --- main.go | 3 --- main_test.go | 16 ++++++++++++++++ prompt.go | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 026951f..1a94bca 100644 --- a/main.go +++ b/main.go @@ -97,9 +97,6 @@ func (i *InitActionCmd) Run(ctx context.Context, client *githubClient, current r if err != nil { return err } - if len(dirs) == 0 { - return errors.New("no migration directories found in the repository") - } if err = i.setParams(dirs); err != nil { return err } diff --git a/main_test.go b/main_test.go index 8b790cd..0ca6c4f 100644 --- a/main_test.go +++ b/main_test.go @@ -183,6 +183,22 @@ func TestRunInitActionCmd(t *testing.T) { Token: "token", }, }, + { + name: "no dir path supplied, choose manual dir path", + client: createGHClient(&mockService{getContentError: &github.ErrorResponse{Message: "Not Found"}}), + cmd: &InitActionCmd{ + Token: "token", + DirName: "name", + }, + // enter, arrow key down, enter, `dir/migrations`, enter + prompt: "\n\x1b[B\n`dir/migrations`\n\n", + expected: &InitActionCmd{ + DirPath: "`dir/migrations`", + DirName: "name", + Driver: "mysql", + Token: "token", + }, + }, { name: "no dir name supplied use cloud dir name", client: createGHClient(&mockService{getContentError: &github.ErrorResponse{Message: "Not Found"}}), diff --git a/prompt.go b/prompt.go index 2a24272..3249616 100644 --- a/prompt.go +++ b/prompt.go @@ -12,14 +12,6 @@ func (i *InitActionCmd) setParams(dirs []string) error { var err error if i.DirPath == "" { prompt := promptui.Select{ - Label: "Choose migration directory", - Items: dirs, - Stdin: i.stdin, - } - if _, i.DirPath, err = prompt.Run(); err != nil { - return err - } - prompt = promptui.Select{ Label: "Choose driver", Items: []string{"mysql", "postgres", "mariadb", "sqlite"}, Stdin: i.stdin, @@ -27,6 +19,35 @@ func (i *InitActionCmd) setParams(dirs []string) error { if _, i.Driver, err = prompt.Run(); err != nil { return err } + switch { + case len(dirs) == 0: + prompt := promptui.Prompt{ + Label: "Enter the path of the migration directory in your repository", + Stdin: i.stdin, + } + if i.DirPath, err = prompt.Run(); err != nil { + return err + } + case len(dirs) > 0: + opts := append(dirs, "provide another path") + prompt := promptui.Select{ + Label: "Choose migration directory", + Items: opts, + Stdin: i.stdin, + } + if _, i.DirPath, err = prompt.Run(); err != nil { + return err + } + } + if i.DirPath == "provide another path" { + prompt := promptui.Prompt{ + Label: "Enter the path of the migration directory in your repository", + Stdin: i.stdin, + } + if i.DirPath, err = prompt.Run(); err != nil { + return err + } + } } if i.Token == "" { prompt := promptui.Prompt{