Skip to content

Commit 4be2e3d

Browse files
authored
Modernize for Go 1.25 (#31)
* Modernize for Go 1.25 Bump the go directive to 1.25 and drop the pinned toolchain, matching go-envconfig and go-limiter. Reject negative numDigits/numSymbols in Generate with a new ErrNegativeInput sentinel. Previously Generate(10, -3, 0, ...) computed chars = 10 - (-3) = 13 and silently returned a 13-character password instead of erroring. Replace the golangci-lint CI job (v1.57 config referencing removed linters like execinquery and exportloopref) with a make test target run across the macOS/Ubuntu/Windows matrix, and pin the actions with ratchet. Convert the error sentinels to fmt.Errorf for consistency. Fix docs: log.Printf(res) -> log.Print(res) (non-constant format string), the deprecated shields.io workflow badge, and the dead godoc.org link. Modernize tests: range-over-int loops and atomic.Int64. * Repoint dead documentation links The AgileBits discussion link 301-redirects to the 1Password community homepage; the original content is gone and was never archived. Repoint it to the current 1Password password generator page. Update the stale help.github.com contributing link to its canonical docs.github.com URL.
1 parent 631000f commit 4be2e3d

8 files changed

Lines changed: 57 additions & 188 deletions

File tree

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ just a few small guidelines you need to follow.
88

99
All submissions, including submissions by project members, require review. We
1010
use GitHub pull requests for this purpose. Consult
11-
[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
11+
[GitHub Help](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests) for more
1212
information on using pull requests.

.github/workflows/test.yml

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,39 @@
1-
name: Test
1+
name: 'Test'
22

33
on:
44
push:
55
branches:
6-
- main
6+
- 'main'
7+
tags:
8+
- '*'
79
pull_request:
810
branches:
9-
- main
11+
- 'main'
1012

1113
concurrency:
1214
group: '${{ github.workflow }}-${{ github.head_ref || github.ref }}'
1315
cancel-in-progress: true
1416

1517
jobs:
16-
lint:
17-
runs-on: 'ubuntu-latest'
18-
19-
steps:
20-
- uses: 'actions/checkout@v4'
21-
22-
- uses: 'actions/setup-go@v5'
23-
with:
24-
cache: false
25-
go-version-file: 'go.mod'
26-
27-
- uses: 'golangci/golangci-lint-action@v4'
28-
with:
29-
version: 'v1.57.2'
30-
skip-cache: true
31-
3218
test:
3319
strategy:
20+
fail-fast: false
3421
matrix:
3522
platform:
3623
- 'macos-latest'
3724
- 'ubuntu-latest'
3825
- 'windows-latest'
39-
fail-fast: false
4026

4127
runs-on: '${{ matrix.platform }}'
4228

4329
steps:
44-
- uses: 'actions/checkout@v4'
30+
- uses: 'actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0' # ratchet:actions/checkout@v7
4531

46-
- uses: 'actions/setup-go@v5'
32+
- uses: 'actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e' # ratchet:actions/setup-go@v7
4733
with:
48-
cache: false
4934
go-version-file: 'go.mod'
5035

51-
- shell: 'bash'
36+
- name: 'Test'
37+
shell: 'bash'
5238
run: |-
53-
go test \
54-
-count=1 \
55-
-race \
56-
-shuffle=on \
57-
-timeout=5m \
58-
-vet=all \
59-
./...
39+
make test

.golangci.yml

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

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test:
2+
@go test \
3+
-count=1 \
4+
-race \
5+
-shuffle=on \
6+
-timeout=5m \
7+
-vet=all \
8+
./...
9+
.PHONY: test

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
## Golang Password Generator
22

33
[![GoDoc](https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://pkg.go.dev/github.com/sethvargo/go-password/password)
4-
[![GitHub Actions](https://img.shields.io/github/workflow/status/sethvargo/go-password/Test?style=flat-square)](https://github.com/sethvargo/go-password/actions?query=workflow%3ATest)
4+
[![GitHub Actions](https://img.shields.io/github/actions/workflow/status/sethvargo/go-password/test.yml?style=flat-square)](https://github.com/sethvargo/go-password/actions/workflows/test.yml)
55

66
This library implements generation of random passwords with provided
77
requirements as described by [AgileBits
8-
1Password](https://discussions.agilebits.com/discussion/23842/how-random-are-the-generated-passwords)
8+
1Password](https://1password.com/password-generator)
99
in pure Golang. The algorithm is commonly used when generating website
1010
passwords.
1111

@@ -26,7 +26,7 @@ wpvbxlsc
2626
## Installation
2727

2828
```sh
29-
$ go get -u github.com/sethvargo/go-password/password
29+
$ go get github.com/sethvargo/go-password/password
3030
```
3131

3232
## Usage
@@ -47,11 +47,11 @@ func main() {
4747
if err != nil {
4848
log.Fatal(err)
4949
}
50-
log.Printf(res)
50+
log.Print(res)
5151
}
5252
```
5353

54-
See the [GoDoc](https://godoc.org/github.com/sethvargo/go-password) for more
54+
See the [GoDoc](https://pkg.go.dev/github.com/sethvargo/go-password/password) for more
5555
information.
5656

5757
## Testing

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module github.com/sethvargo/go-password
22

3-
toolchain go1.21.11
4-
5-
go 1.21
3+
go 1.25

password/generate.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
// if err != nil {
66
// log.Fatal(err)
77
// }
8-
// log.Printf(res)
8+
// log.Print(res)
99
//
1010
// Most functions are safe for concurrent use.
1111
package password
1212

1313
import (
1414
"crypto/rand"
15-
"errors"
1615
"fmt"
1716
"io"
1817
"math/big"
@@ -47,19 +46,23 @@ const (
4746
var (
4847
// ErrExceedsTotalLength is the error returned with the number of digits and
4948
// symbols is greater than the total length.
50-
ErrExceedsTotalLength = errors.New("number of digits and symbols must be less than total length")
49+
ErrExceedsTotalLength = fmt.Errorf("number of digits and symbols must be less than total length")
5150

5251
// ErrLettersExceedsAvailable is the error returned with the number of letters
5352
// exceeds the number of available letters and repeats are not allowed.
54-
ErrLettersExceedsAvailable = errors.New("number of letters exceeds available letters and repeats are not allowed")
53+
ErrLettersExceedsAvailable = fmt.Errorf("number of letters exceeds available letters and repeats are not allowed")
5554

5655
// ErrDigitsExceedsAvailable is the error returned with the number of digits
5756
// exceeds the number of available digits and repeats are not allowed.
58-
ErrDigitsExceedsAvailable = errors.New("number of digits exceeds available digits and repeats are not allowed")
57+
ErrDigitsExceedsAvailable = fmt.Errorf("number of digits exceeds available digits and repeats are not allowed")
5958

6059
// ErrSymbolsExceedsAvailable is the error returned with the number of symbols
6160
// exceeds the number of available symbols and repeats are not allowed.
62-
ErrSymbolsExceedsAvailable = errors.New("number of symbols exceeds available symbols and repeats are not allowed")
61+
ErrSymbolsExceedsAvailable = fmt.Errorf("number of symbols exceeds available symbols and repeats are not allowed")
62+
63+
// ErrNegativeInput is the error returned when the number of digits or symbols
64+
// is negative.
65+
ErrNegativeInput = fmt.Errorf("number of digits and symbols must not be negative")
6366
)
6467

6568
// Generator is the stateful generator which can be used to customize the list
@@ -129,6 +132,10 @@ func NewGenerator(i *GeneratorInput) (*Generator, error) {
129132
// The algorithm is fast, but it's not designed to be performant; it favors
130133
// entropy over speed. This function is safe for concurrent use.
131134
func (g *Generator) Generate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) (string, error) {
135+
if numDigits < 0 || numSymbols < 0 {
136+
return "", ErrNegativeInput
137+
}
138+
132139
letters := g.lowerLetters
133140
if !noUpper {
134141
letters += g.upperLetters

password/generate_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
type (
1212
MockReader struct {
13-
Counter int64
13+
Counter atomic.Int64
1414
}
1515
)
1616

@@ -19,8 +19,8 @@ const (
1919
)
2020

2121
func (mr *MockReader) Read(data []byte) (int, error) {
22-
for i := 0; i < len(data); i++ {
23-
data[i] = byte(atomic.AddInt64(&mr.Counter, 1))
22+
for i := range data {
23+
data[i] = byte(mr.Counter.Add(1))
2424
}
2525
return len(data), nil
2626
}
@@ -85,6 +85,18 @@ func testGeneratorGenerate(t *testing.T, reader io.Reader) {
8585
}
8686
})
8787

88+
t.Run("negative_input", func(t *testing.T) {
89+
t.Parallel()
90+
91+
if _, err := gen.Generate(10, -1, 0, false, false); !errors.Is(err, ErrNegativeInput) {
92+
t.Errorf("expected %q to be %q", err, ErrNegativeInput)
93+
}
94+
95+
if _, err := gen.Generate(10, 0, -1, false, false); !errors.Is(err, ErrNegativeInput) {
96+
t.Errorf("expected %q to be %q", err, ErrNegativeInput)
97+
}
98+
})
99+
88100
t.Run("gen_lowercase", func(t *testing.T) {
89101
t.Parallel()
90102

@@ -116,7 +128,7 @@ func testGeneratorGenerate(t *testing.T, reader io.Reader) {
116128
t.Run("gen_no_repeats", func(t *testing.T) {
117129
t.Parallel()
118130

119-
for i := 0; i < N; i++ {
131+
for range N {
120132
res, err := gen.Generate(52, 10, 30, false, false)
121133
if err != nil {
122134
t.Error(err)
@@ -153,7 +165,7 @@ func testGeneratorGenerateCustom(t *testing.T, reader io.Reader) {
153165
t.Fatal(err)
154166
}
155167

156-
for i := 0; i < N; i++ {
168+
for range N {
157169
res, err := gen.Generate(52, 10, 10, false, true)
158170
if err != nil {
159171
t.Error(err)

0 commit comments

Comments
 (0)