chore: remove uuid dependency

This commit is contained in:
Rob Watson 2025-02-24 05:54:40 +01:00
parent bfdf3bc39f
commit b4edb27657
5 changed files with 48 additions and 7 deletions

View File

@ -12,12 +12,12 @@ import (
"time" "time"
"git.netflux.io/rob/termstream/domain" "git.netflux.io/rob/termstream/domain"
"git.netflux.io/rob/termstream/shortid"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events" "github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/google/uuid"
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
) )
@ -51,7 +51,7 @@ type DockerClient interface {
// Client provides a thin wrapper around the Docker API client, and provides // Client provides a thin wrapper around the Docker API client, and provides
// additional functionality such as exposing container stats. // additional functionality such as exposing container stats.
type Client struct { type Client struct {
id uuid.UUID id shortid.ID
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
wg sync.WaitGroup wg sync.WaitGroup
@ -62,7 +62,7 @@ type Client struct {
// NewClient creates a new Client. // NewClient creates a new Client.
func NewClient(ctx context.Context, apiClient DockerClient, logger *slog.Logger) (*Client, error) { func NewClient(ctx context.Context, apiClient DockerClient, logger *slog.Logger) (*Client, error) {
id := uuid.New() id := shortid.New()
network, err := apiClient.NetworkCreate(ctx, "termstream-"+id.String(), network.CreateOptions{Driver: "bridge"}) network, err := apiClient.NetworkCreate(ctx, "termstream-"+id.String(), network.CreateOptions{Driver: "bridge"})
if err != nil { if err != nil {
return nil, fmt.Errorf("network create: %w", err) return nil, fmt.Errorf("network create: %w", err)

View File

@ -9,10 +9,10 @@ import (
"time" "time"
"git.netflux.io/rob/termstream/container" "git.netflux.io/rob/termstream/container"
"git.netflux.io/rob/termstream/shortid"
"git.netflux.io/rob/termstream/testhelpers" "git.netflux.io/rob/termstream/testhelpers"
typescontainer "github.com/docker/docker/api/types/container" typescontainer "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/google/uuid"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -24,7 +24,7 @@ func TestIntegrationClientStartStop(t *testing.T) {
logger := testhelpers.NewTestLogger() logger := testhelpers.NewTestLogger()
apiClient, err := client.NewClientWithOpts(client.FromEnv) apiClient, err := client.NewClientWithOpts(client.FromEnv)
require.NoError(t, err) require.NoError(t, err)
containerName := "termstream-test-" + uuid.NewString() containerName := "termstream-test-" + shortid.New().String()
component := "test-start-stop" component := "test-start-stop"
client, err := container.NewClient(ctx, apiClient, logger) client, err := container.NewClient(ctx, apiClient, logger)
@ -173,7 +173,7 @@ func TestContainerRestart(t *testing.T) {
logger := testhelpers.NewTestLogger() logger := testhelpers.NewTestLogger()
apiClient, err := client.NewClientWithOpts(client.FromEnv) apiClient, err := client.NewClientWithOpts(client.FromEnv)
require.NoError(t, err) require.NoError(t, err)
containerName := "termstream-test-" + uuid.NewString() containerName := "termstream-test-" + shortid.New().String()
component := "test-restart" component := "test-restart"
client, err := container.NewClient(ctx, apiClient, logger) client, err := container.NewClient(ctx, apiClient, logger)

1
go.mod
View File

@ -6,7 +6,6 @@ require (
github.com/docker/docker v28.0.0+incompatible github.com/docker/docker v28.0.0+incompatible
github.com/docker/go-connections v0.5.0 github.com/docker/go-connections v0.5.0
github.com/gdamore/tcell/v2 v2.8.1 github.com/gdamore/tcell/v2 v2.8.1
github.com/google/uuid v1.6.0
github.com/opencontainers/image-spec v1.1.0 github.com/opencontainers/image-spec v1.1.0
github.com/rivo/tview v0.0.0-20241227133733-17b7edb88c57 github.com/rivo/tview v0.0.0-20241227133733-17b7edb88c57
github.com/stretchr/testify v1.10.0 github.com/stretchr/testify v1.10.0

25
shortid/shortid.go Normal file
View File

@ -0,0 +1,25 @@
package shortid
import (
"crypto/rand"
"fmt"
)
const defaultLenBytes = 6
// ID is a short ID. It is not intended to guarantee uniqueness or any other
// cryptographic property and should only be used for labelling and other
// non-critical purposes.
type ID []byte
// New generates a new short ID, of length 6 bytes.
func New() ID {
p := make([]byte, defaultLenBytes)
_, _ = rand.Read(p)
return ID(p)
}
// String implements the fmt.Stringer interface.
func (id ID) String() string {
return fmt.Sprintf("%x", []byte(id))
}

17
shortid/shortid_test.go Normal file
View File

@ -0,0 +1,17 @@
package shortid_test
import (
"testing"
"git.netflux.io/rob/termstream/shortid"
"github.com/stretchr/testify/assert"
)
func TestShortID(t *testing.T) {
id := shortid.New()
assert.Len(t, id, 6)
assert.Len(t, id.String(), 12)
anotherID := shortid.New()
assert.NotEqual(t, id, anotherID)
}