Unified error handling style
@@ -117,7 +117,7 @@ func Download(cfg *config.DfgetConfig, client dfclient.DaemonClient) error {
logger.Errorf("download by dragonfly error: %s", err)
return downloadFromSource(cfg, hdr)
}
- return err
+ return nil
}
func downloadFromSource(cfg *config.DfgetConfig, hdr map[string]string) (err error) {
@@ -161,10 +161,14 @@ func downloadFromSource(cfg *config.DfgetConfig, hdr map[string]string) (err err
}
written, err = io.Copy(target, response)
- if err == nil {
+ if err != nil {
+ logger.Errorf("copied %d bytes to %s, with error: %s", written, cfg.Output, err)
+ return err
+ }
logger.Infof("copied %d bytes to %s", written, cfg.Output)
end = time.Now()
fmt.Printf("Download from source success, time cost: %dms\n", end.Sub(start).Milliseconds())
+
// change permission
logger.Infof("change own to uid %d gid %d", basic.UserID, basic.UserGroup)
if err = os.Chown(cfg.Output, basic.UserID, basic.UserGroup); err != nil {
@@ -173,10 +177,6 @@ func downloadFromSource(cfg *config.DfgetConfig, hdr map[string]string) (err err
}
return nil
}
- logger.Errorf("copied %d bytes to %s, with error: %s",
- written, cfg.Output, err)
- return err
-}
func parseHeader(s []string) map[string]string {
hdr := map[string]string{}
