From 35444bf56e366e868aaeaf7b139501530f262263 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sun, 27 Nov 2022 11:59:06 -0800 Subject: [PATCH] Swap ioutil to non-deprecated alternatives + clean up pre-commit errors --- .errcheck_excludes.txt | 5 +++++ backend/server/server.go | 12 ++++++------ backend/server/server_test.go | 34 +++++++++++++++++----------------- client/client_test.go | 6 ++---- client/cmd/configAdd.go | 2 +- client/cmd/configDelete.go | 2 +- client/cmd/configGet.go | 3 ++- client/cmd/configSet.go | 2 +- client/cmd/install.go | 15 +++++++-------- client/cmd/saveHistoryEntry.go | 6 +++--- client/hctx/hctx.go | 3 +-- client/lib/lib.go | 5 ++--- client/lib/tui.go | 2 +- shared/testutils/testutils.go | 16 +++++++++++----- 14 files changed, 60 insertions(+), 53 deletions(-) diff --git a/.errcheck_excludes.txt b/.errcheck_excludes.txt index 47e5326..d529739 100644 --- a/.errcheck_excludes.txt +++ b/.errcheck_excludes.txt @@ -1,6 +1,11 @@ (net/http.ResponseWriter).Write (*gorm.io/gorm.DB).AutoMigrate os.Setenv +os.Unsetenv (*os.File).Close (io.ReadCloser).Close (*github.com/gofrs/flock.Flock).Unlock +(*database/sql.Rows).Close +(*github.com/DataDog/datadog-go/statsd.Client).Count +(*github.com/DataDog/datadog-go/statsd.Client).Incr +(*github.com/DataDog/datadog-go/statsd.Client).Distribution diff --git a/backend/server/server.go b/backend/server/server.go index 99ee1f3..9d8a676 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -5,7 +5,7 @@ import ( "encoding/json" "fmt" "html" - "io/ioutil" + "io" "log" "net/http" "os" @@ -160,7 +160,7 @@ func statsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { } func apiSubmitHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { - data, err := ioutil.ReadAll(r.Body) + data, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -301,7 +301,7 @@ func apiSubmitDumpHandler(ctx context.Context, w http.ResponseWriter, r *http.Re userId := getRequiredQueryParam(r, "user_id") srcDeviceId := getRequiredQueryParam(r, "source_device_id") requestingDeviceId := getRequiredQueryParam(r, "requesting_device_id") - data, err := ioutil.ReadAll(r.Body) + data, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -358,7 +358,7 @@ func getDeletionRequestsHandler(ctx context.Context, w http.ResponseWriter, r *h } func addDeletionRequestHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { - data, err := ioutil.ReadAll(r.Body) + data, err := io.ReadAll(r.Body) if err != nil { panic(err) } @@ -570,7 +570,7 @@ func updateReleaseVersion() error { if err != nil { return fmt.Errorf("failed to get latest release version: %v", err) } - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read github API response body: %v", err) } @@ -707,7 +707,7 @@ func slsaStatusHandler(ctx context.Context, w http.ResponseWriter, r *http.Reque } func feedbackHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { - data, err := ioutil.ReadAll(r.Body) + data, err := io.ReadAll(r.Body) if err != nil { panic(err) } diff --git a/backend/server/server_test.go b/backend/server/server_test.go index 59801d3..ac89dcc 100644 --- a/backend/server/server_test.go +++ b/backend/server/server_test.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "encoding/json" - "io/ioutil" + "io" "net/http" "net/http/httptest" "strings" @@ -51,7 +51,7 @@ func TestESubmitThenQuery(t *testing.T) { apiQueryHandler(context.Background(), w, searchReq) res := w.Result() defer res.Body.Close() - respBody, err := ioutil.ReadAll(res.Body) + respBody, err := io.ReadAll(res.Body) testutils.Check(t, err) var retrievedEntries []*shared.EncHistoryEntry testutils.Check(t, json.Unmarshal(respBody, &retrievedEntries)) @@ -80,7 +80,7 @@ func TestESubmitThenQuery(t *testing.T) { apiQueryHandler(context.Background(), w, searchReq) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) testutils.Check(t, json.Unmarshal(respBody, &retrievedEntries)) if len(retrievedEntries) != 1 { @@ -108,7 +108,7 @@ func TestESubmitThenQuery(t *testing.T) { apiBootstrapHandler(context.Background(), w, searchReq) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) testutils.Check(t, json.Unmarshal(respBody, &retrievedEntries)) if len(retrievedEntries) != 2 { @@ -144,7 +144,7 @@ func TestDumpRequestAndResponse(t *testing.T) { apiGetPendingDumpRequestsHandler(context.Background(), w, httptest.NewRequest(http.MethodGet, "/?user_id="+userId+"&device_id="+devId1, nil)) res := w.Result() defer res.Body.Close() - respBody, err := ioutil.ReadAll(res.Body) + respBody, err := io.ReadAll(res.Body) testutils.Check(t, err) var dumpRequests []*shared.DumpRequest testutils.Check(t, json.Unmarshal(respBody, &dumpRequests)) @@ -164,7 +164,7 @@ func TestDumpRequestAndResponse(t *testing.T) { apiGetPendingDumpRequestsHandler(context.Background(), w, httptest.NewRequest(http.MethodGet, "/?user_id="+otherUser+"&device_id="+otherDev1, nil)) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) dumpRequests = make([]*shared.DumpRequest, 0) testutils.Check(t, json.Unmarshal(respBody, &dumpRequests)) @@ -184,7 +184,7 @@ func TestDumpRequestAndResponse(t *testing.T) { apiGetPendingDumpRequestsHandler(context.Background(), w, httptest.NewRequest(http.MethodGet, "/?user_id=foo&device_id=bar", nil)) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) if string(respBody) != "[]" { t.Fatalf("got unexpected respBody: %#v", string(respBody)) @@ -195,7 +195,7 @@ func TestDumpRequestAndResponse(t *testing.T) { apiGetPendingDumpRequestsHandler(context.Background(), w, httptest.NewRequest(http.MethodGet, "/?user_id=%20&device_id=%20", nil)) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) if string(respBody) != "[]" { t.Fatalf("got unexpected respBody: %#v", string(respBody)) @@ -218,7 +218,7 @@ func TestDumpRequestAndResponse(t *testing.T) { apiGetPendingDumpRequestsHandler(context.Background(), w, httptest.NewRequest(http.MethodGet, "/?user_id="+userId+"&device_id="+devId1, nil)) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) if string(respBody) != "[]" { t.Fatalf("got unexpected respBody: %#v", string(respBody)) @@ -228,7 +228,7 @@ func TestDumpRequestAndResponse(t *testing.T) { apiGetPendingDumpRequestsHandler(context.Background(), w, httptest.NewRequest(http.MethodGet, "/?user_id="+userId+"&device_id="+devId2, nil)) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) if string(respBody) != "[]" { t.Fatalf("got unexpected respBody: %#v", string(respBody)) @@ -239,7 +239,7 @@ func TestDumpRequestAndResponse(t *testing.T) { apiGetPendingDumpRequestsHandler(context.Background(), w, httptest.NewRequest(http.MethodGet, "/?user_id="+otherUser+"&device_id="+otherDev1, nil)) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) dumpRequests = make([]*shared.DumpRequest, 0) testutils.Check(t, json.Unmarshal(respBody, &dumpRequests)) @@ -260,7 +260,7 @@ func TestDumpRequestAndResponse(t *testing.T) { apiQueryHandler(context.Background(), w, searchReq) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) var retrievedEntries []*shared.EncHistoryEntry testutils.Check(t, json.Unmarshal(respBody, &retrievedEntries)) @@ -377,7 +377,7 @@ func TestDeletionRequests(t *testing.T) { apiQueryHandler(context.Background(), w, searchReq) res := w.Result() defer res.Body.Close() - respBody, err := ioutil.ReadAll(res.Body) + respBody, err := io.ReadAll(res.Body) testutils.Check(t, err) var retrievedEntries []*shared.EncHistoryEntry testutils.Check(t, json.Unmarshal(respBody, &retrievedEntries)) @@ -422,7 +422,7 @@ func TestDeletionRequests(t *testing.T) { apiQueryHandler(context.Background(), w, searchReq) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) testutils.Check(t, json.Unmarshal(respBody, &retrievedEntries)) if len(retrievedEntries) != 1 { @@ -450,7 +450,7 @@ func TestDeletionRequests(t *testing.T) { apiQueryHandler(context.Background(), w, searchReq) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) testutils.Check(t, json.Unmarshal(respBody, &retrievedEntries)) if len(retrievedEntries) != 1 { @@ -478,7 +478,7 @@ func TestDeletionRequests(t *testing.T) { getDeletionRequestsHandler(context.Background(), w, searchReq) res = w.Result() defer res.Body.Close() - respBody, err = ioutil.ReadAll(res.Body) + respBody, err = io.ReadAll(res.Body) testutils.Check(t, err) var deletionRequests []*shared.DeletionRequest testutils.Check(t, json.Unmarshal(respBody, &deletionRequests)) @@ -511,7 +511,7 @@ func TestHealthcheck(t *testing.T) { } res := w.Result() defer res.Body.Close() - respBody, err := ioutil.ReadAll(res.Body) + respBody, err := io.ReadAll(res.Body) testutils.Check(t, err) if string(respBody) != "OK" { t.Fatalf("expected healthcheckHandler to return OK") diff --git a/client/client_test.go b/client/client_test.go index 686c7f8..eed39a3 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -2230,12 +2230,10 @@ func TestZDotDir(t *testing.T) { defer testutils.BackupAndRestore(t)() defer testutils.BackupAndRestoreEnv("ZDOTDIR")() homedir, err := os.UserHomeDir() - if err != nil { - t.Fatalf("failed to get homedir: %v", err) - } + testutils.Check(t, err) os.Setenv("ZDOTDIR", path.Join(homedir, data.HISHTORY_PATH)) installHishtory(t, tester, "") - defer os.Remove(path.Join(homedir, data.HISHTORY_PATH, ".zshrc")) + defer testutils.Check(t, os.Remove(path.Join(homedir, data.HISHTORY_PATH, ".zshrc"))) // Run a command and check that it was recorded tester.RunInteractiveShell(t, `echo foo`) diff --git a/client/cmd/configAdd.go b/client/cmd/configAdd.go index 45ff04b..daf3f06 100644 --- a/client/cmd/configAdd.go +++ b/client/cmd/configAdd.go @@ -11,7 +11,7 @@ var configAddCmd = &cobra.Command{ Short: "Add a config option", GroupID: GROUP_ID_CONFIG, Run: func(cmd *cobra.Command, args []string) { - cmd.Help() + lib.CheckFatalError(cmd.Help()) }, } diff --git a/client/cmd/configDelete.go b/client/cmd/configDelete.go index 26a9b64..8d88f0d 100644 --- a/client/cmd/configDelete.go +++ b/client/cmd/configDelete.go @@ -13,7 +13,7 @@ var configDeleteCmd = &cobra.Command{ Short: "Delete a config option", GroupID: GROUP_ID_CONFIG, Run: func(cmd *cobra.Command, args []string) { - cmd.Help() + lib.CheckFatalError(cmd.Help()) }, } diff --git a/client/cmd/configGet.go b/client/cmd/configGet.go index efefda9..1056070 100644 --- a/client/cmd/configGet.go +++ b/client/cmd/configGet.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/ddworken/hishtory/client/hctx" + "github.com/ddworken/hishtory/client/lib" "github.com/spf13/cobra" ) @@ -15,7 +16,7 @@ var configGetCmd = &cobra.Command{ Short: "Get the value of a config option", GroupID: GROUP_ID_CONFIG, Run: func(cmd *cobra.Command, args []string) { - cmd.Help() + lib.CheckFatalError(cmd.Help()) }, } diff --git a/client/cmd/configSet.go b/client/cmd/configSet.go index 8a5d81e..71d1cc5 100644 --- a/client/cmd/configSet.go +++ b/client/cmd/configSet.go @@ -14,7 +14,7 @@ var configSetCmd = &cobra.Command{ Short: "Set the value of a config option", GroupID: GROUP_ID_CONFIG, Run: func(cmd *cobra.Command, args []string) { - cmd.Help() + lib.CheckFatalError(cmd.Help()) }, } diff --git a/client/cmd/install.go b/client/cmd/install.go index 4d7a453..decd9ff 100644 --- a/client/cmd/install.go +++ b/client/cmd/install.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "os/exec" "path" @@ -234,7 +233,7 @@ func configureFish(homedir, binaryPath string) error { } configContents = testConfig } - err = ioutil.WriteFile(getFishConfigPath(homedir), []byte(configContents), 0o644) + err = os.WriteFile(getFishConfigPath(homedir), []byte(configContents), 0o644) if err != nil { return fmt.Errorf("failed to write config.zsh file: %v", err) } @@ -263,7 +262,7 @@ func isFishConfigured(homedir string) (bool, error) { if errors.Is(err, os.ErrNotExist) { return false, nil } - fishConfig, err := ioutil.ReadFile(path.Join(homedir, ".config/fish/config.fish")) + fishConfig, err := os.ReadFile(path.Join(homedir, ".config/fish/config.fish")) if err != nil { return false, fmt.Errorf("failed to read ~/.config/fish/config.fish: %v", err) } @@ -284,7 +283,7 @@ func configureZshrc(homedir, binaryPath string) error { } configContents = testConfig } - err := ioutil.WriteFile(getZshConfigPath(homedir), []byte(configContents), 0o644) + err := os.WriteFile(getZshConfigPath(homedir), []byte(configContents), 0o644) if err != nil { return fmt.Errorf("failed to write config.zsh file: %v", err) } @@ -316,7 +315,7 @@ func isZshConfigured(homedir string) (bool, error) { if errors.Is(err, os.ErrNotExist) { return false, nil } - bashrc, err := ioutil.ReadFile(getZshRcPath(homedir)) + bashrc, err := os.ReadFile(getZshRcPath(homedir)) if err != nil { return false, fmt.Errorf("failed to read zshrc: %v", err) } @@ -337,7 +336,7 @@ func configureBashrc(homedir, binaryPath string) error { } configContents = testConfig } - err := ioutil.WriteFile(getBashConfigPath(homedir), []byte(configContents), 0o644) + err := os.WriteFile(getBashConfigPath(homedir), []byte(configContents), 0o644) if err != nil { return fmt.Errorf("failed to write config.sh file: %v", err) } @@ -388,7 +387,7 @@ func isBashRcConfigured(homedir string) (bool, error) { if errors.Is(err, os.ErrNotExist) { return false, nil } - bashrc, err := ioutil.ReadFile(path.Join(homedir, ".bashrc")) + bashrc, err := os.ReadFile(path.Join(homedir, ".bashrc")) if err != nil { return false, fmt.Errorf("failed to read bashrc: %v", err) } @@ -400,7 +399,7 @@ func isBashProfileConfigured(homedir string) (bool, error) { if errors.Is(err, os.ErrNotExist) { return false, nil } - bashrc, err := ioutil.ReadFile(path.Join(homedir, ".bash_profile")) + bashrc, err := os.ReadFile(path.Join(homedir, ".bash_profile")) if err != nil { return false, fmt.Errorf("failed to read bash_profile: %v", err) } diff --git a/client/cmd/saveHistoryEntry.go b/client/cmd/saveHistoryEntry.go index c9fed2a..02bb60c 100644 --- a/client/cmd/saveHistoryEntry.go +++ b/client/cmd/saveHistoryEntry.go @@ -15,9 +15,9 @@ import ( ) var saveHistoryEntryCmd = &cobra.Command{ - Use: "saveHistoryEntry", - Hidden: true, - Short: "[Internal-only] The command used to save history entries", + Use: "saveHistoryEntry", + Hidden: true, + Short: "[Internal-only] The command used to save history entries", DisableFlagParsing: true, Run: func(cmd *cobra.Command, args []string) { ctx := hctx.MakeContext() diff --git a/client/hctx/hctx.go b/client/hctx/hctx.go index 74c6074..ad8fa80 100644 --- a/client/hctx/hctx.go +++ b/client/hctx/hctx.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "os" "path" "sync" @@ -191,7 +190,7 @@ func GetConfigContents() ([]byte, error) { } dat, err := os.ReadFile(path.Join(homedir, data.HISHTORY_PATH, data.CONFIG_PATH)) if err != nil { - files, err := ioutil.ReadDir(path.Join(homedir, data.HISHTORY_PATH)) + files, err := os.ReadDir(path.Join(homedir, data.HISHTORY_PATH)) if err != nil { return nil, fmt.Errorf("failed to read config file (and failed to list too): %v", err) } diff --git a/client/lib/lib.go b/client/lib/lib.go index 9511b45..98a1840 100644 --- a/client/lib/lib.go +++ b/client/lib/lib.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "math/rand" "net/http" @@ -898,7 +897,7 @@ func ApiGet(path string) ([]byte, error) { if resp.StatusCode != 200 { return nil, fmt.Errorf("failed to GET %s%s: status_code=%d", getServerHostname(), path, resp.StatusCode) } - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read response body from GET %s%s: %v", getServerHostname(), path, err) } @@ -926,7 +925,7 @@ func ApiPost(path, contentType string, data []byte) ([]byte, error) { if resp.StatusCode != 200 { return nil, fmt.Errorf("failed to POST %s: status_code=%d", path, resp.StatusCode) } - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read response body from POST %s: %v", path, err) } diff --git a/client/lib/tui.go b/client/lib/tui.go index adb41c7..5b268cf 100644 --- a/client/lib/tui.go +++ b/client/lib/tui.go @@ -444,7 +444,7 @@ func TuiQuery(ctx *context.Context, initialQuery string) error { p.Send(bannerMsg{banner: string(banner)}) }() // Blocking: Start the TUI - err = p.Start() + _, err = p.Run() if err != nil { return err } diff --git a/shared/testutils/testutils.go b/shared/testutils/testutils.go index 7166942..2082966 100644 --- a/shared/testutils/testutils.go +++ b/shared/testutils/testutils.go @@ -116,14 +116,20 @@ func touchFile(p string) { } func configureZshrc(homedir string) { - f, err := os.OpenFile(path.Join(homedir, ".zshrc"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - checkError(err) - defer f.Close() - _, err = f.WriteString(`export HISTFILE=~/.zsh_history + zshrcHistConfig := `export HISTFILE=~/.zsh_history export HISTSIZE=10000 export SAVEHIST=1000 setopt SHARE_HISTORY -`) +` + dat, err := os.ReadFile(path.Join(homedir, ".zshrc")) + checkError(err) + if strings.Contains(string(dat), zshrcHistConfig) { + return + } + f, err := os.OpenFile(path.Join(homedir, ".zshrc"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + checkError(err) + defer f.Close() + _, err = f.WriteString(zshrcHistConfig) checkError(err) }