Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
ciao-cli: For image downloads don't save the whole file into memory
Browse files Browse the repository at this point in the history
Use io.Copy() to stream from source to destination rather than loading
entirely into memory.

Fixes: #662

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
  • Loading branch information
rbradford committed Nov 24, 2016
1 parent c4de6ed commit 34ad7be
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions ciao-cli/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"io"
"os"
"strings"
"text/template"
Expand Down Expand Up @@ -350,7 +350,7 @@ func (cmd *imageDownloadCommand) parseArgs(args []string) []string {
return cmd.Flag.Args()
}

func (cmd *imageDownloadCommand) run(args []string) error {
func (cmd *imageDownloadCommand) run(args []string) (err error) {
client, err := imageServiceClient(*identityUser, *identityPassword, *tenantID)
if err != nil {
fatalf("Could not get Image service client [%s]\n", err)
Expand All @@ -361,19 +361,25 @@ func (cmd *imageDownloadCommand) run(args []string) error {
fatalf("Could not download image [%s]\n", err)
}

b, err := ioutil.ReadAll(r)
if err != nil {
fatalf("Could not read [%s]\n", err)
}

if cmd.file == "" {
fmt.Printf("%s\n", b)
} else {
err := ioutil.WriteFile(cmd.file, b, 0644)
dest := os.Stdout
if cmd.file != "" {
dest, err = os.Create(cmd.file)
defer func() {
closeErr := dest.Close()
if err == nil {
err = closeErr
}
}()
if err != nil {
fatalf("Could not write image to file [%s]\n", err)
fatalf("Could not create destination file: %s: %v", cmd.file, err)
}
}

_, err = io.Copy(dest, r)
if err != nil {
fatalf("Error copying to destination: %v", err)
}

return nil
}

Expand Down

0 comments on commit 34ad7be

Please sign in to comment.