Skip to content

Commit

Permalink
Add progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonhilkert committed Oct 1, 2015
1 parent 8c047f4 commit 2b25b36
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package main

import (
"flag"
"fmt"
"log"
"mime"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"

"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/s3"
"github.com/rakyll/pb"
)

const (
Expand All @@ -30,6 +31,8 @@ var (
)

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())

log.SetOutput(os.Stdout)
log.SetFlags(0)

Expand All @@ -51,16 +54,17 @@ func main() {
fileList, err := GetFileList(*directory)

if err != nil {
log.Fatal(err)
panic(err)
}

for _, path := range fileList {
err := UploadFile(path, *directory, *bucket, *key)
count := len(fileList)
bar := pb.StartNew(count)

if err != nil {
log.Fatal(err)
}
for _, path := range fileList {
UploadFile(path, *directory, *bucket, *key)
bar.Increment()
}
bar.FinishPrint("All done!")
}

func GetFileList(dir string) (fileList []string, err error) {
Expand All @@ -74,24 +78,24 @@ func GetFileList(dir string) (fileList []string, err error) {
return fileList, err
}

func UploadFile(path string, dir string, bucketName string, prefixKey string) (err error) {
func UploadFile(path string, dir string, bucketName string, prefixKey string) {
auth, err := aws.EnvAuth()

if err != nil {
return err
panic(err)
}

client := s3.New(auth, aws.USEast)
b := client.Bucket(bucketName)

file, err := os.Open(path)
if err != nil {
return err
panic(err)
}

stat, err := file.Stat()
if err != nil {
return err
panic(err)
}

ext := filepath.Ext(path)
Expand All @@ -100,6 +104,7 @@ func UploadFile(path string, dir string, bucketName string, prefixKey string) (e
headers := map[string][]string{
"Content-Length": {strconv.FormatInt(stat.Size(), 10)},
"Content-Type": {fileType},
"Cache-Control": {"max-age=31104000"},
}

relativePath := strings.TrimPrefix(path, dir+"/")
Expand All @@ -111,12 +116,8 @@ func UploadFile(path string, dir string, bucketName string, prefixKey string) (e
err = b.PutReaderHeader(relativePath, file, stat.Size(), headers, s3.ACL("public-read"))

if err != nil {
return err
panic(err)
}

fmt.Println(relativePath)

return nil
}

func printHelp() {
Expand Down

0 comments on commit 2b25b36

Please sign in to comment.