Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 89b4b36

Browse files
committed
Add stricter linter rules
1 parent cfdde80 commit 89b4b36

19 files changed

+135
-29
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
ci/bin
33
cmd/coder/coder
44
ci/integration/bin
5-
ci/integration/env.sh
5+
ci/integration/env.sh
6+
coder-sdk/env.sh

.golangci.yml

+30-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,33 @@ linters-settings:
1414
funcs: # Run `go tool vet help printf` to see available settings for `printf` analyzer.
1515
- (cdr.dev/coder-cli/pkg/clog).Tipf
1616
- (cdr.dev/coder-cli/pkg/clog).Hintf
17-
- (cdr.dev/coder-cli/pkg/clog).Causef
17+
- (cdr.dev/coder-cli/pkg/clog).Causef
18+
linters:
19+
disable-all: true
20+
enable:
21+
- megacheck
22+
- govet
23+
- golint
24+
- goconst
25+
- gocognit
26+
- nestif
27+
- misspell
28+
- unparam
29+
- unused
30+
- bodyclose
31+
- deadcode
32+
- depguard
33+
- dogsled
34+
- errcheck
35+
- unconvert
36+
- unparam
37+
- varcheck
38+
- whitespace
39+
- structcheck
40+
- stylecheck
41+
- typecheck
42+
- noctx
43+
- nolintlint
44+
- rowserrcheck
45+
- scopelint
46+
- goprintffuncname

ci/integration/devurls_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,4 @@ func TestDevURLCLI(t *testing.T) {
7676
// c.Run(ctx, `coder urls ls env1 -o json | jq -c '.[] | select( .name == "foobar")'`).Assert(t,
7777
// tcli.Error(),
7878
// jsonUnmarshals(&durl))
79-
8079
}

ci/integration/envs_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/google/go-cmp/cmp"
1818
)
1919

