diff --git a/container/container.go b/container/container.go index 4094717..35b39eb 100644 --- a/container/container.go +++ b/container/container.go @@ -12,12 +12,12 @@ import ( "time" "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/events" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/network" - "github.com/google/uuid" 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 // additional functionality such as exposing container stats. type Client struct { - id uuid.UUID + id shortid.ID ctx context.Context cancel context.CancelFunc wg sync.WaitGroup @@ -62,7 +62,7 @@ type Client struct { // NewClient creates a new Client. 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"}) if err != nil { return nil, fmt.Errorf("network create: %w", err) diff --git a/container/integration_test.go b/container/integration_test.go index 33bd228..6850fc1 100644 --- a/container/integration_test.go +++ b/container/integration_test.go @@ -9,10 +9,10 @@ import ( "time" "git.netflux.io/rob/termstream/container" + "git.netflux.io/rob/termstream/shortid" "git.netflux.io/rob/termstream/testhelpers" typescontainer "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" - "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -24,7 +24,7 @@ func TestIntegrationClientStartStop(t *testing.T) { logger := testhelpers.NewTestLogger() apiClient, err := client.NewClientWithOpts(client.FromEnv) require.NoError(t, err) - containerName := "termstream-test-" + uuid.NewString() + containerName := "termstream-test-" + shortid.New().String() component := "test-start-stop" client, err := container.NewClient(ctx, apiClient, logger) @@ -173,7 +173,7 @@ func TestContainerRestart(t *testing.T) { logger := testhelpers.NewTestLogger() apiClient, err := client.NewClientWithOpts(client.FromEnv) require.NoError(t, err) - containerName := "termstream-test-" + uuid.NewString() + containerName := "termstream-test-" + shortid.New().String() component := "test-restart" client, err := container.NewClient(ctx, apiClient, logger) diff --git a/go.mod b/go.mod index 4edecf9..1247d44 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/docker/docker v28.0.0+incompatible github.com/docker/go-connections v0.5.0 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/rivo/tview v0.0.0-20241227133733-17b7edb88c57 github.com/stretchr/testify v1.10.0 diff --git a/shortid/shortid.go b/shortid/shortid.go new file mode 100644 index 0000000..d2cdedf --- /dev/null +++ b/shortid/shortid.go @@ -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)) +} diff --git a/shortid/shortid_test.go b/shortid/shortid_test.go new file mode 100644 index 0000000..c1abb67 --- /dev/null +++ b/shortid/shortid_test.go @@ -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) +}