From 685c222b9d577b23362aaa95d2022eda5db44378 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Mon, 14 Nov 2022 15:43:34 -0600 Subject: [PATCH 1/5] Lint: replace os.Kill trap with syscall.SIGTERM --- pkg/cli/cli.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 64d527f..ed59413 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -7,6 +7,7 @@ import ( "os/signal" "regexp" "strings" + "syscall" "github.com/gin-gonic/gin" "github.com/jessevdk/go-flags" @@ -203,7 +204,7 @@ func startServer() { func handleSignals() { c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt, os.Kill) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) <-c } From c706523bd053a425a203137c9dd5950975bafeab Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Mon, 14 Nov 2022 15:47:36 -0600 Subject: [PATCH 2/5] Lint: replace ioutil with os package --- pkg/bookmarks/bookmarks.go | 6 +++--- pkg/client/tunnel.go | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/bookmarks/bookmarks.go b/pkg/bookmarks/bookmarks.go index 2648b04..b81b930 100644 --- a/pkg/bookmarks/bookmarks.go +++ b/pkg/bookmarks/bookmarks.go @@ -2,7 +2,7 @@ package bookmarks import ( "fmt" - "io/ioutil" + "os" "path/filepath" "strings" @@ -46,7 +46,7 @@ func (b Bookmark) ConvertToOptions() command.Options { func readServerConfig(path string) (Bookmark, error) { bookmark := Bookmark{} - buff, err := ioutil.ReadFile(path) + buff, err := os.ReadFile(path) if err != nil { return bookmark, err } @@ -100,7 +100,7 @@ func Path(overrideDir string) string { func ReadAll(path string) (map[string]Bookmark, error) { results := map[string]Bookmark{} - files, err := ioutil.ReadDir(path) + files, err := os.ReadDir(path) if err != nil { return results, err } diff --git a/pkg/client/tunnel.go b/pkg/client/tunnel.go index 784c01e..685cc0b 100644 --- a/pkg/client/tunnel.go +++ b/pkg/client/tunnel.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "net" "net/url" @@ -54,7 +53,7 @@ func fileExists(path string) bool { } func parsePrivateKey(keyPath string, keyPass string) (ssh.Signer, error) { - buff, err := ioutil.ReadFile(keyPath) + buff, err := os.ReadFile(keyPath) if err != nil { return nil, err } From 580c1093d1a1cc59d634cac70ecd9bce154a2fa4 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Mon, 14 Nov 2022 15:49:30 -0600 Subject: [PATCH 3/5] Add lint into github workflow --- .github/workflows/checks.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index eaf83bf..6c954a3 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -42,6 +42,24 @@ jobs: PGPASSWORD: postgres PGDATABASE: booktown + lint: + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.50.1 + fmt: name: fmt runs-on: ubuntu-latest From 9bfec11b48f9cd537bba391299cce96b9e59890b Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Mon, 14 Nov 2022 16:10:50 -0600 Subject: [PATCH 4/5] Fix linting errors --- Makefile | 3 +++ pkg/api/api.go | 2 +- pkg/api/backend_test.go | 15 +++++++++++---- pkg/cli/cli.go | 5 ++++- pkg/client/client_test.go | 12 ++++++++---- pkg/client/result.go | 5 ++++- pkg/connection/port.go | 5 +---- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index ed2a2ea..722ecc1 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,9 @@ test-all: @./script/test_all.sh @./script/test_cockroach.sh +lint: + golangci-lint run + dev: go build @echo "You can now execute ./pgweb" diff --git a/pkg/api/api.go b/pkg/api/api.go index c951727..b1a6a3a 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -533,7 +533,7 @@ func DataExport(c *gin.Context) { if dump.Table != "" { filename = filename + "_" + dump.Table } - reg := regexp.MustCompile("[^._\\w]+") + reg := regexp.MustCompile(`[^._\\w]+`) cleanFilename := reg.ReplaceAllString(filename, "") c.Header( diff --git a/pkg/api/backend_test.go b/pkg/api/backend_test.go index c10be7e..c0110a0 100644 --- a/pkg/api/backend_test.go +++ b/pkg/api/backend_test.go @@ -139,10 +139,17 @@ func startTestBackend(ctx context.Context, listenAddr string) { }) server := &http.Server{Addr: listenAddr, Handler: router} - go server.ListenAndServe() + go mustStartServer(server) - select { - case <-ctx.Done(): - server.Shutdown(context.Background()) + <-ctx.Done() + if err := server.Shutdown(context.Background()); err != nil && err != http.ErrServerClosed { + panic(err) + } +} + +func mustStartServer(server *http.Server) { + err := server.ListenAndServe() + if err != nil && err != http.ErrServerClosed { + panic(err) } } diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index ed59413..d0954ef 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -221,7 +221,10 @@ func openPage() { return } - exec.Command("open", url).Output() + _, err = exec.Command("open", url).Output() + if err != nil { + fmt.Println("Unable to auto-open pgweb URL:", err) + } } func Run() { diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 4d3f59d..ef346ca 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -26,7 +26,7 @@ var ( func mapKeys(data map[string]*Objects) []string { result := []string{} - for k, _ := range data { + for k := range data { result = append(result, k) } return result @@ -421,8 +421,10 @@ func testHistoryUniqueness(t *testing.T) { url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase) client, _ := NewFromUrl(url, nil) - client.Query("SELECT * FROM books WHERE id = 1") - client.Query("SELECT * FROM books WHERE id = 1") + for i := 0; i < 3; i++ { + _, err := client.Query("SELECT * FROM books WHERE id = 1") + assert.NoError(t, err) + } assert.Equal(t, 1, len(client.History)) assert.Equal(t, "SELECT * FROM books WHERE id = 1", client.History[0].Query) @@ -445,7 +447,8 @@ func testReadOnlyMode(t *testing.T) { assert.Error(t, err, "query contains keywords not allowed in read-only mode") // Turn off guard - client.db.Exec("SET default_transaction_read_only=off;") + _, err = client.db.Exec("SET default_transaction_read_only=off;") + assert.NoError(t, err) _, err = client.Query("\nCREATE TABLE foobar(id integer);\n") assert.NotNil(t, err) @@ -471,6 +474,7 @@ func TestAll(t *testing.T) { setupClient() testNewClientFromUrl(t) + testNewClientFromUrl2(t) testClientIdleTime(t) testTest(t) testInfo(t) diff --git a/pkg/client/result.go b/pkg/client/result.go index 4cef669..88d7da0 100644 --- a/pkg/client/result.go +++ b/pkg/client/result.go @@ -5,6 +5,7 @@ import ( "encoding/csv" "encoding/json" "fmt" + "log" "math" "strconv" "time" @@ -88,7 +89,9 @@ func (res *Result) CSV() []byte { buff := &bytes.Buffer{} writer := csv.NewWriter(buff) - writer.Write(res.Columns) + if err := writer.Write(res.Columns); err != nil { + log.Printf("result csv write error: %v\n", err) + } for _, row := range res.Rows { record := make([]string, len(res.Columns)) diff --git a/pkg/connection/port.go b/pkg/connection/port.go index a596730..fc6c33b 100644 --- a/pkg/connection/port.go +++ b/pkg/connection/port.go @@ -11,10 +11,7 @@ import ( func IsPortAvailable(port int) bool { conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%v", port)) if err != nil { - if strings.Index(err.Error(), "connection refused") > 0 { - return true - } - return false + return strings.Index(err.Error(), "connection refused") > 0 } conn.Close() From a5143ad9d398407291c7cc1912ed066826b99f35 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Mon, 14 Nov 2022 16:14:50 -0600 Subject: [PATCH 5/5] Use checkout/setup v3 actions --- .github/workflows/checks.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 6c954a3..0b14b51 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -26,10 +26,10 @@ jobs: --health-timeout 5s --health-retries 5 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 with: fetch-depth: 0 - - uses: actions/setup-go@v2 + - uses: actions/setup-go@v3 with: go-version: ${{ env.GO_VERSION }} - run: go mod download @@ -64,10 +64,10 @@ jobs: name: fmt runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 with: fetch-depth: 0 - - uses: actions/setup-go@v2 + - uses: actions/setup-go@v3 with: go-version: ${{ env.GO_VERSION }} - run: go mod download