Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
fix(*): properly load images on import
Browse files Browse the repository at this point in the history
Import was not properly loading images because
the filepath.Walk function was opening the
archives directory like it was a file and then
passing that into the ImageLoad function which
failed silently.

This code change does two things:
1. It checks os.FileInfo to see if the path is
a directory and returns if it is. If the path is not
a directory, it proceeds to import the image archive
file.
2. It adds a verbose flag to the import command. If
enabled, it streams the ouput from image load to stdout.

resolves #469
  • Loading branch information
Michelle Noorali committed Nov 21, 2018
1 parent 0041b00 commit 5228aaf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cmd/duffle/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type importCmd struct {
out io.Writer
home home.Home
insecure bool
verbose bool
}

func newImportCmd(w io.Writer) *cobra.Command {
Expand All @@ -46,6 +47,7 @@ func newImportCmd(w io.Writer) *cobra.Command {
f := cmd.Flags()
f.StringVarP(&importc.dest, "destination", "d", "", "Location to unpack bundle")
f.BoolVarP(&importc.insecure, "insecure", "k", false, "Do not verify the bundle (INSECURE)")
f.BoolVarP(&importc.verbose, "verbose", "v", false, "Verbose output")

return cmd
}
Expand All @@ -66,7 +68,7 @@ func (im *importCmd) run() error {
return err
}

imp, err := packager.NewImporter(source, dest, l)
imp, err := packager.NewImporter(source, dest, l, im.verbose)
if err != nil {
return err
}
Expand Down
23 changes: 20 additions & 3 deletions pkg/packager/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -25,14 +26,15 @@ type Importer struct {
Destination string
Client *client.Client
Loader loader.Loader
Verbose bool
}

// NewImporter creates a new secure *Importer
//
// source is the filesystem path to the archive.
// destination is the directory to unpack the contents.
// load is a loader.Loader preconfigured for loading secure or insecure bundles.
func NewImporter(source, destination string, load loader.Loader) (*Importer, error) {
func NewImporter(source, destination string, load loader.Loader, verbose bool) (*Importer, error) {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, err
Expand All @@ -47,6 +49,7 @@ func NewImporter(source, destination string, load loader.Loader) (*Importer, err
Destination: destination,
Client: cli,
Loader: load,
Verbose: verbose,
}, nil
}

Expand Down Expand Up @@ -97,13 +100,27 @@ func (im *Importer) Import() error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = im.Client.ImageLoad(context.Background(), file, true) // quiet = true
return err
out, err := im.Client.ImageLoad(context.Background(), file, false)
if err != nil {
return err
}
defer out.Body.Close()

if im.Verbose {
io.Copy(os.Stdout, out.Body)
}

return nil
})
}

Expand Down

0 comments on commit 5228aaf

Please sign in to comment.