Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
theFong committed Feb 9, 2024
1 parent e4e1f9a commit 201b9f0
Show file tree
Hide file tree
Showing 33 changed files with 189 additions and 159 deletions.
6 changes: 3 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ linters:
- unused
- varcheck
- bodyclose
- depguard
# - depguard
- dupl
- exportloopref
- forcetypeassert
- funlen
# - gci
- gocognit
- goconst
# - goconst
- gocritic
- gocyclo
# - godot
- gofumpt
- revive
# - revive
# - gomnd
- goprintffuncname
- gosec
Expand Down
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@
],
"go.lintOnSave": "workspace",
"go.testTimeout": "240s",
"go.goroot": "/home/ubuntu/.asdf/installs/golang//go"
"go.goroot": "/home/ubuntu/.asdf/installs/golang//go",
"[go][go.mod]": {
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func main() {
if err := command.Execute(); err != nil {
cmderrors.DisplayAndHandleError(err)
done()
os.Exit(1)
os.Exit(1) //nolint:gocritic // manually call done
}
}
3 changes: 2 additions & 1 deletion pkg/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analytics

import (
"bytes"
"context"
"encoding/json"
"net/http"

Expand All @@ -25,7 +26,7 @@ func TrackEvent(data EventData) error {
return breverrors.WrapAndTrace(err)
}

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
req, err := http.NewRequestWithContext(context.TODO(), "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return breverrors.WrapAndTrace(err)
}
Expand Down
24 changes: 20 additions & 4 deletions pkg/auth/auth0.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ func (a *Authenticator) Start(ctx context.Context) (State, error) {
return s, nil
}

// postFormWithContext wraps the process of creating a POST request with form data and a context
func postFormWithContext(ctx context.Context, url string, data url.Values) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, "POST", url, strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}

req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
return resp, nil
}

// Wait waits until the user is logged in on the browser.
func (a *Authenticator) Wait(ctx context.Context, state State) (Result, error) {
t := time.NewTicker(state.IntervalDuration())
Expand All @@ -135,7 +151,7 @@ func (a *Authenticator) Wait(ctx context.Context, state State) (Result, error) {
"grant_type": {"urn:ietf:params:oauth:grant-type:device_code"},
"device_code": {state.DeviceCode},
}
r, err := http.PostForm(a.OauthTokenEndpoint, data)
r, err := postFormWithContext(ctx, a.OauthTokenEndpoint, data)
if err != nil {
return Result{}, breverrors.WrapAndTrace(err, breverrors.NetworkErrorMessage)
}
Expand Down Expand Up @@ -187,13 +203,13 @@ func (a *Authenticator) Wait(ctx context.Context, state State) (Result, error) {
}
}

