Skip to content

Commit da95b2f

Browse files
authored
chore: fix linter errors (#20)
1 parent 17e0301 commit da95b2f

23 files changed

Lines changed: 635 additions & 685 deletions

.github/workflows/linter.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: linter
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
linter:
12+
runs-on: ubuntu-latest
13+
steps:
14+
-
15+
name: Checkout
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
- name: Install Go
20+
uses: actions/setup-go@v5
21+
with:
22+
go-version: '>=1.24'
23+
- name: Install devbox
24+
uses: jetify-com/devbox-install-action@v0.13.0
25+
with:
26+
enable-cache: true
27+
devbox-version: 0.14.0
28+
29+
- name: Install prerequisites
30+
shell: /usr/bin/bash {0}
31+
run: |
32+
devbox install
33+
devbox run linter

.golang-ci.yml

Lines changed: 0 additions & 154 deletions
This file was deleted.

.golangci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
version: "2"
3+
# Configure which files to skip during linting
4+
run:
5+
tests: false
6+
7+
linters:
8+
default: all
9+
10+
disable:
11+
- wsl
12+
- wsl_v5
13+
- nlreturn
14+
- depguard
15+
- gochecknoinits
16+
- gochecknoglobals
17+
- forbidigo
18+
- varnamelen
19+
- exhaustruct
20+
- tagliatelle
21+
- noinlineerr
22+
- dupl # Duplication is expected for similar command structures
23+
- ireturn # Returning interfaces is intentional for our design
24+
- err113 # We use formatted errors throughout
25+
- lll # Line length limit is too restrictive for function signatures
26+
- nolintlint # We use nolint directives where needed

Taskfile.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ tasks:
99
cmds:
1010
- task -a
1111

12-
lint:
12+
linter:
1313
desc: "Run linter"
1414
cmds:
15-
- golangci-lint run -c .golang-ci.yml
15+
- golangci-lint run
1616

1717
binary:
1818
desc: "Build binary"

cmd/decode-es.go

Lines changed: 53 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package cmd contains the command-line interface commands.
12
package cmd
23

34
import (
@@ -8,99 +9,58 @@ import (
89
"github.com/spf13/cobra"
910
)
1011

11-
var decodeES256Cmd = &cobra.Command{
12-
Use: "es256",
13-
Short: "decode JWT token",
14-
Long: `decode JWT token`,
15-
Run: func(cmd *cobra.Command, args []string) {
16-
var (
17-
j cryptojwt.Decoder
18-
err error
19-
)
20-
if privateKeyFile == "" && publicKeyFile == "" {
21-
fmt.Println("private key file or public key file is mandatory")
22-
fmt.Println(cmd.UsageString())
23-
os.Exit(1)
24-
}
25-
if token == "" {
26-
fmt.Println("token is mandatory")
27-
fmt.Println(cmd.UsageString())
28-
os.Exit(1)
29-
}
30-
if publicKeyFile != "" {
31-
j = cryptojwt.NewES256DecoderWithPublicKeyFile(publicKeyFile)
32-
} else {
33-
j = cryptojwt.NewES256DecoderWithPrivateKeyFile(privateKeyFile)
34-
}
35-
if err != nil {
36-
fmt.Println(err)
37-
os.Exit(1)
38-
}
39-
t, err := j.Decode(token)
40-
if err != nil {
41-
fmt.Println(err)
42-
os.Exit(1)
43-
}
44-
fmt.Println(t)
45-
},
12+
//nolint:dupl // Similar structure needed for different algorithms
13+
func createESDecodeCommand(_ /* alg */, use string, pubKeyDecoder, privKeyDecoder func(string) cryptojwt.Decoder) *cobra.Command {
14+
return &cobra.Command{
15+
Use: use,
16+
Short: "decode JWT token",
17+
Long: `decode JWT token`,
18+
Run: func(cmd *cobra.Command, _ []string) {
19+
if privateKeyFile == "" && publicKeyFile == "" {
20+
fmt.Println("private key file or public key file is mandatory")
21+
fmt.Println(cmd.UsageString())
22+
os.Exit(1)
23+
}
24+
if token == "" {
25+
fmt.Println("token is mandatory")
26+
fmt.Println(cmd.UsageString())
27+
os.Exit(1)
28+
}
29+
30+
var j cryptojwt.Decoder
31+
if publicKeyFile != "" {
32+
j = pubKeyDecoder(publicKeyFile)
33+
} else {
34+
j = privKeyDecoder(privateKeyFile)
35+
}
36+
37+
t, err := j.Decode(token)
38+
if err != nil {
39+
fmt.Println(err)
40+
os.Exit(1)
41+
}
42+
fmt.Println(t)
43+
},
44+
}
4645
}
4746

48-
var decodeES384Cmd = &cobra.Command{
49-
Use: "es384",
50-
Short: "decode es384 JWT token",
51-
Long: `decode es384 JWT token`,
52-
Run: func(cmd *cobra.Command, args []string) {
53-
var j cryptojwt.Decoder
54-
if privateKeyFile == "" && publicKeyFile == "" {
55-
fmt.Println("private key file or public key file is mandatory")
56-
fmt.Println(cmd.UsageString())
57-
os.Exit(1)
58-
}
59-
if token == "" {
60-
fmt.Println("token is mandatory")
61-
fmt.Println(cmd.UsageString())
62-
os.Exit(1)
63-
}
64-
if publicKeyFile != "" {
65-
j = cryptojwt.NewES384DecoderWithPublicKeyFile(publicKeyFile)
66-
} else {
67-
j = cryptojwt.NewES384DecoderWithPrivateKeyFile(privateKeyFile)
68-
}
69-
t, err := j.Decode(token)
70-
if err != nil {
71-
fmt.Println(err)
72-
os.Exit(1)
73-
}
74-
fmt.Println(t)
75-
},
76-
}
47+
var decodeES256Cmd = createESDecodeCommand(
48+
"ES256",
49+
"es256",
50+
cryptojwt.NewES256DecoderWithPublicKeyFile,
51+
cryptojwt.NewES256DecoderWithPrivateKeyFile,
52+
)
7753

78-
var decodeES512Cmd = &cobra.Command{
79-
Use: "es512",
80-
Short: "decode es512 JWT token",
81-
Long: `decode es512 JWT token`,
82-
Run: func(cmd *cobra.Command, args []string) {
83-
var j cryptojwt.Decoder
84-
if privateKeyFile == "" && publicKeyFile == "" {
85-
fmt.Println("private key file or public key file is mandatory")
86-
fmt.Println(cmd.UsageString())
87-
os.Exit(1)
88-
}
89-
if token == "" {
90-
fmt.Println("token is mandatory")
91-
fmt.Println(cmd.UsageString())
92-
os.Exit(1)
93-
}
94-
if publicKeyFile != "" {
95-
j = cryptojwt.NewES512DecoderWithPublicKeyFile(publicKeyFile)
96-
} else {
97-
j = cryptojwt.NewES512DecoderWithPrivateKeyFile(privateKeyFile)
98-
}
99-
t, err := j.Decode(token)
100-
if err != nil {
101-
fmt.Println(err)
102-
os.Exit(1)
103-
}
104-
fmt.Println(t)
105-
},
106-
}
54+
var decodeES384Cmd = createESDecodeCommand(
55+
"ES384",
56+
"es384",
57+
cryptojwt.NewES384DecoderWithPublicKeyFile,
58+
cryptojwt.NewES384DecoderWithPrivateKeyFile,
59+
)
60+
61+
var decodeES512Cmd = createESDecodeCommand(
62+
"ES512",
63+
"es512",
64+
cryptojwt.NewES512DecoderWithPublicKeyFile,
65+
cryptojwt.NewES512DecoderWithPrivateKeyFile,
66+
)

0 commit comments

Comments
 (0)