diff --git a/cmd/gowo/cmd/upload.go b/cmd/gowo/cmd/upload.go index 691a420..9ac5826 100644 --- a/cmd/gowo/cmd/upload.go +++ b/cmd/gowo/cmd/upload.go @@ -33,33 +33,32 @@ import ( "github.com/whats-this/owo.go" ) -func doUpload(cdn string, names []string) { - files, err := owo.FilesToNamedReaders(names) +func doUpload(cdn string, names []string) (err error) { + var files []owo.NamedReader + files, err = owo.FilesToNamedReaders(names) if err != nil { - log.Println("[upload]", err) return } - response, err := owo.UploadFiles(context.Background(), files) + var response *owo.Response + response, err = owo.UploadFiles(context.Background(), files) if err != nil { - log.Println("[upload]", err) - return - } - if !response.Success { - log.Printf("[upload] %d: %s", response.Errorcode, response.Description) return } buf := bytes.Buffer{} + var url string for _, file := range response.Files { - if file.Error { - log.Printf("%d: %s", file.Errorcode, file.Description) - continue + url, err = file.WithCDN(cdn) + if err != nil { + log.Println("[upload]", err) } - fmt.Fprintf(&buf, "%s\n", file.WithCDN(cdn)) + fmt.Fprintf(&buf, "%s\n", url) } - if err := output(buf.String(), len(response.Files)); err != nil { - log.Println("[upload]", err) + err = output(buf.String(), len(response.Files)) + if err != nil { + return } response = nil + return } // uploadCmd represents the upload command @@ -72,7 +71,9 @@ var uploadCmd = &cobra.Command{ log.Fatal("Need at least one file.") } cdn := viper.GetString("cdn") - doUpload(cdn, args) + if err := doUpload(cdn, args); err != nil { + log.Fatal(err) + } }, } diff --git a/cmd/gowo/cmd/watch.go b/cmd/gowo/cmd/watch.go index 998f797..5b83af4 100644 --- a/cmd/gowo/cmd/watch.go +++ b/cmd/gowo/cmd/watch.go @@ -79,7 +79,12 @@ var watchCmd = &cobra.Command{ continue } - go doUpload(cdn, names) + go func() { + err := doUpload(cdn, names) + if err != nil { + log.Print(err) + } + }() } }() diff --git a/response.go b/response.go index a741d28..bfc6376 100644 --- a/response.go +++ b/response.go @@ -55,6 +55,10 @@ func (e ErrUploadFailed) Error() string { } // WithCDN returns file url prefixed with the CDN -func (f File) WithCDN(cdn string) string { - return cdn + f.URL +func (f File) WithCDN(cdn string) (string, error) { + if f.Error { + return "", ErrUploadFailed{f.Description, f.Errorcode} + } + + return cdn + f.URL, nil }