Skip to content

Commit

Permalink
Add golangci-lint linter (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
majd authored May 27, 2023
1 parent bf33e0d commit 67e557e
Show file tree
Hide file tree
Showing 46 changed files with 295 additions and 110 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Lint

on:
pull_request:
branches:
- main

jobs:
lint:
name: Lint
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v3
with:
go-version: '1.19.3'
cache: true
- run: go generate github.com/majd/ipatool/...
- uses: golangci/golangci-lint-action@v3
with:
version: v1.52.2
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
jobs:
run_tests:
name: Run tests
runs-on: macos-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v3
Expand Down
22 changes: 22 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
linters:
enable:
- errcheck
- govet
- goimports
- gofmt
- unused
- ginkgolinter
- godot
- godox
- importas
- nlreturn
- nonamedreturns
- prealloc
- predeclared
- tenv
- unconvert
- unparam
- usestdlibvars
- wastedassign
- wrapcheck
- wsl
22 changes: 13 additions & 9 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/99designs/keyring"
"github.com/avast/retry-go"
"github.com/majd/ipatool/pkg/appstore"
"github.com/majd/ipatool/pkg/util"
"github.com/spf13/cobra"
"golang.org/x/term"
"os"
"strings"
"time"
)

func authCmd() *cobra.Command {
Expand All @@ -34,10 +35,9 @@ func authCmd() *cobra.Command {

func loginCmd() *cobra.Command {
promptForAuthCode := func() (string, error) {
reader := bufio.NewReader(os.Stdin)
authCode, err := reader.ReadString('\n')
authCode, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
return "", fmt.Errorf("failed to read string: %w", err)
}

authCode = strings.Trim(authCode, "\n")
Expand All @@ -46,9 +46,7 @@ func loginCmd() *cobra.Command {
return authCode, nil
}

var email string
var password string
var authCode string
var email, password, authCode string

cmd := &cobra.Command{
Use: "login",
Expand All @@ -72,6 +70,7 @@ func loginCmd() *cobra.Command {

var lastErr error

// nolint:wrapcheck
return retry.Do(func() error {
if errors.Is(lastErr, appstore.ErrAuthCodeRequired) && interactive {
dependencies.Logger.Log().Msg("enter 2FA code:")
Expand All @@ -97,6 +96,7 @@ func loginCmd() *cobra.Command {
if err != nil {
if errors.Is(err, appstore.ErrAuthCodeRequired) && !interactive {
dependencies.Logger.Log().Msg("2FA code is required; run the command again and supply a code using the `--auth-code` flag")

return nil
}

Expand All @@ -117,6 +117,7 @@ func loginCmd() *cobra.Command {
retry.Attempts(2),
retry.RetryIf(func(err error) bool {
lastErr = err

return errors.Is(err, appstore.ErrAuthCodeRequired)
}),
)
Expand All @@ -132,6 +133,7 @@ func loginCmd() *cobra.Command {
return cmd
}

// nolint:wrapcheck
func infoCmd() *cobra.Command {
return &cobra.Command{
Use: "info",
Expand All @@ -153,6 +155,7 @@ func infoCmd() *cobra.Command {
}
}

// nolint:wrapcheck
func revokeCmd() *cobra.Command {
return &cobra.Command{
Use: "revoke",
Expand All @@ -164,6 +167,7 @@ func revokeCmd() *cobra.Command {
}

dependencies.Logger.Log().Bool("success", true).Send()

return nil
},
}
Expand Down
21 changes: 13 additions & 8 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package cmd
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/99designs/keyring"
"github.com/juju/persistent-cookiejar"
cookiejar "github.com/juju/persistent-cookiejar"
"github.com/majd/ipatool/pkg/appstore"
"github.com/majd/ipatool/pkg/http"
"github.com/majd/ipatool/pkg/keychain"
Expand All @@ -16,10 +21,6 @@ import (
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"golang.org/x/term"
"io"
"os"
"path/filepath"
"strings"
)

var dependencies = Dependencies{}
Expand All @@ -38,23 +39,25 @@ type Dependencies struct {
// newLogger returns a new logger instance.
func newLogger(format OutputFormat, verbose bool) log.Logger {
var writer io.Writer

switch format {
case OutputFormatJSON:
writer = zerolog.SyncWriter(os.Stdout)
case OutputFormatText:
writer = log.NewWriter()
}

return log.NewLogger(log.Args{
Verbose: verbose,
Writer: writer,
})
},
)
}

// newCookieJar returns a new cookie jar instance.
func newCookieJar(machine machine.Machine) http.CookieJar {
path := filepath.Join(machine.HomeDirectory(), ConfigDirectoryName, CookieJarFileName)
return util.Must(cookiejar.New(&cookiejar.Options{
Filename: path,
Filename: filepath.Join(machine.HomeDirectory(), ConfigDirectoryName, CookieJarFileName),
}))
}

Expand Down Expand Up @@ -89,6 +92,7 @@ func newKeychain(machine machine.Machine, logger log.Logger, backendType keyring
return password, nil
},
}))

return keychain.New(keychain.Args{Keyring: ring})
}

Expand Down Expand Up @@ -134,6 +138,7 @@ func initWithCommand(cmd *cobra.Command) {
func createConfigDirectory() error {
configDirectoryPath := filepath.Join(dependencies.Machine.HomeDirectory(), ConfigDirectoryName)
_, err := dependencies.OS.Stat(configDirectoryPath)

if err != nil && dependencies.OS.IsNotExist(err) {
err = dependencies.OS.MkdirAll(configDirectoryPath, 0700)
if err != nil {
Expand Down
14 changes: 9 additions & 5 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ package cmd

import (
"errors"
"os"
"time"

"github.com/99designs/keyring"
"github.com/avast/retry-go"
"github.com/majd/ipatool/pkg/appstore"
"github.com/schollz/progressbar/v3"
"github.com/spf13/cobra"
"os"
"time"
)

// nolint:wrapcheck
func downloadCmd() *cobra.Command {
var acquireLicense bool
var outputPath string
var bundleID string
var (
acquireLicense bool
outputPath string
bundleID string
)

cmd := &cobra.Command{
Use: "download",
Expand Down
1 change: 1 addition & 0 deletions cmd/output_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"

"github.com/thediveo/enumflag/v2"
)

Expand Down
6 changes: 5 additions & 1 deletion cmd/purchase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package cmd

import (
"errors"
"time"

"github.com/99designs/keyring"
"github.com/avast/retry-go"
"github.com/majd/ipatool/pkg/appstore"
"github.com/spf13/cobra"
"time"
)

// nolint:wrapcheck
func purchaseCmd() *cobra.Command {
var bundleID string

Expand Down Expand Up @@ -47,6 +49,7 @@ func purchaseCmd() *cobra.Command {
}

dependencies.Logger.Log().Bool("success", true).Send()

return nil
},
retry.LastErrorOnly(true),
Expand All @@ -55,6 +58,7 @@ func purchaseCmd() *cobra.Command {
retry.Attempts(2),
retry.RetryIf(func(err error) bool {
lastErr = err

return errors.Is(err, appstore.ErrPasswordTokenExpired)
}),
)
Expand Down
22 changes: 13 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ package cmd

import (
"errors"
"reflect"

"github.com/majd/ipatool/pkg/appstore"
"github.com/spf13/cobra"
"github.com/thediveo/enumflag/v2"
"golang.org/x/net/context"
"reflect"
)

var version = "dev"

func rootCmd() *cobra.Command {
var verbose bool
var nonInteractive bool
var format OutputFormat
var (
verbose bool
nonInteractive bool
format OutputFormat
)

cmd := &cobra.Command{
Use: "ipatool",
Expand All @@ -23,7 +26,7 @@ func rootCmd() *cobra.Command {
SilenceUsage: true,
Version: version,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
ctx := context.WithValue(context.Background(), "interactive", nonInteractive == false)
ctx := context.WithValue(context.Background(), "interactive", !nonInteractive)
cmd.SetContext(ctx)
initWithCommand(cmd)
},
Expand All @@ -46,12 +49,11 @@ func rootCmd() *cobra.Command {
}

// Execute runs the program and returns the appropriate exit status code.
func Execute() (exitCode int) {
func Execute() int {
cmd := rootCmd()
err := cmd.Execute()
if err != nil {
exitCode = 1

if err != nil {
if reflect.ValueOf(dependencies).IsZero() {
initWithCommand(cmd)
}
Expand All @@ -70,7 +72,9 @@ func Execute() (exitCode int) {
Err(err).
Bool("success", false).
Send()

return 1
}

return
return 0
}
1 change: 1 addition & 0 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"
)

// nolint:wrapcheck
func searchCmd() *cobra.Command {
var limit int64

Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/majd/ipatool/cmd"
"os"

"github.com/majd/ipatool/cmd"
)

func main() {
Expand Down
1 change: 1 addition & 0 deletions pkg/appstore/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appstore
import (
"bytes"
"encoding/json"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rs/zerolog"
Expand Down
1 change: 1 addition & 0 deletions pkg/appstore/appstore_account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (t *appstore) AccountInfo() (AccountInfoOutput, error) {
}

var acc Account

err = json.Unmarshal(data, &acc)
if err != nil {
return AccountInfoOutput{}, fmt.Errorf("failed to unmarshal json: %w", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/appstore/appstore_account_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appstore
import (
"errors"
"fmt"

"github.com/golang/mock/gomock"
"github.com/majd/ipatool/pkg/keychain"
. "github.com/onsi/ginkgo/v2"
Expand Down
Loading

0 comments on commit 67e557e

Please sign in to comment.