-
Notifications
You must be signed in to change notification settings - Fork 26
/
buck_obj.go
120 lines (104 loc) · 3.8 KB
/
buck_obj.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package view
import (
"context"
"fmt"
"github.com/atotto/clipboard"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloudlens/internal"
"github.com/one2nc/cloudlens/internal/aws"
"github.com/one2nc/cloudlens/internal/ui"
"github.com/rs/zerolog/log"
)
type S3FileViewer struct {
name, path string
ResourceViewer
}
func NewS3FileViewer(path, resource string) *S3FileViewer {
var obj S3FileViewer
obj.name = resource
obj.path = path
if obj.path == "s3://" {
obj.path = obj.path + obj.name
}
obj.ResourceViewer = NewBrowser("OBJ")
obj.AddBindKeysFn(obj.bindKeys)
//s3.GetTable().SetEnterFn(s3.describeInstace)
return &obj
}
func (obj *S3FileViewer) Name() string {
return obj.name
}
func (obj *S3FileViewer) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyShiftN: ui.NewKeyAction("Sort Name", obj.GetTable().SortColCmd("Name", true), true),
ui.KeyShiftM: ui.NewKeyAction("Sort Modification-Time", obj.GetTable().SortColCmd("Last-Modified", true), true),
ui.KeyShiftS: ui.NewKeyAction("Sort Size", obj.GetTable().SortColCmd("Size", true), true),
ui.KeyShiftC: ui.NewKeyAction("Sort Storage-Class", obj.GetTable().SortColCmd("Storage-Class", true), true),
tcell.KeyEscape: ui.NewKeyAction("Back", obj.App().PrevCmd, false),
tcell.KeyEnter: ui.NewKeyAction("View", obj.enterCmd, false),
tcell.KeyCtrlD: ui.NewKeyAction("Download Object", obj.downloadCmd, true),
tcell.KeyCtrlP: ui.NewKeyAction("Pre-Signed URL", obj.preSignedUrlCmd, true),
})
}
func (obj *S3FileViewer) enterCmd(evt *tcell.EventKey) *tcell.EventKey {
objName := obj.GetTable().GetSelectedItem()
fileType := obj.GetTable().GetSecondColumn()
if fileType == "Folder" {
o := NewS3FileViewer(obj.path+"/"+objName, objName)
ctx := obj.App().GetContext()
bn := ctx.Value(internal.BucketName)
fn := fmt.Sprintf("%v%v/", ctx.Value(internal.FolderName), objName)
log.Info().Msg(fmt.Sprintf("In view Folder Name: %v", fn))
ctx = context.WithValue(obj.App().context, internal.BucketName, bn)
obj.App().SetContext(ctx)
ctx = context.WithValue(obj.App().context, internal.FolderName, fn)
obj.App().SetContext(ctx)
obj.App().Flash().Info(fmt.Sprintf("Bucket Name: %v", bn))
obj.App().inject(o)
o.GetTable().SetTitle(o.path)
}
return evt
}
func (obj *S3FileViewer) downloadCmd(evt *tcell.EventKey) *tcell.EventKey {
objName := obj.GetTable().GetSelectedItem()
fileType := obj.GetTable().GetSecondColumn()
if fileType == "File" {
ctx := obj.App().GetContext()
op := getObjectParams(ctx, objName)
res := aws.DownloadObject(*op.sess, op.bucketName, op.key)
obj.App().Flash().Info(res)
}
return nil
}
func (obj *S3FileViewer) preSignedUrlCmd(evt *tcell.EventKey) *tcell.EventKey {
objNmae := obj.GetTable().GetSelectedItem()
fileType := obj.GetTable().GetSecondColumn()
if fileType == "File" {
ctx := obj.App().GetContext()
op := getObjectParams(ctx, objNmae)
url := aws.GetPreSignedUrl(*op.sess, op.bucketName, op.key)
log.Info().Msg(fmt.Sprintf("In view Presigned URL: %v", url))
clipboard.WriteAll(url)
obj.App().Flash().Info("Presigned URL Copied to Clipboard.")
}
return nil
}
func getObjectParams(ctx context.Context, objName string) ObjectParams {
sess, ok := ctx.Value(internal.KeySession).(*session.Session)
if !ok {
log.Err(fmt.Errorf("conversion err: Expected session.session but got %v", sess))
}
bn := fmt.Sprintf("%v", ctx.Value(internal.BucketName))
fn := fmt.Sprintf("%v", ctx.Value(internal.FolderName))
log.Info().Msg(fmt.Sprintf("In view Bucket Name: %v", bn))
log.Info().Msg(fmt.Sprintf("In view Folder Name: %v", fn))
log.Info().Msg(fmt.Sprintf("In view Object Name: %v", objName))
key := fn + objName
log.Info().Msg(fmt.Sprintf("In view key: %v", key))
return ObjectParams{
sess: sess,
bucketName: bn,
key: key,
}
}