Skip to content

Commit eda8d34

Browse files
committed
WIP: replace uses of stringid
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent eb6ca0c commit eda8d34

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

cmd/compose/bridge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
"github.com/distribution/reference"
2525
"github.com/docker/cli/cli/command"
26-
"github.com/docker/docker/pkg/stringid"
26+
stringid "github.com/docker/cli/cli/command/formatter"
2727
"github.com/docker/go-units"
2828
"github.com/moby/moby/api/types/image"
2929
"github.com/spf13/cobra"

cmd/compose/images.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
"github.com/containerd/platforms"
2929
"github.com/docker/cli/cli/command"
30-
"github.com/docker/docker/pkg/stringid"
30+
stringid "github.com/docker/cli/cli/command/formatter"
3131
"github.com/docker/go-units"
3232
"github.com/spf13/cobra"
3333

cmd/formatter/container.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/docker/cli/cli/command/formatter"
2626
"github.com/docker/compose/v2/pkg/api"
27-
"github.com/docker/docker/pkg/stringid"
2827
"github.com/docker/go-units"
2928
"github.com/moby/moby/api/types/container"
3029
)
@@ -137,7 +136,7 @@ func (c *ContainerContext) MarshalJSON() ([]byte, error) {
137136
// option being set, the full or truncated ID is returned.
138137
func (c *ContainerContext) ID() string {
139138
if c.trunc {
140-
return stringid.TruncateID(c.c.ID)
139+
return formatter.TruncateID(c.c.ID)
141140
}
142141
return c.c.ID
143142
}

pkg/compose/run.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package compose
1818

1919
import (
2020
"context"
21+
"crypto/rand"
22+
"encoding/hex"
2123
"errors"
2224
"fmt"
2325
"os"
@@ -27,9 +29,9 @@ import (
2729
"github.com/compose-spec/compose-go/v2/types"
2830
"github.com/docker/cli/cli"
2931
cmd "github.com/docker/cli/cli/command/container"
32+
"github.com/docker/cli/cli/command/formatter"
3033
"github.com/docker/compose/v2/pkg/api"
3134
"github.com/docker/compose/v2/pkg/progress"
32-
"github.com/docker/docker/pkg/stringid"
3335
)
3436

3537
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,
8385
return "", err
8486
}
8587

86-
slug := stringid.GenerateRandomID()
88+
slug := generateRandomID()
8789
if service.ContainerName == "" {
88-
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)
90+
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)
8991
}
9092
one := 1
9193
service.Scale = &one
@@ -207,3 +209,14 @@ func (s *composeService) startDependencies(ctx context.Context, project *types.P
207209
}
208210
return nil
209211
}
212+
213+
// generateRandomID returns a unique, 64-character ID consisting of a-z, 0-9.
214+
func generateRandomID() string {
215+
b := make([]byte, 32)
216+
for {
217+
if _, err := rand.Read(b); err != nil {
218+
panic(err) // This shouldn't happen
219+
}
220+
return hex.EncodeToString(b)
221+
}
222+
}

0 commit comments

Comments
 (0)