Skip to content

MINOR: add option to set url_env and private_token_env for aspell #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .aspell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ allowed:
- json
- req
- nallowed
- url
2 changes: 2 additions & 0 deletions aspell/aspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (

type RemoteFile struct {
URL string `yaml:"url"`
URLEnv string `yaml:"url_env"`
HeaderFromENV string `yaml:"header_from_env"`
PrivateTokenENV string `yaml:"private_token_env"`
AllowedItemsKey string `yaml:"allowed_items_key"`
}

Expand Down
8 changes: 6 additions & 2 deletions aspell/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func New(filename string) (Aspell, error) {
var err error
fileExists := true
if data, err = os.ReadFile(filename); err != nil {
log.Printf("warning: aspell exceptions file not found (%s)", err)
log.Printf("warning: aspell exceptions file not found (%s)", err.Error())
fileExists = false
}

Expand All @@ -25,11 +25,15 @@ func New(filename string) (Aspell, error) {
}

var extraAllowedWords []string
if aspell.RemoteFile.URL != "" {
if aspell.RemoteFile.URL != "" || aspell.RemoteFile.URLEnv != "" {
extraAllowedWords, err = fetchRemoteFile(aspell)
if err != nil {
log.Printf("warning: aspell remote file (%s)", err.Error())
return Aspell{}, err
}
if len(extraAllowedWords) == 0 {
log.Printf("warning: aspell remote file is empty")
}
}

for i, word := range aspell.AllowedWords {
Expand Down
14 changes: 11 additions & 3 deletions aspell/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aspell

import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
Expand All @@ -11,8 +12,8 @@ import (

func fetchRemoteFile(aspell Aspell) ([]string, error) {
url := aspell.RemoteFile.URL
if url == "" {
return []string{}, nil
if aspell.RemoteFile.URLEnv != "" {
url = os.Getenv(aspell.RemoteFile.URLEnv)
}

req, err := http.NewRequest("GET", url, nil)
Expand All @@ -24,15 +25,22 @@ func fetchRemoteFile(aspell Aspell) ([]string, error) {
envValue := os.Getenv(aspell.RemoteFile.HeaderFromENV)
req.Header.Set(aspell.RemoteFile.HeaderFromENV, envValue)
}
if aspell.RemoteFile.PrivateTokenENV != "" {
envValue := os.Getenv(aspell.RemoteFile.PrivateTokenENV)
req.Header.Set("PRIVATE-TOKEN", envValue)
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error fetching remote file: %s", resp.Status)
}

var data map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
Expand Down