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

Commit

Permalink
Merge pull request #859 from rbradford/ciao-cli-image-optimisation
Browse files Browse the repository at this point in the history
Fix resource issues with ciao-cli image
  • Loading branch information
markdryan authored Nov 25, 2016
2 parents 480a803 + 34ad7be commit 4b668bf
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions ciao-cli/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package main

import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"io"
"os"
"strings"
"text/template"
Expand Down Expand Up @@ -350,7 +349,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 +360,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 Expand Up @@ -427,14 +432,7 @@ func uploadTenantImage(username, password, tenant, image, filename string) error
}
defer file.Close()

fileInfo, _ := file.Stat()
var size = fileInfo.Size()
buffer := make([]byte, size)

file.Read(buffer)
fileBytes := bytes.NewReader(buffer)

res := images.Upload(client, image, fileBytes)
res := images.Upload(client, image, file)
if res.Err != nil {
fatalf("Could not upload %s [%s]", filename, res.Err)
}
Expand Down

0 comments on commit 4b668bf

Please sign in to comment.