WIP: replace uses of stringid

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-07-21 20:48:22 +02:00
parent af4c231b2c
commit 252ddd8da9
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 19 additions and 7 deletions

View File

@ -23,7 +23,7 @@ import (
"github.com/distribution/reference"
"github.com/docker/cli/cli/command"
"github.com/docker/docker/pkg/stringid"
stringid "github.com/docker/cli/cli/command/formatter"
"github.com/docker/go-units"
"github.com/moby/moby/api/types/image"
"github.com/spf13/cobra"

View File

@ -27,7 +27,7 @@ import (
"github.com/containerd/platforms"
"github.com/docker/cli/cli/command"
"github.com/docker/docker/pkg/stringid"
stringid "github.com/docker/cli/cli/command/formatter"
"github.com/docker/go-units"
"github.com/spf13/cobra"

View File

@ -24,7 +24,6 @@ import (
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/go-units"
"github.com/moby/moby/api/types/container"
)
@ -137,7 +136,7 @@ func (c *ContainerContext) MarshalJSON() ([]byte, error) {
// option being set, the full or truncated ID is returned.
func (c *ContainerContext) ID() string {
if c.trunc {
return stringid.TruncateID(c.c.ID)
return formatter.TruncateID(c.c.ID)
}
return c.c.ID
}

View File

@ -18,6 +18,8 @@ package compose
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"os"
@ -27,9 +29,9 @@ import (
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli"
cmd "github.com/docker/cli/cli/command/container"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/docker/pkg/stringid"
)
func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) {
@ -83,9 +85,9 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
return "", err
}
slug := stringid.GenerateRandomID()
slug := generateRandomID()
if service.ContainerName == "" {
service.ContainerName = fmt.Sprintf("%[1]s%[4]s%[2]s%[4]srun%[4]s%[3]s", project.Name, service.Name, stringid.TruncateID(slug), api.Separator)
service.ContainerName = fmt.Sprintf("%[1]s%[4]s%[2]s%[4]srun%[4]s%[3]s", project.Name, service.Name, formatter.TruncateID(slug), api.Separator)
}
one := 1
service.Scale = &one
@ -207,3 +209,14 @@ func (s *composeService) startDependencies(ctx context.Context, project *types.P
}
return nil
}
// generateRandomID returns a unique, 64-character ID consisting of a-z, 0-9.
func generateRandomID() string {
b := make([]byte, 32)
for {
if _, err := rand.Read(b); err != nil {
panic(err) // This shouldn't happen
}
return hex.EncodeToString(b)
}
}