Swap ioutil to non-deprecated alternatives + clean up pre-commit errors

This commit is contained in:
David Dworken 2022-11-27 11:59:06 -08:00
parent 369e7ec8ea
commit 35444bf56e
No known key found for this signature in database
14 changed files with 60 additions and 53 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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")

View File

@ -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`)

View File

@ -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())
},
}

View File

@ -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())
},
}

View File

@ -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())
},
}

View File

@ -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())
},
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)
}