Skip to content

Commit a014db0

Browse files
committed
Lint fixes for personalaccesstoken pacakge
1 parent 3dba026 commit a014db0

File tree

5 files changed

+20
-28
lines changed

5 files changed

+20
-28
lines changed

internal/command/personalaccesstoken/personalaccesstoken.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package personalaccesstoken handles operations related to personal access tokens,
2+
// including parsing arguments, requesting tokens, and formatting responses.
13
package personalaccesstoken
24

35
import (
@@ -18,10 +20,11 @@ import (
1820
)
1921

2022
const (
21-
usageText = "Usage: personal_access_token <name> <scope1[,scope2,...]> [ttl_days]"
23+
usageText = "usage: personal_access_token <name> <scope1[,scope2,...]> [ttl_days]"
2224
expiresDateFormat = "2006-01-02"
2325
)
2426

27+
// Command represents a command to manage personal access tokens.
2528
type Command struct {
2629
Config *config.Config
2730
Args *commandargs.Shell
@@ -35,6 +38,7 @@ type tokenArgs struct {
3538
ExpiresDate string // Calculated, a TTL is passed from command-line.
3639
}
3740

41+
// Execute processes the command, requests a personal access token, and prints the result.
3842
func (c *Command) Execute(ctx context.Context) (context.Context, error) {
3943
err := c.parseTokenArgs()
4044
if err != nil {
@@ -50,9 +54,9 @@ func (c *Command) Execute(ctx context.Context) (context.Context, error) {
5054
return ctx, err
5155
}
5256

53-
fmt.Fprint(c.ReadWriter.Out, "Token: "+response.Token+"\n")
54-
fmt.Fprint(c.ReadWriter.Out, "Scopes: "+strings.Join(response.Scopes, ",")+"\n")
55-
fmt.Fprint(c.ReadWriter.Out, "Expires: "+response.ExpiresAt+"\n")
57+
_, _ = fmt.Fprint(c.ReadWriter.Out, "Token: "+response.Token+"\n")
58+
_, _ = fmt.Fprint(c.ReadWriter.Out, "Scopes: "+strings.Join(response.Scopes, ",")+"\n")
59+
_, _ = fmt.Fprint(c.ReadWriter.Out, "Expires: "+response.ExpiresAt+"\n")
5660

5761
return ctx, nil
5862
}
@@ -86,7 +90,7 @@ func (c *Command) parseTokenArgs() error {
8690

8791
TTL, err := strconv.Atoi(rawTTL)
8892
if err != nil || TTL < 0 {
89-
return fmt.Errorf("Invalid value for days_ttl: '%s'", rawTTL)
93+
return fmt.Errorf("invalid value for days_ttl: '%s'", rawTTL)
9094
}
9195

9296
c.TokenArgs.ExpiresDate = time.Now().AddDate(0, 0, TTL+1).Format(expiresDateFormat)

internal/command/personalaccesstoken/personalaccesstoken_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"testing"
1111

12+
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314

1415
"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
@@ -28,7 +29,7 @@ func setup(t *testing.T) {
2829
b, err := io.ReadAll(r.Body)
2930
defer r.Body.Close()
3031

31-
require.NoError(t, err)
32+
assert.NoError(t, err)
3233

3334
var requestBody *personalaccesstoken.RequestBody
3435
json.Unmarshal(b, &requestBody)
@@ -111,7 +112,7 @@ func TestExecute(t *testing.T) {
111112
arguments: &commandargs.Shell{
112113
SshArgs: []string{cmdname, "newtoken", "api", "bad_ttl"},
113114
},
114-
expectedError: "Invalid value for days_ttl: 'bad_ttl'",
115+
expectedError: "invalid value for days_ttl: 'bad_ttl'",
115116
},
116117
{
117118
desc: "Without a ttl argument",
@@ -177,7 +178,7 @@ func TestExecute(t *testing.T) {
177178
},
178179
{
179180
desc: "With unknown configured scopes",
180-
PATConfig: config.PATConfig{AllowedScopes: []string{"read_reposotory"}},
181+
PATConfig: config.PATConfig{AllowedScopes: []string{"unknown_repository"}},
181182
arguments: &commandargs.Shell{
182183
GitlabKeyId: "default",
183184
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
@@ -191,20 +192,20 @@ func TestExecute(t *testing.T) {
191192
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_repository"}},
192193
arguments: &commandargs.Shell{
193194
GitlabKeyId: "default",
194-
SshArgs: []string{cmdname, "newtoken", "read_api,read_reposotory"},
195+
SshArgs: []string{cmdname, "newtoken", "read_api,unknown_repository"},
195196
},
196197
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
197198
"Scopes: read_api\n" +
198199
"Expires: 9001-11-17\n",
199200
},
200201
{
201202
desc: "With matching unknown requested scopes",
202-
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_reposotory"}},
203+
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "unknown_repository"}},
203204
arguments: &commandargs.Shell{
204205
GitlabKeyId: "invalidscope",
205-
SshArgs: []string{cmdname, "newtoken", "read_reposotory"},
206+
SshArgs: []string{cmdname, "newtoken", "unknown_repository"},
206207
},
207-
expectedError: "Invalid scope: 'read_reposotory'. Valid scopes are: [\"api\", \"create_runner\", \"k8s_proxy\", \"read_api\", \"read_registry\", \"read_repository\", \"read_user\", \"write_registry\", \"write_repository\"]",
208+
expectedError: "Invalid scope: 'unknown_repository'. Valid scopes are: [\"api\", \"create_runner\", \"k8s_proxy\", \"read_api\", \"read_registry\", \"read_repository\", \"read_user\", \"write_registry\", \"write_repository\"]",
208209
},
209210
}
210211

internal/gitlabnet/personalaccesstoken/client_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"testing"
99

10+
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112
"gitlab.com/gitlab-org/gitlab-shell/v14/client"
1213
"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
@@ -27,7 +28,7 @@ func initialize(t *testing.T) {
2728
b, err := io.ReadAll(r.Body)
2829
defer r.Body.Close()
2930

30-
require.NoError(t, err)
31+
assert.NoError(t, err)
3132

3233
var requestBody *RequestBody
3334
json.Unmarshal(b, &requestBody)

spec/gitlab_shell_personal_access_token_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def mock_server(server)
5353
remote:
5454
remote: ========================================================================
5555
remote:
56-
remote: Usage: personal_access_token <name> <scope1[,scope2,...]> [ttl_days]
56+
remote: usage: personal_access_token <name> <scope1[,scope2,...]> [ttl_days]
5757
remote:
5858
remote: ========================================================================
5959
remote:

support/lint_last_known_acceptable.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,6 @@ internal/command/lfstransfer/lfstransfer_test.go:1436:5: go-require: do not use
230230
internal/command/lfstransfer/lfstransfer_test.go:1451:5: go-require: do not use require in http handlers (testifylint)
231231
internal/command/lfstransfer/lfstransfer_test.go:1453:5: go-require: do not use require in http handlers (testifylint)
232232
internal/command/lfstransfer/lfstransfer_test.go:1454:5: go-require: do not use require in http handlers (testifylint)
233-
internal/command/personalaccesstoken/personalaccesstoken.go:1:1: package-comments: should have a package comment (revive)
234-
internal/command/personalaccesstoken/personalaccesstoken.go:25:6: exported: exported type Command should have comment or be unexported (revive)
235-
internal/command/personalaccesstoken/personalaccesstoken.go:38:1: exported: exported method Command.Execute should have comment or be unexported (revive)
236-
internal/command/personalaccesstoken/personalaccesstoken.go:53:12: Error return value of `fmt.Fprint` is not checked (errcheck)
237-
internal/command/personalaccesstoken/personalaccesstoken.go:54:12: Error return value of `fmt.Fprint` is not checked (errcheck)
238-
internal/command/personalaccesstoken/personalaccesstoken.go:55:12: Error return value of `fmt.Fprint` is not checked (errcheck)
239-
internal/command/personalaccesstoken/personalaccesstoken.go:62:10: ST1005: error strings should not be capitalized (stylecheck)
240-
internal/command/personalaccesstoken/personalaccesstoken.go:89:10: ST1005: error strings should not be capitalized (stylecheck)
241-
internal/command/personalaccesstoken/personalaccesstoken_test.go:31:5: go-require: do not use require in http handlers (testifylint)
242-
internal/command/personalaccesstoken/personalaccesstoken_test.go:180:62: `reposotory` is a misspelling of `repository` (misspell)
243-
internal/command/personalaccesstoken/personalaccesstoken_test.go:194:63: `reposotory` is a misspelling of `repository` (misspell)
244-
internal/command/personalaccesstoken/personalaccesstoken_test.go:202:74: `reposotory` is a misspelling of `repository` (misspell)
245-
internal/command/personalaccesstoken/personalaccesstoken_test.go:205:54: `reposotory` is a misspelling of `repository` (misspell)
246233
internal/command/readwriter/readwriter.go:1:1: package-comments: should have a package comment (revive)
247234
internal/command/readwriter/readwriter.go:7:6: exported: exported type ReadWriter should have comment or be unexported (revive)
248235
internal/command/receivepack/gitalycall_test.go:24:4: S1038: should use t.Logf(...) instead of t.Log(fmt.Sprintf(...)) (gosimple)
@@ -380,7 +367,6 @@ internal/gitlabnet/lfstransfer/client.go:125: internal/gitlabnet/lfstransfer/cli
380367
internal/gitlabnet/lfstransfer/client.go:214:1: exported: exported method Client.Lock should have comment or be unexported (revive)
381368
internal/gitlabnet/lfstransfer/client.go:271:1: exported: exported method Client.Unlock should have comment or be unexported (revive)
382369
internal/gitlabnet/lfstransfer/client.go:321:1: exported: exported method Client.ListLocksVerify should have comment or be unexported (revive)
383-
internal/gitlabnet/personalaccesstoken/client_test.go:30:5: go-require: do not use require in http handlers (testifylint)
384370
internal/gitlabnet/twofactorrecover/client_test.go:30:5: go-require: do not use require in http handlers (testifylint)
385371
internal/gitlabnet/twofactorverify/client_test.go:24:3: go-require: do not use require in http handlers (testifylint)
386372
internal/gitlabnet/twofactorverify/client_test.go:27:3: go-require: do not use require in http handlers (testifylint)

0 commit comments

Comments
 (0)