Skip to content

Commit 267b723

Browse files
jsorianoefd6
andcommitted
Suggestions from review.
Co-Authored-By: Dan Kortschak <[email protected]>
1 parent 8b2f932 commit 267b723

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/shirou/gopsutil/v3 v3.24.5
3535
github.com/spf13/cobra v1.9.1
3636
github.com/stretchr/testify v1.10.0
37+
golang.org/x/sys v0.30.0
3738
golang.org/x/tools v0.30.0
3839
gopkg.in/dnaeon/go-vcr.v3 v3.2.0
3940
gopkg.in/yaml.v3 v3.0.1
@@ -153,7 +154,6 @@ require (
153154
golang.org/x/net v0.35.0 // indirect
154155
golang.org/x/oauth2 v0.23.0 // indirect
155156
golang.org/x/sync v0.11.0 // indirect
156-
golang.org/x/sys v0.30.0 // indirect
157157
golang.org/x/term v0.29.0 // indirect
158158
golang.org/x/text v0.22.0 // indirect
159159
golang.org/x/time v0.7.0 // indirect

internal/common/bytesize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func parseFileSizeInt(s string) (uint64, error) {
5050
// the string representation in a format that can be unmarshaled back to an
5151
// equivalent value.
5252
func (s ByteSize) MarshalJSON() ([]byte, error) {
53-
return []byte(`"` + s.String() + `"`), nil
53+
return json.Marshal(s.String())
5454
}
5555

5656
// MarshalYAML implements the yaml.Marshaler interface for FileSize, it returns

internal/docker/imagesgc.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111
"fmt"
1212
"io/fs"
1313
"os"
14-
"os/exec"
1514
"path/filepath"
1615
"regexp"
1716
"slices"
1817
"strings"
1918
"time"
2019

20+
"golang.org/x/sys/execabs"
21+
2122
"github.com/elastic/elastic-package/internal/common"
2223
)
2324

@@ -54,7 +55,7 @@ type imagesGCClient interface {
5455
// ListImages should list local images in the same format as "docker-compose images".
5556
ListImages() ([]string, error)
5657

57-
// RemoveImage should try to remove an image. If the image is busy, it should return a known error.
58+
// RemoveImage should try to remove an image. If the image is busy, it returns ErrBusyImage.
5859
RemoveImage(image string) error
5960

6061
// TotalImagesSize returns the total size of the local images.
@@ -116,7 +117,7 @@ func (gc *ImagesGC) Persist() error {
116117
if err != nil {
117118
return fmt.Errorf("failed to encode list of images: %w", err)
118119
}
119-
return os.WriteFile(gc.path, d, 0644)
120+
return os.WriteFile(gc.path, d, 0o644)
120121
}
121122

122123
// Track images before they are downloaded. Images already present are ignored if they are not already tracked.
@@ -129,20 +130,21 @@ func (gc *ImagesGC) Track(images ...string) error {
129130
now := gc.clock()
130131
for _, image := range images {
131132
currentIndex := slices.IndexFunc(gc.images, func(i gcEntry) bool { return i.ImageTag == image })
132-
if slices.Contains(present, image) && currentIndex < 0 {
133-
// We don't track images already present.
133+
if currentIndex >= 0 {
134+
// Already tracked, update last used time.
135+
gc.images[currentIndex].LastUsed = now
134136
continue
135137
}
136138

137-
if currentIndex < 0 {
138-
gc.images = append(gc.images, gcEntry{
139-
ImageTag: image,
140-
LastUsed: now,
141-
})
139+
if slices.Contains(present, image) {
140+
// Don't track images already present.
142141
continue
143142
}
144143

145-
gc.images[currentIndex].LastUsed = now
144+
gc.images = append(gc.images, gcEntry{
145+
ImageTag: image,
146+
LastUsed: now,
147+
})
146148
}
147149

148150
return nil
@@ -196,18 +198,18 @@ func (gc *ImagesGC) Run() error {
196198
type localImagesGCClient struct {
197199
}
198200

199-
func defaultImagesGCClient() *localImagesGCClient {
200-
return &localImagesGCClient{}
201+
func defaultImagesGCClient() localImagesGCClient {
202+
return localImagesGCClient{}
201203
}
202204

203-
func (c *localImagesGCClient) ListImages() ([]string, error) {
204-
cmd := exec.Command("docker", "image", "list", "--format=json")
205+
func (localImagesGCClient) ListImages() ([]string, error) {
206+
cmd := execabs.Command("docker", "image", "list", "--format=json")
205207
errOutput := new(bytes.Buffer)
206208
cmd.Stderr = errOutput
207209

208210
output, err := cmd.Output()
209211
if err != nil {
210-
return nil, fmt.Errorf("docker image list failed (stderr=%q): %w", errOutput.String(), err)
212+
return nil, fmt.Errorf("docker image list failed (stderr=%q): %w", errOutput, err)
211213
}
212214

213215
var line struct {
@@ -229,8 +231,8 @@ func (c *localImagesGCClient) ListImages() ([]string, error) {
229231

230232
var removeConflictRegexp = regexp.MustCompile("container [^/s]+ is using its referenced image [^/s]+")
231233

232-
func (c *localImagesGCClient) RemoveImage(image string) error {
233-
cmd := exec.Command("docker", "image", "rm", image)
234+
func (localImagesGCClient) RemoveImage(image string) error {
235+
cmd := execabs.Command("docker", "image", "rm", image)
234236
errOutput := new(bytes.Buffer)
235237
cmd.Stderr = errOutput
236238

@@ -246,14 +248,14 @@ func (c *localImagesGCClient) RemoveImage(image string) error {
246248
return nil
247249
}
248250

249-
func (c *localImagesGCClient) TotalImagesSize() (common.ByteSize, error) {
250-
cmd := exec.Command("docker", "system", "df", "--format=json")
251+
func (localImagesGCClient) TotalImagesSize() (common.ByteSize, error) {
252+
cmd := execabs.Command("docker", "system", "df", "--format=json")
251253
errOutput := new(bytes.Buffer)
252254
cmd.Stderr = errOutput
253255

254256
output, err := cmd.Output()
255257
if err != nil {
256-
return 0, fmt.Errorf("docker system df failed (stderr=%q): %w", errOutput.String(), err)
258+
return 0, fmt.Errorf("docker system df failed (stderr=%q): %w", errOutput, err)
257259
}
258260

259261
var df struct {

0 commit comments

Comments
 (0)