Skip to content

Commit 52bd7a3

Browse files
committed
Fix creating a temporary file
enable editing multi files with same name.
1 parent 4269766 commit 52bd7a3

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

cli/edit.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@ func Edit(path myS3.Path, params *config.AWSParams) {
1818

1919
object := myS3.GetObject(svc, path)
2020

21-
tempfilePath := createTempfile(path, object.Body)
22-
defer os.Remove(tempfilePath)
21+
tempDirPath, tempfilePath := createTempfile(path, object.Body)
22+
defer os.RemoveAll(tempDirPath)
2323

2424
editedBody := editFile(tempfilePath)
2525
object.Body = []byte(editedBody)
2626
myS3.PutObject(svc, path, object)
2727
}
2828

29-
func createTempfile(path myS3.Path, body []byte) (tempfilePath string) {
29+
func createTempfile(path myS3.Path, body []byte) (tempDirPath string, tempfilePath string) {
30+
tempDirPath, err := ioutil.TempDir("/tmp", "s3-edit")
31+
if err != nil {
32+
fmt.Println(err)
33+
os.Exit(1)
34+
}
3035
keys := strings.Split(path.Key, "/")
3136
fileName := keys[len(keys)-1]
32-
tempfilePath = "/tmp/" + fileName
37+
tempfilePath = tempDirPath + "/" + fileName
3338

3439
if err := ioutil.WriteFile(tempfilePath, body, os.ModePerm); err != nil {
3540
fmt.Println(err)

0 commit comments

Comments
 (0)