Skip to content

Commit

Permalink
Fix creating a temporary file
Browse files Browse the repository at this point in the history
enable editing multi files with same name.
  • Loading branch information
ryosms committed Jan 10, 2019
1 parent 4269766 commit 52bd7a3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions cli/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ func Edit(path myS3.Path, params *config.AWSParams) {

object := myS3.GetObject(svc, path)

tempfilePath := createTempfile(path, object.Body)
defer os.Remove(tempfilePath)
tempDirPath, tempfilePath := createTempfile(path, object.Body)
defer os.RemoveAll(tempDirPath)

editedBody := editFile(tempfilePath)
object.Body = []byte(editedBody)
myS3.PutObject(svc, path, object)
}

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

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

0 comments on commit 52bd7a3

Please sign in to comment.