func (a *Authenticator) getDeviceCode(_ context.Context) (State, error) {
func (a *Authenticator) getDeviceCode(ctx context.Context) (State, error) {
data := url.Values{
"client_id": {a.ClientID},
"scope": {strings.Join(requiredScopes, " ")},
"audience": {a.Audience},
}
r, err := http.PostForm(a.DeviceCodeEndpoint, data)
r, err := postFormWithContext(ctx, a.DeviceCodeEndpoint, data)
if err != nil {
return State{}, breverrors.WrapAndTrace(err, breverrors.NetworkErrorMessage)
}
Expand Down Expand Up @@ -256,7 +272,7 @@ func (a Authenticator) GetNewAuthTokensWithRefresh(refreshToken string) (*entity
"refresh_token": {refreshToken},
}

r, err := http.PostForm(a.OauthTokenEndpoint, payload)
r, err := postFormWithContext(context.TODO(), a.OauthTokenEndpoint, payload)
if err != nil {
return nil, breverrors.WrapAndTrace(err, breverrors.NetworkErrorMessage)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/background/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ func NewCmdBackground(t *terminal.Terminal, s BackgroundStore) *cobra.Command {
if err != nil {
log.Fatal(err)
}
defer logFile.Close() //nolint: gosec,errcheck // TODO
defer logFile.Close() //nolint: errcheck // TODO
logFile.WriteString(time.Now().Format("2006-01-02 15:04:05") + ": Command \"" + command + "\" was run in the background.\n") //nolint: errcheck,gosec // TODO

// Write process details to data file
processesFile, err := os.OpenFile(logsDir+"/processes.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o666) //nolint: gosec // TODO
if err != nil {
log.Fatal(err)
}
defer processesFile.Close() //nolint: errcheck,gosec // TODO
defer processesFile.Close() //nolint: errcheck // TODO
_, _ = processesFile.WriteString(fmt.Sprintf("%d,%s,%s\n", c.Process.Pid, time.Now().Format("2006-01-02 15:04:05"), command))

if stopFlag {
Expand Down Expand Up @@ -178,7 +178,7 @@ func checkProgress() {
if err != nil {
panic(err)
}
defer file.Close() //nolint: errcheck,gosec // TODO
defer file.Close() //nolint: errcheck // TODO

scanner := bufio.NewScanner(file)

Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/clipboard/clipboard_listener.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clipboard

import (
"context"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -33,7 +34,7 @@ func CreateListener(config *Config) *Server {
// tcp req
func SendRequest(address string, message string) error {
reader := strings.NewReader(message)
request, err := http.NewRequest("GET", "http://"+address+"/", reader)
request, err := http.NewRequestWithContext(context.TODO(), "GET", "http://"+address+"/", reader)
if err != nil {
fmt.Println(err)
return breverrors.WrapAndTrace(err)
Expand Down
9 changes: 6 additions & 3 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewDefaultBrevCommand() *cobra.Command {
return cmd
}

func NewBrevCommand() *cobra.Command { //nolint:funlen // define brev command
func NewBrevCommand() *cobra.Command { //nolint:funlen,gocognit,gocyclo // define brev command
// in io.Reader, out io.Writer, err io.Writer
t := terminal.New()
var printVersion bool
Expand Down Expand Up @@ -140,7 +140,10 @@ func NewBrevCommand() *cobra.Command { //nolint:funlen // define brev command
if err != nil {
return
}
hello.CanWeOnboard(t, user, loginCmdStore)
err = hello.CanWeOnboard(t, user, loginCmdStore)
if err != nil {
return
}
}
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -227,7 +230,7 @@ func NewBrevCommand() *cobra.Command { //nolint:funlen // define brev command
return cmds
}

func createCmdTree(cmd *cobra.Command, t *terminal.Terminal, loginCmdStore *store.AuthHTTPStore, noLoginCmdStore *store.AuthHTTPStore, loginAuth *auth.LoginAuth) {
func createCmdTree(cmd *cobra.Command, t *terminal.Terminal, loginCmdStore *store.AuthHTTPStore, noLoginCmdStore *store.AuthHTTPStore, loginAuth *auth.LoginAuth) { //nolint:funlen // define brev command
cmd.AddCommand(set.NewCmdSet(t, loginCmdStore, noLoginCmdStore))
cmd.AddCommand(ls.NewCmdLs(t, loginCmdStore, noLoginCmdStore))
cmd.AddCommand(org.NewCmdOrg(t, loginCmdStore, noLoginCmdStore))
Expand Down
16 changes: 8 additions & 8 deletions pkg/cmd/configureenvvars/configureenvvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"strings"

"github.com/alessio/shellescape"
"github.com/brevdev/brev-cli/pkg/collections" //nolint:typecheck
"github.com/brevdev/brev-cli/pkg/collections"
"github.com/brevdev/brev-cli/pkg/terminal"
"github.com/spf13/cobra"
)

const (
BREV_WORKSPACE_ENV_PATH = "/home/brev/workspace/.env"
BREV_DEV_PLANE_ENV_PATH = "/home/ubuntu/.brev/.env"
BREV_MANGED_ENV_VARS_KEY = "BREV_MANAGED_ENV_VARS"
BrevWorkspaceEnvPath = "/home/brev/workspace/.env"
BrevDevPlaneEnvPath = "/home/ubuntu/.brev/.env"
BrevManagedEnvVarsKey = "BREV_MANAGED_ENV_VARS"
)

type envVars map[string]string
Expand Down Expand Up @@ -48,10 +48,10 @@ func NewCmdConfigureEnvVars(_ *terminal.Terminal, cevStore ConfigureEnvVarsStore
}

func RunConfigureEnvVars(cevStore ConfigureEnvVarsStore) (string, error) {
brevEnvsString := os.Getenv(BREV_MANGED_ENV_VARS_KEY)
brevEnvsString := os.Getenv(BrevManagedEnvVarsKey)
// intentionally ignoring err
envFileContents, _ := cevStore.GetFileAsString(BREV_WORKSPACE_ENV_PATH)
devplaneContents, _ := cevStore.GetFileAsString(BREV_DEV_PLANE_ENV_PATH)
envFileContents, _ := cevStore.GetFileAsString(BrevWorkspaceEnvPath)
devplaneContents, _ := cevStore.GetFileAsString(BrevDevPlaneEnvPath)
envFileContents = envFileContents + "\n" + devplaneContents
return generateExportString(brevEnvsString, envFileContents), nil
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func makeEnvCmdOutputLines(brevEnvKeys, envFileKeys []string, envfileEntries env
newBrevEnvKeys := strings.Join(envFileKeys, ",")
newBrevEnvKeysEntry := ""
if newBrevEnvKeys != "" {
newBrevEnvKeysEntry = BREV_MANGED_ENV_VARS_KEY + "=" + newBrevEnvKeys
newBrevEnvKeysEntry = BrevManagedEnvVarsKey + "=" + newBrevEnvKeys
}
if newBrevEnvKeysEntry != "" {
envCmdOutput = append(envCmdOutput, "export "+newBrevEnvKeysEntry)
Expand Down
20 changes: 10 additions & 10 deletions pkg/cmd/configureenvvars/configureenvvars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/google/go-cmp/cmp"
)

func Test_generateExportString(t *testing.T) {
func Test_generateExportString(t *testing.T) { //nolint:funlen // this is a test
type args struct {
brevEnvsString string
envFileContents string
Expand Down Expand Up @@ -44,7 +44,7 @@ unset baz`,
envFileContents: "foo=bar",
},
want: `export foo=bar
export ` + BREV_MANGED_ENV_VARS_KEY + `=foo`,
export ` + BrevManagedEnvVarsKey + `=foo`,
},
{
name: "sets env var with export prefix",
Expand All @@ -53,7 +53,7 @@ export ` + BREV_MANGED_ENV_VARS_KEY + `=foo`,
envFileContents: "export foo=bar",
},
want: `export foo=bar
export ` + BREV_MANGED_ENV_VARS_KEY + `=foo`,
export ` + BrevManagedEnvVarsKey + `=foo`,
},
{
name: "is idempotent",
Expand All @@ -62,7 +62,7 @@ export ` + BREV_MANGED_ENV_VARS_KEY + `=foo`,
envFileContents: "foo=bar",
},
want: `export foo=bar
export ` + BREV_MANGED_ENV_VARS_KEY + "=foo",
export ` + BrevManagedEnvVarsKey + "=foo",
},
{
name: "multiple operations(journal) case",
Expand All @@ -74,7 +74,7 @@ export ` + BREV_MANGED_ENV_VARS_KEY + "=foo",
unset key2
unset key3
export key4=val
export ` + BREV_MANGED_ENV_VARS_KEY + "=key4",
export ` + BrevManagedEnvVarsKey + "=key4",
},
{
name: "using env format found on workspace",
Expand All @@ -84,7 +84,7 @@ export ` + BREV_MANGED_ENV_VARS_KEY + "=key4",
},
want: `export alice='bob'
export foo='bar'
export ` + BREV_MANGED_ENV_VARS_KEY + "=alice,foo",
export ` + BrevManagedEnvVarsKey + "=alice,foo",
},
{
name: "multi line file",
Expand All @@ -95,7 +95,7 @@ export alice='bob'`,
},
want: `export alice='bob'
export foo='bar'
export ` + BREV_MANGED_ENV_VARS_KEY + "=alice,foo",
export ` + BrevManagedEnvVarsKey + "=alice,foo",
},
{
name: "semicolon -> newline file ",
Expand All @@ -107,7 +107,7 @@ export alice='bob'`,
},
want: `export alice='bob'
export foo='bar'
export ` + BREV_MANGED_ENV_VARS_KEY + "=alice,foo",
export ` + BrevManagedEnvVarsKey + "=alice,foo",
},
{
name: "hyphen in env var shouldn't be included since that's not allowed in most shells",
Expand Down Expand Up @@ -140,7 +140,7 @@ export ` + BREV_MANGED_ENV_VARS_KEY + "=alice,foo",
envFileContents: `export foo='90ie&$>'`,
},
want: `export foo='90ie&$>'
export ` + BREV_MANGED_ENV_VARS_KEY + "=foo",
export ` + BrevManagedEnvVarsKey + "=foo",
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -240,7 +240,7 @@ func Test_addUnsetEntriesToOutput(t *testing.T) {
}
}

func Test_parse(t *testing.T) {
func Test_parse(t *testing.T) { //nolint:funlen // this is a test
type args struct {
content string
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/cmd/configureenvvars/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const eof = -1

// next returns the next rune in the input.
func (l *lexer) next() rune {
if int(l.pos) >= len(l.input) {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
Expand Down Expand Up @@ -145,8 +145,6 @@ func lexText(l *lexer) stateFn {
return nil
}

const hyphen = "-"

func lexKey(l *lexer) stateFn {
s := l.input[l.start:l.pos]
// determine if s alphanumeric or an underscore
Expand Down Expand Up @@ -193,7 +191,7 @@ func lexNewline(l *lexer) stateFn {

func lexSpace(l *lexer) stateFn {
if strings.HasPrefix(l.input[l.start:l.pos], "export") {
l.start = l.start + len("export")
l.start += len("export")
}
l.next()
if l.input[l.start:l.pos] != space {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/configureenvvars/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/google/go-cmp/cmp"
)

func Test_lex(t *testing.T) {
func Test_lex(t *testing.T) { //nolint:funlen // this is a test
type args struct {
input string
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/envsetup/envsetup.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package envsetup

import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/user"
"path/filepath"
Expand All @@ -22,6 +22,7 @@ import (
"github.com/brevdev/brev-cli/pkg/autostartconf"
"github.com/brevdev/brev-cli/pkg/cmd/updatemodel"
"github.com/brevdev/brev-cli/pkg/cmd/version"
"github.com/brevdev/brev-cli/pkg/collections"
"github.com/brevdev/brev-cli/pkg/entity"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/featureflag"
Expand Down Expand Up @@ -370,7 +371,7 @@ func (e envInitier) SetupDatadog() error {
installScriptURL := "https://s3.amazonaws.com/dd-agent/scripts/install_script.sh"
var installScript string

resp, err := http.Get(installScriptURL)
resp, err := collections.GetRequestWithContext(context.TODO(), installScriptURL)
if err != nil {
return breverrors.WrapAndTrace(err)
}
Expand Down
Loading

0 comments on commit 201b9f0

Please sign in to comment.