| 408 | } |
| 409 | |
| 410 | func moveFile(sourcePath, destPath string) error { |
| 411 | inputFile, err := os.Open(sourcePath) |
| 412 | if err != nil { |
| 413 | return fmt.Errorf("couldn't open source file: %s", err) |
| 414 | } |
| 415 | outputFile, err := os.Create(destPath) |
| 416 | if err != nil { |
| 417 | inputFile.Close() |
| 418 | return fmt.Errorf("couldn't open dest file: %s", err) |
| 419 | } |
| 420 | defer outputFile.Close() |
| 421 | _, err = io.Copy(outputFile, inputFile) |
| 422 | inputFile.Close() |
| 423 | if err != nil { |
| 424 | return fmt.Errorf("writing to output file failed: %s", err) |
| 425 | } |
| 426 | // The copy was successful, so now delete the original file |
| 427 | err = os.Remove(sourcePath) |
| 428 | if err != nil { |
| 429 | return fmt.Errorf("failed removing original file: %s", err) |
| 430 | } |
| 431 | return nil |
| 432 | } |