From eec0e2176b2b19cdc90e46e88662192d0d8d5922 Mon Sep 17 00:00:00 2001 From: sho-hata Date: Sat, 4 Jun 2022 16:43:11 +0900 Subject: [PATCH 1/4] update: replace deprecated package: ioutil -> os --- cmd/hasura/hasura.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/hasura/hasura.go b/cmd/hasura/hasura.go index 5dfadce..0920874 100644 --- a/cmd/hasura/hasura.go +++ b/cmd/hasura/hasura.go @@ -3,7 +3,7 @@ package hasura import ( "errors" "fmt" - "io/ioutil" + "os" "os/exec" "regexp" "strconv" @@ -104,7 +104,7 @@ func (h *HasuraCmd) setFileNames() error { return nil } - files, err := ioutil.ReadDir(filePath) + files, err := os.ReadDir(filePath) if err != nil { return err } @@ -133,7 +133,10 @@ func (h *HasuraCmd) findOne() (string, error) { for _, f := range h.fileNames { fileNames = append(fileNames, fileName{f}) } - i, err := fuzzyfinder.Find(fileNames, func(i int) string { return fileNames[i].name }) + i, err := fuzzyfinder.Find( + fileNames, + func(i int) string { return fileNames[i].name }, + ) if err != nil { return "", err } From 939ebed975b8b75cc0928cbc12ff58d4db749a01 Mon Sep 17 00:00:00 2001 From: sho-hata Date: Sun, 5 Jun 2022 16:29:54 +0900 Subject: [PATCH 2/4] update: implement preview window --- cmd/hasura/hasura.go | 88 +++++++++++++++++++++--------- cmd/hasura/hasura_test.go | 110 +++++++++++++++++++------------------- 2 files changed, 118 insertions(+), 80 deletions(-) diff --git a/cmd/hasura/hasura.go b/cmd/hasura/hasura.go index 0920874..8601416 100644 --- a/cmd/hasura/hasura.go +++ b/cmd/hasura/hasura.go @@ -1,6 +1,7 @@ package hasura import ( + "bufio" "errors" "fmt" "os" @@ -21,11 +22,16 @@ const ( var regex *regexp.Regexp type HasuraCmd struct { - called string - command []string - fileNames []string - options map[string]interface{} - target string + called string + command []string + options map[string]interface{} + files []fileInfo + applyTarget string +} + +type fileInfo struct { + name string + headline string } func NewHasuraCmd(called string, options map[string]interface{}) *HasuraCmd { @@ -56,11 +62,11 @@ func (h *HasuraCmd) exec() (string, error) { func (h *HasuraCmd) setCommand() *HasuraCmd { switch h.called { case CalledSeedApply: - h.command = append(strings.Split(h.called, " "), []string{"--file", h.target}...) + h.command = append(strings.Split(h.called, " "), []string{"--file", h.applyTarget}...) case calledMigrateApply, calledMigrateDelete: - h.command = append(strings.Split(h.called, " "), []string{"--version", h.target}...) + h.command = append(strings.Split(h.called, " "), []string{"--version", h.applyTarget}...) } - if h.target == "" { + if h.applyTarget == "" { h.command = strings.Split(h.called, " ") } @@ -78,7 +84,7 @@ func (h *HasuraCmd) setCommand() *HasuraCmd { } func (h *HasuraCmd) setTarget() error { - if len(h.fileNames) == 0 { + if len(h.files) == 0 { return nil } fileName, err := h.findOne() @@ -86,9 +92,9 @@ func (h *HasuraCmd) setTarget() error { return err } if h.called == calledMigrateApply || h.called == calledMigrateDelete { - h.target = trimVersion(fileName) + h.applyTarget = trimVersion(fileName) } else { - h.target = fileName + h.applyTarget = fileName } return nil } @@ -109,38 +115,49 @@ func (h *HasuraCmd) setFileNames() error { return err } if len(files) == 0 { - return errors.New("no file") + return errors.New("no such file or directory") } for _, file := range files { if file.IsDir() { if h.called == calledMigrateApply || h.called == calledMigrateDelete { - h.fileNames = append(h.fileNames, file.Name()) + headline, err := readFileHeadline("./migrations/default/" + file.Name() + "/up.sql") + if err != nil { + return err + } + h.files = append(h.files, fileInfo{name: file.Name(), headline: headline}) } } if !file.IsDir() && h.called == CalledSeedApply { - h.fileNames = append(h.fileNames, file.Name()) + headline, err := readFileHeadline("./seeds/default/" + file.Name()) + if err != nil { + return err + } + h.files = append(h.files, fileInfo{name: file.Name(), headline: headline}) } } return nil } func (h *HasuraCmd) findOne() (string, error) { - type fileName struct { - name string - } - var fileNames []fileName //nolint:prealloc // Since filenames include directory names, they are less in length than in capacity. - for _, f := range h.fileNames { - fileNames = append(fileNames, fileName{f}) - } i, err := fuzzyfinder.Find( - fileNames, - func(i int) string { return fileNames[i].name }, + h.files, + func(i int) string { return h.files[i].name }, + fuzzyfinder.WithPreviewWindow(func(i, width, height int) string { + if i == -1 { + return "" + } + return fmt.Sprintf(`📝 Applied SQL file + + %s + ... + `, h.files[i].headline) + }), ) if err != nil { return "", err } - return fileNames[i].name, nil + return h.files[i].name, nil } func trimVersion(fileName string) string { @@ -150,3 +167,26 @@ func trimVersion(fileName string) string { func setRegex() { regex = regexp.MustCompile(`^[0-9]+`) } + +// readFileHeadline +// Opens a file in the path passed as an argument, reads the first three lines, and returns them as a string. +func readFileHeadline(path string) (string, error) { + var headline string + f, err := os.Open(path) + if err != nil { + return "", err + } + defer f.Close() + scanner := bufio.NewScanner(f) + + var count int + for scanner.Scan() { + if count == 4 { + headline += scanner.Text() + break + } + headline += fmt.Sprintln(scanner.Text()) + count++ + } + return headline, nil +} diff --git a/cmd/hasura/hasura_test.go b/cmd/hasura/hasura_test.go index ecef701..1005766 100644 --- a/cmd/hasura/hasura_test.go +++ b/cmd/hasura/hasura_test.go @@ -9,11 +9,11 @@ import ( func TestHasuraCmd_setCommand(t *testing.T) { t.Parallel() type fields struct { - called string - command []string - fileNames []string - options map[string]interface{} - target string + called string + command []string + files []fileInfo + options map[string]interface{} + target string } tests := []struct { name string @@ -37,9 +37,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { target: "xxx.sql", }, want: &HasuraCmd{ - called: "seed apply", - target: "xxx.sql", - command: []string{"seed", "apply", "--file", "xxx.sql"}, + called: "seed apply", + applyTarget: "xxx.sql", + command: []string{"seed", "apply", "--file", "xxx.sql"}, }, }, { @@ -52,9 +52,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { }, }, want: &HasuraCmd{ - called: "seed apply", - target: "xxx.sql", - command: []string{"seed", "apply", "--file", "xxx.sql", "--admin-secret", "secret"}, + called: "seed apply", + applyTarget: "xxx.sql", + command: []string{"seed", "apply", "--file", "xxx.sql", "--admin-secret", "secret"}, options: map[string]interface{}{ "admin-secret": "secret", }, @@ -70,9 +70,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { }, }, want: &HasuraCmd{ - called: "seed apply", - target: "xxx.sql", - command: []string{"seed", "apply", "--file", "xxx.sql", "--no-color", "false"}, + called: "seed apply", + applyTarget: "xxx.sql", + command: []string{"seed", "apply", "--file", "xxx.sql", "--no-color", "false"}, options: map[string]interface{}{ "no-color": false, }, @@ -89,9 +89,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { }, }, want: &HasuraCmd{ - called: "seed apply", - target: "xxx.sql", - command: []string{"seed", "apply", "--file", "xxx.sql", "--admin-secret", "secret", "--no-color", "false"}, + called: "seed apply", + applyTarget: "xxx.sql", + command: []string{"seed", "apply", "--file", "xxx.sql", "--admin-secret", "secret", "--no-color", "false"}, options: map[string]interface{}{ "no-color": false, "admin-secret": "secret", @@ -105,8 +105,8 @@ func TestHasuraCmd_setCommand(t *testing.T) { target: "xxx.sql", }, want: &HasuraCmd{ - called: "migrate", - target: "xxx.sql", + called: "migrate", + applyTarget: "xxx.sql", }, }, { @@ -116,9 +116,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { target: "xxx", }, want: &HasuraCmd{ - called: "migrate apply", - target: "xxx", - command: []string{"migrate", "apply", "--version", "xxx"}, + called: "migrate apply", + applyTarget: "xxx", + command: []string{"migrate", "apply", "--version", "xxx"}, }, }, { @@ -131,9 +131,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { }, }, want: &HasuraCmd{ - called: "migrate apply", - target: "xxx", - command: []string{"migrate", "apply", "--version", "xxx", "--admin-secret", "secret"}, + called: "migrate apply", + applyTarget: "xxx", + command: []string{"migrate", "apply", "--version", "xxx", "--admin-secret", "secret"}, options: map[string]interface{}{ "admin-secret": "secret", }, @@ -149,9 +149,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { }, }, want: &HasuraCmd{ - called: "migrate apply", - target: "xxx", - command: []string{"migrate", "apply", "--version", "xxx", "--no-color", "false"}, + called: "migrate apply", + applyTarget: "xxx", + command: []string{"migrate", "apply", "--version", "xxx", "--no-color", "false"}, options: map[string]interface{}{ "no-color": false, }, @@ -168,9 +168,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { }, }, want: &HasuraCmd{ - called: "migrate apply", - target: "xxx", - command: []string{"migrate", "apply", "--version", "xxx", "--admin-secret", "secret", "--no-color", "false"}, + called: "migrate apply", + applyTarget: "xxx", + command: []string{"migrate", "apply", "--version", "xxx", "--admin-secret", "secret", "--no-color", "false"}, options: map[string]interface{}{ "no-color": false, "admin-secret": "secret", @@ -184,9 +184,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { target: "xxx", }, want: &HasuraCmd{ - called: "migrate delete", - target: "xxx", - command: []string{"migrate", "delete", "--version", "xxx"}, + called: "migrate delete", + applyTarget: "xxx", + command: []string{"migrate", "delete", "--version", "xxx"}, }, }, { @@ -199,9 +199,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { }, }, want: &HasuraCmd{ - called: "migrate delete", - target: "xxx", - command: []string{"migrate", "delete", "--version", "xxx", "--admin-secret", "secret"}, + called: "migrate delete", + applyTarget: "xxx", + command: []string{"migrate", "delete", "--version", "xxx", "--admin-secret", "secret"}, options: map[string]interface{}{ "admin-secret": "secret", }, @@ -217,9 +217,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { }, }, want: &HasuraCmd{ - called: "migrate delete", - target: "xxx", - command: []string{"migrate", "delete", "--version", "xxx", "--no-color", "false"}, + called: "migrate delete", + applyTarget: "xxx", + command: []string{"migrate", "delete", "--version", "xxx", "--no-color", "false"}, options: map[string]interface{}{ "no-color": false, }, @@ -236,9 +236,9 @@ func TestHasuraCmd_setCommand(t *testing.T) { }, }, want: &HasuraCmd{ - called: "migrate delete", - target: "xxx", - command: []string{"migrate", "delete", "--version", "xxx", "--admin-secret", "secret", "--no-color", "false"}, + called: "migrate delete", + applyTarget: "xxx", + command: []string{"migrate", "delete", "--version", "xxx", "--admin-secret", "secret", "--no-color", "false"}, options: map[string]interface{}{ "no-color": false, "admin-secret": "secret", @@ -252,8 +252,8 @@ func TestHasuraCmd_setCommand(t *testing.T) { target: "xxx", }, want: &HasuraCmd{ - called: "hoge", - target: "xxx", + called: "hoge", + applyTarget: "xxx", }, }, } @@ -262,29 +262,27 @@ func TestHasuraCmd_setCommand(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() h := &HasuraCmd{ - called: tt.fields.called, - command: tt.fields.command, - fileNames: tt.fields.fileNames, - options: tt.fields.options, - target: tt.fields.target, + called: tt.fields.called, + command: tt.fields.command, + files: tt.fields.files, + options: tt.fields.options, + applyTarget: tt.fields.target, } got := h.setCommand() sort.Strings(got.command) sort.Strings(tt.want.command) - sort.Strings(got.fileNames) - sort.Strings(tt.want.fileNames) if !reflect.DeepEqual(got.called, tt.want.called) { t.Errorf("HasuraCmd.setCommand() = %v, want.called %v", got.called, tt.want.called) } - if !reflect.DeepEqual(got.target, tt.want.target) { - t.Errorf("HasuraCmd.setCommand() = %v, want.target %v", got.target, tt.want.target) + if !reflect.DeepEqual(got.applyTarget, tt.want.applyTarget) { + t.Errorf("HasuraCmd.setCommand() = %v, want.target %v", got.applyTarget, tt.want.applyTarget) } if !reflect.DeepEqual(got.command, tt.want.command) { t.Errorf("HasuraCmd.setCommand() = %v, want.command %v", got.command, tt.want.command) } - if !reflect.DeepEqual(got.fileNames, tt.want.fileNames) { - t.Errorf("HasuraCmd.setCommand() = %v, want.fileNames %v", got.fileNames, tt.want.fileNames) + if !reflect.DeepEqual(got.files, tt.want.files) { + t.Errorf("HasuraCmd.setCommand() = %v, want.files %v", got.files, tt.want.files) } if !reflect.DeepEqual(got.options, tt.want.options) { for _, set := range tt.want.options { From 3146d740c416802be11beaf6343398e437d91196 Mon Sep 17 00:00:00 2001 From: sho-hata Date: Sun, 5 Jun 2022 17:37:20 +0900 Subject: [PATCH 3/4] update: README includes information about the preview window --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3042ac4..7664aaf 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This command has a fzf-like UI that allows you to find and run the file version used by the [hasura cli command](https://hasura.io/docs/latest/graphql/core/hasura-cli/index.html). - + ## install @@ -131,6 +131,7 @@ Compliant with [originnal](https://hasura.io/docs/latest/graphql/core/hasura-cli However, the `--version` option is not accepted. ## supported hasura cli command(wip) + You can run the hasura cli command as is. ## supported hasura config version From 7d8d5c94b7e1f2e224aaf61732c05cadcfea1320 Mon Sep 17 00:00:00 2001 From: sho-hata Date: Sun, 5 Jun 2022 17:41:30 +0900 Subject: [PATCH 4/4] chore: fix settings to golangci-lint action --- .github/workflows/test.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 98ffaaf..e33d6d8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,12 +7,12 @@ jobs: runs-on: ubuntu-latest steps: - name: set up - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: ^1.17 id: go - name: check out - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Cache uses: actions/cache@v2.1.0 @@ -26,7 +26,7 @@ jobs: needs: setup runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: build run: go build ./... @@ -34,7 +34,7 @@ jobs: needs: setup runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: test run: go test ./... -v @@ -42,8 +42,8 @@ jobs: needs: setup runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: golangci-lint - uses: golangci/golangci-lint-action@v2 + uses: golangci/golangci-lint-action@v3 with: version: v1.29