Skip to content

Commit 8baf572

Browse files
committed
MEDIUM: add init and help CLI commands for aspell config
Refactored the argument parsing in main.go to use a switch statement and introduced two new CLI commands to improve user experience with configuration: - `init`: Generates a default, commented-out `.aspell.yml` configuration template in the current directory to help users scaffold their settings. - `help`: Prints all available configuration options and their documentation directly to stdout.
1 parent 231f385 commit 8baf572

File tree

2 files changed

+97
-10
lines changed

2 files changed

+97
-10
lines changed

aspell/config_template.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package aspell
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
// configTemplate is the full .aspell.yml with all options commented out and documented.
9+
const configTemplate = `## Spell check scope: disabled (off), subject (first line only),
10+
## commit (full commit message), all (commit + file content, default: subject)
11+
# mode: disabled | subject | commit | all
12+
13+
## Minimum word length to spell-check (words shorter than this are skipped, default: 3)
14+
# min_length: 3
15+
16+
## If true, code identifiers will NOT be excluded from spell check
17+
# no_ignore_identifiers: false
18+
19+
## Where to collect code identifiers from to exclude as allowed words (default: files)
20+
## diff - only from the diff content (added lines)
21+
## files - from the full content of changed files
22+
## all - from every file in the repository
23+
# identifier_scope: diff | files | all
24+
25+
## File patterns to skip when mode is "all" (glob syntax)
26+
# ignore_files:
27+
# - '*test.go'
28+
# - 'gen/*'
29+
30+
## Extra words to accept as correctly spelled (one per line, case-insensitive)
31+
# allowed:
32+
# - myword
33+
# - anotherword
34+
35+
## Fetch an additional allowed-word list from a JSON API endpoint
36+
# remote_file:
37+
# ## Direct URL to fetch
38+
# url: "https://example.com/api/words"
39+
# ## Or read the URL from this environment variable instead
40+
# url_env: ALLOWED_WORDS_URL
41+
# ## Set a custom HTTP header whose value is read from the named env var
42+
# header_from_env: X-Custom-Header
43+
# ## Set PRIVATE-TOKEN header from this env var (GitLab/GitHub auth)
44+
# private_token_env: PRIVATE_TOKEN
45+
# ## JSON key that contains the word list in the response
46+
# allowed_items_key: words
47+
48+
## Fetch dictionary files (.txt word lists or .rws aspell compiled dicts) from remote sources
49+
# dictionaries:
50+
# ## Fetch every .txt/.rws file from a GitHub repo directory
51+
# github:
52+
# - url: "https://github.com/haproxytech/check-commit/tree/main/aspell/dictionaries"
53+
# ## Optional env var holding a GitHub token (needed for private repos)
54+
# token_env: GITHUB_TOKEN
55+
# ## Fetch every .txt/.rws file from a GitLab repo directory (supports self-hosted instances)
56+
# gitlab:
57+
# - url: "https://gitlab.com/group/project/-/tree/main/path/to/dictionaries"
58+
# ## Optional env var holding a GitLab private token (needed for private repos)
59+
# token_env: GITLAB_TOKEN
60+
# ## Fetch individual dictionary files by direct URL
61+
# urls:
62+
# - "https://raw.githubusercontent.com/haproxytech/check-commit/main/aspell/dictionaries/computing.txt"
63+
# - "https://example.com/custom-words.txt"
64+
`
65+
66+
// PrintHelp prints all available .aspell.yml configuration options to stdout.
67+
func PrintHelp() {
68+
fmt.Print(".aspell.yml configuration options:\n\n")
69+
fmt.Print(configTemplate)
70+
}
71+
72+
// Init creates a .aspell.yml file with all options commented out.
73+
// Returns an error if the file already exists.
74+
func Init(filename string) error {
75+
if _, err := os.Stat(filename); err == nil {
76+
return fmt.Errorf("%s already exists", filename)
77+
}
78+
return os.WriteFile(filename, []byte(configTemplate), 0o644)
79+
}

main.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,25 @@ func main() {
2222
log.Fatal(err)
2323
}
2424
if len(os.Args) == 2 {
25-
for _, arg := range os.Args[1:] {
26-
if arg == "version" {
27-
fmt.Println("check-commit", version.Version)
28-
fmt.Println("built from:", version.Repo)
29-
fmt.Println("commit date:", version.CommitDate)
30-
os.Exit(0)
31-
}
32-
if arg == "tag" {
33-
fmt.Println(version.Tag)
34-
os.Exit(0)
25+
switch os.Args[1] {
26+
case "version":
27+
fmt.Println("check-commit", version.Version)
28+
fmt.Println("built from:", version.Repo)
29+
fmt.Println("commit date:", version.CommitDate)
30+
os.Exit(0)
31+
case "tag":
32+
fmt.Println(version.Tag)
33+
os.Exit(0)
34+
case "help":
35+
aspell.PrintHelp()
36+
os.Exit(0)
37+
case "init":
38+
if err := aspell.Init(".aspell.yml"); err != nil {
39+
fmt.Fprintf(os.Stderr, "error: %s\n", err)
40+
os.Exit(1)
3541
}
42+
fmt.Println(".aspell.yml created")
43+
os.Exit(0)
3644
}
3745
}
3846
log.SetFlags(log.LstdFlags | log.Lshortfile)

0 commit comments

Comments
 (0)