Skip to content
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

Replace golang.org/x/exp with stdlib #759

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ require (
github.com/yuin/goldmark v1.7.8
go.uber.org/dig v1.18.0
go.uber.org/multierr v1.11.0
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa
golang.org/x/oauth2 v0.26.0
golang.org/x/sys v0.30.0
golang.org/x/term v0.29.0
Expand Down Expand Up @@ -112,6 +111,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
golang.org/x/exp/typeparams v0.0.0-20231108232855-2478ac86f678 // indirect
golang.org/x/mod v0.23.0 // indirect
golang.org/x/net v0.35.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/tui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"cmp"
"fmt"
"math"
"os"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/mgutz/ansi"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/exp/constraints"

"github.com/stateful/runme/v3/internal/runner"
"github.com/stateful/runme/v3/internal/runner/client"
Expand Down Expand Up @@ -462,6 +462,6 @@ func (m tuiModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}

func clamp[T constraints.Ordered](x, a, b T) T {
func clamp[T cmp.Ordered](x, a, b T) T {
return min(b, max(a, x))
}
4 changes: 2 additions & 2 deletions internal/dockerexec/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"encoding/hex"
"io"
"math/rand"
"time"

"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"go.uber.org/zap"
"golang.org/x/exp/rand"
)

type Options struct {
Expand All @@ -34,7 +34,7 @@ func New(opts *Options) (*Docker, error) {
logger = zap.NewNop()
}

rnd := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))

d := &Docker{
client: c,
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"io"
"os"
"os/exec"
"slices"
"syscall"
"testing"
"time"

"github.com/creack/pty"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)

func init() {
Expand Down
3 changes: 1 addition & 2 deletions internal/runner/env_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package runner

import (
"errors"
"slices"
"strings"
"sync"

"golang.org/x/exp/slices"
)

// Limited by windows
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package runner
import (
"context"
"fmt"
"slices"
"sync"

"go.uber.org/zap"
"golang.org/x/exp/slices"

"github.com/stateful/runme/v3/internal/lru"
"github.com/stateful/runme/v3/internal/owl"
Expand Down
27 changes: 2 additions & 25 deletions internal/ulid/generator.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
package ulid

import (
"io"
"regexp"
"sync"
"time"

"github.com/oklog/ulid/v2"
"golang.org/x/exp/rand"
)

var (
entropy io.Reader
entropyOnce sync.Once
generator = DefaultGenerator
)

// DefaultEntropy returns a reader that generates ULID entropy.
// The default entropy function utilizes math/rand.Rand, which is not safe for concurrent use by multiple goroutines.
// Therefore, this function employs x/exp/rand, as recommended by the authors of the library.
func DefaultEntropy() io.Reader {
entropyOnce.Do(func() {
seed := uint64(time.Now().UnixNano())
source := rand.NewSource(seed)
rng := rand.New(source)

entropy = &ulid.LockedMonotonicReader{
MonotonicReader: ulid.Monotonic(rng, 0),
}
})
return entropy
}
var generator = DefaultGenerator

// IsULID checks if the given string is a valid ULID
// ULID pattern:
Expand Down Expand Up @@ -60,7 +37,7 @@ func GenerateID() string {
}

func DefaultGenerator() string {
entropy := DefaultEntropy()
entropy := ulid.DefaultEntropy()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After applying the code suggestion #759 (comment), I realize that we can use the DefaultEntropy() function from github.com/oklog/ulid/v2 since it also uses math/rand and LockedMonotonicReader internally.

https://github.com/oklog/ulid/blob/v2.1.0/ulid.go#L132-L140

now := time.Now()
ts := ulid.Timestamp(now)
return ulid.MustNew(ts, entropy).String()
Expand Down