Skip to content

Commit cc6ccad

Browse files
committed
MAJOR: allow reading allowed list from a remote json file
1 parent 599c5be commit cc6ccad

File tree

7 files changed

+102
-5
lines changed

7 files changed

+102
-5
lines changed

.aspell.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ allowed:
3030
- devel
3131
- ioutil
3232
- defaultconf
33+
- json
34+
- req
35+
- nallowed

aspell/aspell.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ import (
1414
"github.com/fatih/camelcase"
1515
)
1616

17+
type RemoteFile struct {
18+
URL string `yaml:"url"`
19+
HeaderFromENV string `yaml:"header_from_env"`
20+
AllowedItemsKey string `yaml:"allowed_items_key"`
21+
}
22+
1723
type Aspell struct {
18-
Mode mode `yaml:"mode"`
19-
MinLength int `yaml:"min_length"`
20-
IgnoreFiles []string `yaml:"ignore_files"`
21-
AllowedWords []string `yaml:"allowed"`
22-
HelpText string `yaml:"-"`
24+
Mode mode `yaml:"mode"`
25+
RemoteFile RemoteFile `yaml:"remote_file"`
26+
MinLength int `yaml:"min_length"`
27+
IgnoreFiles []string `yaml:"ignore_files"`
28+
AllowedWords []string `yaml:"allowed"`
29+
HelpText string `yaml:"-"`
2330
}
2431

2532
var (

aspell/new.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,20 @@ func New(filename string) (Aspell, error) {
2424
return Aspell{}, err
2525
}
2626

27+
var extraAllowedWords []string
28+
if aspell.RemoteFile.URL != "" {
29+
extraAllowedWords, err = fetchRemoteFile(aspell)
30+
if err != nil {
31+
return Aspell{}, err
32+
}
33+
}
34+
2735
for i, word := range aspell.AllowedWords {
2836
aspell.AllowedWords[i] = strings.ToLower(word)
2937
}
38+
for _, word := range extraAllowedWords {
39+
aspell.AllowedWords = append(aspell.AllowedWords, strings.ToLower(word))
40+
}
3041

3142
if aspell.MinLength < 1 {
3243
aspell.MinLength = 3
@@ -59,6 +70,9 @@ allowed:
5970
- config
6071
`
6172
}
73+
if aspell.RemoteFile.URL != "" {
74+
aspell.HelpText += fmt.Sprintf("\n\nallowed words can be added to remote file:\n url: %s\n", aspell.RemoteFile.URL)
75+
}
6276

6377
ignoreFiles := []string{"go.mod", "go.sum"}
6478
for _, file := range ignoreFiles {

aspell/remote.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package aspell
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"os"
7+
"strings"
8+
9+
"gopkg.in/yaml.v3"
10+
)
11+
12+
func fetchRemoteFile(aspell Aspell) ([]string, error) {
13+
url := aspell.RemoteFile.URL
14+
if url == "" {
15+
return []string{}, nil
16+
}
17+
18+
req, err := http.NewRequest("GET", url, nil)
19+
if err != nil {
20+
return nil, err
21+
}
22+
23+
if aspell.RemoteFile.HeaderFromENV != "" {
24+
envValue := os.Getenv(aspell.RemoteFile.HeaderFromENV)
25+
req.Header.Set(aspell.RemoteFile.HeaderFromENV, envValue)
26+
}
27+
28+
client := &http.Client{}
29+
resp, err := client.Do(req)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
defer resp.Body.Close()
35+
36+
var data map[string]interface{}
37+
err = json.NewDecoder(resp.Body).Decode(&data)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
var allowedWords []string
43+
44+
items, ok := data[aspell.RemoteFile.AllowedItemsKey].([]interface{})
45+
if !ok {
46+
content, ok := data[aspell.RemoteFile.AllowedItemsKey].(string)
47+
if !ok {
48+
return nil, nil
49+
}
50+
if strings.HasPrefix(content, "```yaml\n") && strings.HasSuffix(content, "\n```") {
51+
content = strings.TrimPrefix(content, "```yaml\n")
52+
content = strings.TrimSuffix(content, "\n```")
53+
err = yaml.Unmarshal([]byte(content), &allowedWords)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
return allowedWords, nil
59+
}
60+
allowedWords = strings.Split(content, "\n")
61+
}
62+
63+
for _, item := range items {
64+
allowedWords = append(allowedWords, item.(string))
65+
}
66+
67+
return allowedWords, nil
68+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.24.0
55
require (
66
github.com/fatih/camelcase v1.0.0
77
github.com/google/go-github/v56 v56.0.0
8+
github.com/joho/godotenv v1.5.1
89
github.com/xanzy/go-gitlab v0.115.0
910
golang.org/x/oauth2 v0.26.0
1011
gopkg.in/src-d/go-git.v4 v4.13.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFO
3838
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
3939
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
4040
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
41+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
42+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
4143
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
4244
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
4345
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88

99
"github.com/haproxytech/check-commit/v5/aspell"
1010
"github.com/haproxytech/check-commit/v5/version"
11+
"github.com/joho/godotenv"
1112
)
1213

1314
func main() {
15+
_ = godotenv.Load(".env")
1416
err := version.Set()
1517
if err != nil {
1618
log.Fatal(err)

0 commit comments

Comments
 (0)