20-
func cleanupClient(t *testing.T, ctx context.Context) *coder.Client {
20+
func cleanupClient(ctx context.Context, t *testing.T) *coder.Client {
2121
creds := login(ctx, t)
2222

2323
u, err := url.Parse(creds.url)
@@ -41,7 +41,7 @@ func TestEnvsCLI(t *testing.T) {
4141

4242
run(t, "coder-cli-env-tests", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner) {
4343
headlessLogin(ctx, t, c)
44-
client := cleanupClient(t, ctx)
44+
client := cleanupClient(ctx, t)
4545

4646
// Minimum args not received.
4747
c.Run(ctx, "coder envs create").Assert(t,
@@ -101,7 +101,7 @@ func TestEnvsCLI(t *testing.T) {
101101

102102
run(t, "coder-cli-env-edit-tests", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner) {
103103
headlessLogin(ctx, t, c)
104-
client := cleanupClient(t, ctx)
104+
client := cleanupClient(ctx, t)
105105

106106
name := randString(10)
107107
c.Run(ctx, fmt.Sprintf("coder envs create %s --image ubuntu --follow", name)).Assert(t,

ci/integration/integration_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func TestCoderCLI(t *testing.T) {
8080
tcli.Error(),
8181
)
8282
})
83-
8483
}
8584

8685
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))

ci/steps/unit_test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ cd "$(git rev-parse --show-toplevel)"
66

77
echo "--- go test..."
88

9-
go test $(go list ./... | grep -v pkg/tcli | grep -v ci/integration)
9+
go test $(go list ./... | grep -v pkg/tcli | grep -v ci/integration | grep -v coder-sdk)

coder-sdk/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type ConfigOAuth struct {
5858

5959
func (c Client) SiteConfigAuth(ctx context.Context) (*ConfigAuth, error) {
6060
var conf ConfigAuth
61-
if err := c.requestBody(ctx, http.MethodGet, "/api/auth/config", nil, &c); err != nil {
61+
if err := c.requestBody(ctx, http.MethodGet, "/api/auth/config", nil, &conf); err != nil {
6262
return nil, err
6363
}
6464
return &conf, nil
@@ -117,5 +117,5 @@ func (c Client) SiteConfigExtensionMarketplace(ctx context.Context) (*ConfigExte
117117
}
118118

119119
func (c Client) PutSiteConfigExtensionMarketplace(ctx context.Context, req ConfigExtensionMarketplace) error {
120-
return c.requestBody(ctx, http.MethodGet, "/api/extensions/config", req, nil)
120+
return c.requestBody(ctx, http.MethodPut, "/api/extensions/config", req, nil)
121121
}

coder-sdk/config_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package coder_test
2+
3+
import (
4+
"context"
5+
"net/url"
6+
"os"
7+
"testing"
8+
"time"
9+
10+
"cdr.dev/coder-cli/coder-sdk"
11+
"cdr.dev/slog"
12+
"cdr.dev/slog/sloggers/slogtest"
13+
"cdr.dev/slog/sloggers/slogtest/assert"
14+
)
15+
16+
func newClient(t *testing.T) *coder.Client {
17+
token := os.Getenv("CODER_TOKEN")
18+
if token == "" {
19+
slogtest.Fatal(t, `"CODER_TOKEN" env var is empty`)
20+
}
21+
raw := os.Getenv("CODER_URL")
22+
u, err := url.Parse(raw)
23+
if err != nil {
24+
slogtest.Fatal(t, `"CODER_URL" env var is invalid`, slog.Error(err))
25+
}
26+
27+
return &coder.Client{
28+
BaseURL: u,
29+
Token: token,
30+
}
31+
}
32+
33+
func TestConfig(t *testing.T) {
34+
t.Parallel()
35+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
36+
defer cancel()
37+
38+
client := newClient(t)
39+
40+
version, err := client.APIVersion(ctx)
41+
assert.Success(t, "get api version", err)
42+
slogtest.Info(t, "got api version", slog.F("version", version))
43+
44+
authConfig, err := client.SiteConfigAuth(ctx)
45+
assert.Success(t, "auth config", err)
46+
slogtest.Info(t, "got site auth config", slog.F("config", authConfig))
47+
48+
oauthConf, err := client.SiteConfigOAuth(ctx)
49+
assert.Success(t, "auth config", err)
50+
slogtest.Info(t, "got site oauth config", slog.F("config", oauthConf))
51+
52+
putOAuth := &coder.ConfigOAuth{
53+
GitHub: coder.ConfigOAuthGitHub{
54+
BaseURL: "github.com",
55+
ClientID: "fake client id",
56+
ClientSecret: "fake secrets",
57+
},
58+
}
59+
60+
err = client.PutSiteConfigOAuth(ctx, *putOAuth)
61+
assert.Success(t, "put site oauth", err)
62+
63+
oauthConf, err = client.SiteConfigOAuth(ctx)
64+
assert.Success(t, "auth config", err)
65+
assert.Equal(t, "oauth was updated", putOAuth, oauthConf)
66+
}

coder-sdk/doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package coder provides simple APIs for integrating Go applications with Coder Enterprise.
2+
package coder

coder-sdk/secrets.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ func (c *Client) DeleteSecretByName(ctx context.Context, name, userID string) er
8787
// Delete the secret.
8888
// NOTE: This is racy, but acceptable. If the secret is gone or the permission changed since we looked up the id,
8989
// the call will simply fail and surface the error to the user.
90-
if _, err := c.request(ctx, http.MethodDelete, "/api/users/"+userID+"/secrets/"+secret.ID, nil); err != nil {
90+
resp, err := c.request(ctx, http.MethodDelete, "/api/users/"+userID+"/secrets/"+secret.ID, nil)
91+
if err != nil {
9192
return err
9293
}
94+
defer resp.Body.Close()
9395
return nil
9496
}

coder-sdk/version.go

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func (c Client) APIVersion(ctx context.Context) (string, error) {
1212
if err != nil {
1313
return "", err
1414
}
15+
defer resp.Body.Close()
1516

1617
version := resp.Header.Get(coderVersionHeaderKey)
1718
if version == "" {

internal/cmd/auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newClient(ctx context.Context) (*coder.Client, error) {
4444

4545
u, err := url.Parse(rawURL)
4646
if err != nil {
47-
return nil, xerrors.Errorf("url malformed: %w try runing \"coder login\" with a valid URL", err)
47+
return nil, xerrors.Errorf("url malformed: %w try running \"coder login\" with a valid URL", err)
4848
}
4949

5050
c := &coder.Client{

internal/cmd/configssh.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func writeSSHKey(ctx context.Context, client *coder.Client, privateKeyPath strin
153153
func makeNewConfigs(userName string, envs []coder.Environment, startToken, startMsg, endToken, privateKeyFilepath string) (string, error) {
154154
hostname, err := configuredHostname()
155155
if err != nil {
156-
return "", nil
156+
return "", err
157157
}
158158

159159
newConfig := fmt.Sprintf("\n%s\n%s\n\n", startToken, startMsg)

internal/cmd/envs.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func envsCmd() *cobra.Command {
3838
return cmd
3939
}
4040

41+
const (
42+
humanOutput = "human"
43+
jsonOutput = "json"
44+
)
45+
4146
func lsEnvsCommand(user *string) *cobra.Command {
4247
var outputFmt string
4348

@@ -61,14 +66,14 @@ func lsEnvsCommand(user *string) *cobra.Command {
6166
}
6267

6368
switch outputFmt {
64-
case "human":
69+
case humanOutput:
6570
err := tablewriter.WriteTable(len(envs), func(i int) interface{} {
6671
return envs[i]
6772
})
6873
if err != nil {
6974
return xerrors.Errorf("write table: %w", err)
7075
}
71-
case "json":
76+
case jsonOutput:
7277
err := json.NewEncoder(os.Stdout).Encode(envs)
7378
if err != nil {
7479
return xerrors.Errorf("write environments as JSON: %w", err)
@@ -80,7 +85,7 @@ func lsEnvsCommand(user *string) *cobra.Command {
8085
},
8186
}
8287

83-
cmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json")
88+
cmd.Flags().StringVarP(&outputFmt, "output", "o", humanOutput, "human | json")
8489

8590
return cmd
8691
}

internal/cmd/shell.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
166166
if err != nil {
167167
var closeErr websocket.CloseError
168168
if xerrors.As(err, &closeErr) {
169-
return networkErr(client, env)
169+
return networkErr(env)
170170
}
171171
return xerrors.Errorf("start remote command: %w", err)
172172
}
@@ -200,14 +200,14 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
200200
if err := process.Wait(); err != nil {
201201
var closeErr websocket.CloseError
202202
if xerrors.Is(err, ctx.Err()) || xerrors.As(err, &closeErr) {
203-
return networkErr(client, env)
203+
return networkErr(env)
204204
}
205205
return err
206206
}
207207
return nil
208208
}
209209

210-
func networkErr(client *coder.Client, env *coder.Environment) error {
210+
func networkErr(env *coder.Environment) error {
211211
if env.LatestStat.ContainerStatus != coder.EnvironmentOn {
212212
return clog.Fatal(
213213
"environment is not running",

internal/cmd/urls.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func urlCmd() *cobra.Command {
3131
ValidArgsFunction: getEnvsForCompletion(coder.Me),
3232
RunE: listDevURLsCmd(&outputFmt),
3333
}
34-
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human|json")
34+
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", humanOutput, "human|json")
3535

3636
rmCmd := &cobra.Command{
3737
Use: "rm [environment_name] [port]",
@@ -99,7 +99,7 @@ func listDevURLsCmd(outputFmt *string) func(cmd *cobra.Command, args []string) e
9999
}
100100

101101
switch *outputFmt {
102-
case "human":
102+
case humanOutput:
103103
if len(devURLs) < 1 {
104104
clog.LogInfo(fmt.Sprintf("no devURLs found for environment %q", envName))
105105
return nil
@@ -110,7 +110,7 @@ func listDevURLsCmd(outputFmt *string) func(cmd *cobra.Command, args []string) e
110110
if err != nil {
111111
return xerrors.Errorf("write table: %w", err)
112112
}
113-
case "json":
113+
case jsonOutput:
114114
if err := json.NewEncoder(os.Stdout).Encode(devURLs); err != nil {
115115
return xerrors.Errorf("encode DevURLs as json: %w", err)
116116
}
@@ -263,7 +263,12 @@ func urlList(ctx context.Context, envName string) ([]DevURL, error) {
263263
reqString := "%s/api/environments/%s/devurls?session_token=%s"
264264
reqURL := fmt.Sprintf(reqString, client.BaseURL, env.ID, client.Token)
265265

266-
resp, err := http.Get(reqURL)
266+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
267+
if err != nil {
268+
return nil, err
269+
}
270+
271+
resp, err := http.DefaultClient.Do(req)
267272
if err != nil {
268273
return nil, err
269274
}

internal/cmd/users.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func usersCmd() *cobra.Command {
2323
coder users ls -o json | jq .[] | jq -r .email`,
2424
RunE: listUsers(&outputFmt),
2525
}
26-
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json")
26+
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", humanOutput, "human | json")
2727

2828
cmd.AddCommand(lsCmd)
2929
return cmd
@@ -43,7 +43,7 @@ func listUsers(outputFmt *string) func(cmd *cobra.Command, args []string) error
4343
}
4444

4545
switch *outputFmt {
46-
case "human":
46+
case humanOutput:
4747
// For each element, return the user.
4848
each := func(i int) interface{} { return users[i] }
4949
if err := tablewriter.WriteTable(len(users), each); err != nil {

internal/sync/eventcache.go

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func (cache eventCache) SequentialEvents() []timedEvent {
4040
// Include files that have deleted here.
4141
// It's unclear whether they're files or folders.
4242
r = append(r, ev)
43-
4443
}
4544
return r
4645
}
@@ -58,7 +57,6 @@ func (cache eventCache) ConcurrentEvents() []timedEvent {
5857
continue
5958
}
6059
r = append(r, ev)
61-
6260
}
6361
return r
6462
}

internal/x/xterminal/terminal.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func ResizeEvents(ctx context.Context, termFD uintptr) chan ResizeEvent {
6262
signal.Notify(sigChan, unix.SIGWINCH)
6363
defer signal.Stop(sigChan)
6464

65-
// Emit an inital signal event to make sure the server receives our current window size.
65+
// Emit an initial signal event to make sure the server receives our current window size.
6666
select {
6767
case <-ctx.Done():
6868
return
@@ -87,7 +87,6 @@ func ResizeEvents(ctx context.Context, termFD uintptr) chan ResizeEvent {
8787
return
8888
case events <- event:
8989
}
90-
9190
}
9291
}
9392
}()

0 commit comments

Comments
 (0)