Skip to content

Commit d48006a

Browse files
unpack local image file with prefix file:// (#318)
Co-authored-by: Ettore Di Giacinto <[email protected]>
1 parent 5ee1ff6 commit d48006a

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.swp
2+
.idea/
23
luet
34
tests/integration/shunit2
45
tests/integration/bin

cmd/util.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"path/filepath"
2222
"runtime"
23+
"strings"
2324

2425
"github.com/docker/docker/api/types"
2526
"github.com/docker/go-units"
@@ -34,6 +35,10 @@ import (
3435
"github.com/spf13/cobra"
3536
)
3637

38+
const (
39+
filePrefix = "file://"
40+
)
41+
3742
func pack(ctx *context.Context, p, dst, imageName, arch, OS string) error {
3843

3944
tempimage, err := ctx.TempFile("tempimage")
@@ -126,7 +131,7 @@ func NewUnpackCommand() *cobra.Command {
126131
RegistryToken: registryToken,
127132
}
128133

129-
if !local {
134+
if !local && !strings.HasPrefix(image, filePrefix) {
130135
info, err := docker.DownloadAndExtractDockerImage(util.DefaultContext, image, destination, auth, verify)
131136
if err != nil {
132137
util.DefaultContext.Error(err.Error())

pkg/helpers/docker/docker.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
"encoding/hex"
2121
"net/http"
2222
"os"
23+
"strings"
24+
25+
v1 "github.com/google/go-containerregistry/pkg/v1"
26+
"github.com/google/go-containerregistry/pkg/v1/tarball"
2327

2428
"github.com/containerd/containerd/images"
2529
luetimages "github.com/mudler/luet/pkg/api/core/image"
@@ -42,6 +46,11 @@ import (
4246
"github.com/theupdateframework/notary/tuf/data"
4347
)
4448

49+
const (
50+
filePrefix = "file://"
51+
fileImageSeparator = ":/"
52+
)
53+
4554
// See also https://github.com/docker/cli/blob/88c6089300a82d3373892adf6845a4fed1a4ba8d/cli/command/image/trust.go#L171
4655

4756
func verifyImage(image string, authConfig *types.AuthConfig) (string, error) {
@@ -196,18 +205,26 @@ func DownloadAndExtractDockerImage(ctx luettypes.Context, image, dest string, au
196205
}
197206

198207
func ExtractDockerImage(ctx luettypes.Context, local, dest string) (*images.Image, error) {
208+
var img v1.Image
199209
if !fileHelper.Exists(dest) {
200210
if err := os.MkdirAll(dest, os.ModePerm); err != nil {
201211
return nil, errors.Wrapf(err, "cannot create destination directory")
202212
}
203213
}
204214

205-
ref, err := name.ParseReference(local)
206-
if err != nil {
207-
return nil, err
215+
var err error
216+
if strings.HasPrefix(local, filePrefix) {
217+
parts := strings.Split(local, fileImageSeparator)
218+
if len(parts) == 2 && parts[1] != "" {
219+
img, err = tarball.ImageFromPath(parts[1], nil)
220+
}
221+
} else {
222+
ref, err := name.ParseReference(local)
223+
if err != nil {
224+
return nil, err
225+
}
226+
img, err = daemon.Image(ref)
208227
}
209-
210-
img, err := daemon.Image(ref)
211228
if err != nil {
212229
return nil, err
213230
}

0 commit comments

Comments
 (0)