Skip to content

Commit

Permalink
change the path of saving objects to current users's home directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Abrar-Ahmed7 committed Feb 9, 2023
1 parent da97126 commit a7353f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
21 changes: 15 additions & 6 deletions internal/aws/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"fmt"
"os"
"os/user"
"strings"
"time"

"github.com/atotto/clipboard"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
Expand Down Expand Up @@ -75,16 +77,22 @@ func GetPreSignedUrl(sess session.Session, bucketName, key string) string {

func DownloadObject(sess session.Session, bucketName, key string) string {
downloader := s3manager.NewDownloader(&sess)
err := os.MkdirAll("./resource/s3/objects", os.ModePerm)
usr, err := user.Current()
if err != nil {
log.Info().Msg(fmt.Sprintf("error in creating CSV directory: %v", err))
log.Info().Msg(fmt.Sprintf("error in getting the machine's user: %v", err))
}
path := usr.HomeDir + "/cloud-lens/s3objects/"
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Info().Msg(fmt.Sprintf("error in creating s3 Object directory: %v", err))
}
files := strings.Split(key, "/")
objectName := files[len(files)-1]
p := fmt.Sprintf("./resource/s3/objects/%v", objectName)
p := fmt.Sprintf("%v%v", path, objectName)
log.Info().Msg(fmt.Sprintf("path: %v", p))
f, err := os.Create(p)
if err != nil {
fmt.Println("Failed to create file", err)
log.Info().Msg(fmt.Sprintf("Failed to create file, err: %v", err))
return ""
}
defer f.Close()
Expand All @@ -93,11 +101,12 @@ func DownloadObject(sess session.Session, bucketName, key string) string {
Key: aws.String(key),
})
if err != nil {
fmt.Println("failed to download file, err: ", err)
log.Info().Msg(fmt.Sprintf("failed to download file, err: %v", err))
return ""
}
clipboard.WriteAll(p)

return fmt.Sprintf("%v with size %d bytes, downloaded successfully", objectName, n)
return fmt.Sprintf("%v with size %d bytes, downloaded and its path copied to the clipboard", objectName, n)
}

func PutObjects(sess session.Session) {
Expand Down
8 changes: 7 additions & 1 deletion internal/view/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,14 @@ func (t *Table) importAsCSV(evt *tcell.EventKey) *tcell.EventKey {
}
csvFileName := strings.Split(t.GetTitle(), " ")
usr, err := user.Current()
path := usr.HomeDir + "/cloud-lens/CSV/"
if err != nil {
log.Info().Msg(fmt.Sprintf("error in getting the machine's user: %v", err))
}
path := usr.HomeDir + "/cloud-lens/csv/"
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Info().Msg(fmt.Sprintf("error in creating csv directory: %v", err))
}
path = filepath.Join(path + "/" + csvFileName[len(csvFileName)-2] + ".csv")
file, err := os.Create(path)
if err != nil {
Expand Down

0 comments on commit a7353f7

Please sign in to comment.