Skip to content

Commit

Permalink
refactor: Address golangci issues and introduce goimports (#99)
Browse files Browse the repository at this point in the history
* Address io/ioutil deprecation. Replace it with os and io equivalents.
* Add goimports as import statements flips occasionally
* Replace deprecated structcheck with unused
* Run gofmt for all
  • Loading branch information
draftcode authored May 3, 2023
1 parent ca898d4 commit 442e6ec
Show file tree
Hide file tree
Showing 37 changed files with 101 additions and 74 deletions.
5 changes: 3 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ linters:
- exhaustive
- importas
- gofmt
- goimports
- gosec
- structcheck
- unused
- tagliatelle
- wastedassign

issues:
exclude:
- "Expect WriteFile permissions to be 0600 or less"
- "Subprocess launched with a potential tainted input or cmd arguments"
- "Subprocess launched with a potential tainted input or cmd arguments"
4 changes: 2 additions & 2 deletions cmd/av/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"context"
"io/ioutil"
"io"
"os"
"strings"

Expand Down Expand Up @@ -65,7 +65,7 @@ Examples:
body := prCreateFlags.Body
// Special case: ready body from stdin
if prCreateFlags.Body == "-" {
bodyBytes, err := ioutil.ReadAll(os.Stdin)
bodyBytes, err := io.ReadAll(os.Stdin)
if err != nil {
return errors.Wrap(err, "failed to read body from stdin")
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/av/stack_reparent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"os"

"github.com/aviator-co/av/internal/utils/colors"
"github.com/spf13/cobra"
"os"
)

var stackReparentCmd = &cobra.Command{
Expand Down
7 changes: 3 additions & 4 deletions cmd/av/stack_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"golang.org/x/exp/slices"
"io/ioutil"
"os"
"path"
"strings"
Expand All @@ -20,6 +18,7 @@ import (
"github.com/kr/text"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)

// stackSyncConfig contains the configuration for a sync operation.
Expand Down Expand Up @@ -332,7 +331,7 @@ const stackSyncStateFile = "stack-sync.state.json"

func readStackSyncState(repo *git.Repo) (stackSyncState, error) {
var state stackSyncState
data, err := ioutil.ReadFile(path.Join(repo.GitDir(), "av", stackSyncStateFile))
data, err := os.ReadFile(path.Join(repo.GitDir(), "av", stackSyncStateFile))
if err != nil {
return state, err
}
Expand Down Expand Up @@ -367,7 +366,7 @@ func writeStackSyncState(repo *git.Repo, state *stackSyncState) error {
if err != nil {
return err
}
return ioutil.WriteFile(path.Join(avDir, stackSyncStateFile), data, 0644)
return os.WriteFile(path.Join(avDir, stackSyncStateFile), data, 0644)
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions cmd/av/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/aviator-co/av/internal/config"
"github.com/spf13/cobra"
)
Expand Down
9 changes: 5 additions & 4 deletions e2e_tests/av.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package e2e_tests

import (
"bytes"
"emperror.dev/errors"
"fmt"
"github.com/kr/text"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"os"
"os/exec"
"path/filepath"
"testing"

"emperror.dev/errors"
"github.com/kr/text"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)

var avCmdPath string
Expand Down
3 changes: 2 additions & 1 deletion e2e_tests/helpers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package e2e_tests

import (
"testing"

"github.com/aviator-co/av/internal/git"
"github.com/stretchr/testify/require"
"testing"
)

func RequireCurrentBranchName(t *testing.T, repo *git.Repo, name string) {
Expand Down
3 changes: 2 additions & 1 deletion e2e_tests/stack_branch_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package e2e_tests

import (
"testing"

"github.com/aviator-co/av/internal/git/gittest"
"github.com/aviator-co/av/internal/meta/jsonfiledb"
"github.com/stretchr/testify/require"
"testing"
)

func TestStackBranchMove(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions e2e_tests/stack_reparent_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package e2e_tests

import (
"os"
"testing"

"github.com/aviator-co/av/internal/git/gittest"
"github.com/stretchr/testify/require"
"io/ioutil"
"testing"
)

func TestStackSyncReparent(t *testing.T) {
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestStackSyncReparent(t *testing.T) {
}

func requireFileContent(t *testing.T, file string, expected string, args ...any) {
actual, err := ioutil.ReadFile(file)
actual, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 3 additions & 2 deletions e2e_tests/stack_sync_amend_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package e2e_tests

import (
"github.com/aviator-co/av/internal/git/gittest"
"github.com/stretchr/testify/require"
"os"
"testing"

"github.com/aviator-co/av/internal/git/gittest"
"github.com/stretchr/testify/require"
)

func TestSyncAfterAmendingCommit(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion e2e_tests/stack_sync_merge_commit_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package e2e_tests

import (
"testing"

"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/git/gittest"
"github.com/aviator-co/av/internal/meta/jsonfiledb"
"github.com/stretchr/testify/require"
"testing"
)

func TestStackSyncMergeCommit(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions e2e_tests/stack_sync_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package e2e_tests

import (
"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/git/gittest"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"testing"

"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/git/gittest"
"github.com/stretchr/testify/require"
)

func TestHelp(t *testing.T) {
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestStackSync(t *testing.T) {
"stack sync --continue should return non-zero exit code if conflicts have not been resolved",
)
// resolve the conflict
err := ioutil.WriteFile(filepath.Join(repo.Dir(), "my-file"), []byte("1a\n1b\n2a\n"), 0644)
err := os.WriteFile(filepath.Join(repo.Dir(), "my-file"), []byte("1a\n1b\n2a\n"), 0644)
require.NoError(t, err)
_, err = repo.Git("add", "my-file")
require.NoError(t, err, "failed to stage file")
Expand Down
5 changes: 3 additions & 2 deletions internal/actions/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package actions

import (
"fmt"
"os"
"strings"

"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/utils/colors"
"github.com/kr/text"
"os"
"strings"
)

func msgRebaseResult(rebase *git.RebaseResult) {
Expand Down
3 changes: 2 additions & 1 deletion internal/actions/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package actions_test

import (
"fmt"
"testing"

"github.com/aviator-co/av/internal/actions"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestReadPRMetadata(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions internal/actions/sync_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
)

type SyncBranchOpts struct {
Branch string
Fetch bool
Push bool
Branch string
Fetch bool
Push bool
// If specified, synchronize the branch against the latest version of the
// trunk branch. This value is ignored if the branch is not a stack root.
ToTrunk bool
Expand Down Expand Up @@ -80,7 +80,6 @@ func SyncBranch(
_, _ = fmt.Fprint(os.Stderr, colors.Failure(" - error: ", err.Error()), "\n")
return nil, errors.Wrap(err, "failed to fetch latest PR info")
}
branch = update.Branch
pull = update.Pull
if update.Changed {
_, _ = fmt.Fprint(os.Stderr, " - found updated pull request: ", colors.UserInput(update.Pull.Permalink), "\n")
Expand Down
3 changes: 2 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package config

import (
"os"

"emperror.dev/errors"
"github.com/spf13/viper"
"os"
)

type GitHub struct {
Expand Down
5 changes: 2 additions & 3 deletions internal/config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"time"
Expand All @@ -28,7 +27,7 @@ func FetchLatestVersion() (string, error) {
stat, _ := os.Stat(cacheFile)

if stat != nil && time.Since(stat.ModTime()) <= (24*time.Hour) {
data, err := ioutil.ReadFile(cacheFile)
data, err := os.ReadFile(cacheFile)
if err != nil {
return "", err
}
Expand All @@ -55,7 +54,7 @@ func FetchLatestVersion() (string, error) {
return "", err
}

if err := ioutil.WriteFile(cacheFile, []byte(data.Name), os.ModePerm); err != nil {
if err := os.WriteFile(cacheFile, []byte(data.Name), os.ModePerm); err != nil {
return "", err
}

Expand Down
7 changes: 4 additions & 3 deletions internal/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package editor
import (
"bufio"
"bytes"
"os"
"os/exec"
"strings"

"emperror.dev/errors"
"github.com/aviator-co/av/internal/git"
"github.com/kballard/go-shellquote"
"github.com/sirupsen/logrus"
"os"
"os/exec"
"strings"
)

type Config struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/editor/editor_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package editor

import (
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"
)

func TestEditor(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/gh/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -108,7 +108,7 @@ func (c *Client) restPost(ctx context.Context, endpoint string, body interface{}
}
defer res.Body.Close()

resBody, err := ioutil.ReadAll(res.Body)
resBody, err := io.ReadAll(res.Body)
if err != nil {
return errors.Wrap(err, "failed to read response body")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/gh/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package gh

import (
"context"
"strings"

"emperror.dev/errors"
"github.com/shurcooL/githubv4"
"strings"
)

type Repository struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/git/catfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package git

import (
"bytes"
"emperror.dev/errors"
"fmt"
"io"

"emperror.dev/errors"
)

type GetRefs struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/git/diff.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package git

import (
"emperror.dev/errors"
"os/exec"

"emperror.dev/errors"
)

type DiffOpts struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/git/err.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package git

import (
"emperror.dev/errors"
"os/exec"
"strings"

"emperror.dev/errors"
)

func StderrMatches(err error, target string) bool {
Expand Down
Loading

0 comments on commit 442e6ec

Please sign in to comment.