-
Notifications
You must be signed in to change notification settings - Fork 26
/
table.go
158 lines (137 loc) · 3.72 KB
/
table.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package view
import (
"context"
"encoding/csv"
"fmt"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/atotto/clipboard"
"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloudlens/internal/ui"
"github.com/rs/zerolog/log"
)
const (
DefaultRefreshRate = time.Second * 20
)
// Table represents a table viewer.
type Table struct {
*ui.Table
app *App
enterFn EnterFunc
bindKeysFn []BindKeysFunc
}
// NewTable returns a new viewer.
func NewTable(res string) *Table {
t := Table{
Table: ui.NewTable(res),
}
return &t
}
// Init initializes the component.
func (t *Table) Init(ctx context.Context) (err error) {
if t.app, err = extractApp(ctx); err != nil {
return err
}
t.Table.Init(ctx)
t.SetInputCapture(t.keyboard)
t.bindKeys()
t.GetModel().SetRefreshRate(DefaultRefreshRate)
return nil
}
// App returns the current app handle.
func (t *Table) App() *App {
return t.app
}
// Start runs the component.
func (t *Table) Start() {
}
// Stop terminates the component.
func (t *Table) Stop() {
}
// SetEnterFn specifies the default enter behavior.
func (t *Table) SetEnterFn(f EnterFunc) {
t.enterFn = f
}
func (t *Table) keyboard(evt *tcell.EventKey) *tcell.EventKey {
key := evt.Key()
if key == tcell.KeyUp || key == tcell.KeyDown {
return evt
}
if a, ok := t.Actions()[ui.AsKey(evt)]; ok {
return a.Action(evt)
}
return evt
}
func (t *Table) bindKeys() {
t.Actions().Add(ui.KeyActions{
tcell.KeyCtrlW: ui.NewKeyAction("Toggle Wide", t.toggleWideCmd, false),
ui.KeyHelp: ui.NewKeyAction("Help", t.App().helpCmd, true),
ui.KeyZ: ui.NewKeyAction("CSV", t.importAsCSV, true),
})
}
// Name returns the table name.
func (t *Table) Name() string { return t.Table.Resource() }
// AddBindKeysFn adds additional key bindings.
func (t *Table) AddBindKeysFn(f BindKeysFunc) {
t.bindKeysFn = append(t.bindKeysFn, f)
}
func (t *Table) toggleWideCmd(evt *tcell.EventKey) *tcell.EventKey {
t.ToggleWide()
return nil
}
func (t *Table) importAsCSV(evt *tcell.EventKey) *tcell.EventKey {
_, profile := t.app.profile().GetCurrentOption()
_, region := t.app.region().GetCurrentOption()
var tableData [][]string
rowCount := t.GetRowCount()
colCount := t.GetColumnCount()
for i := 0; i < rowCount; i++ {
var row []string
for j := 0; j < colCount; j++ {
text := t.GetCell(i, j).Text
text = decolorize(text)
row = append(row, text)
}
tableData = append(tableData, row)
}
csvFileName := strings.Split(t.GetTitle(), " ")
usr, err := user.Current()
if err != nil {
log.Info().Msg(fmt.Sprintf("error in getting the machine's user: %v", err))
}
path := usr.HomeDir + "/cloudlens/csv/"
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Info().Msg(fmt.Sprintf("error in creating csv directory: %v", err))
}
if csvFileName[len(csvFileName)-2] == "S3" || strings.Contains(csvFileName[len(csvFileName)-2], "IAM") {
path = filepath.Join(path + "/" + csvFileName[len(csvFileName)-2] + "-" + profile + ".csv")
} else {
path = filepath.Join(path + "/" + csvFileName[len(csvFileName)-2] + "-" + profile + "-" + region + ".csv")
}
file, err := os.Create(path)
if err != nil {
log.Info().Msg(fmt.Sprintf("error in creating csv file: %v", err))
}
writer := csv.NewWriter(file)
for _, record := range tableData {
err := writer.Write(record)
if err != nil {
log.Info().Msg(fmt.Sprintf("error in writing records to csv file: %v", err))
}
}
writer.Flush()
t.app.Flash().Info("CSV file created and CSV file path copied to clipboard.")
clipboard.WriteAll(path)
return nil
}
func decolorize(input string) string {
input = strings.Replace(input, "↑", "", 1)
input = strings.Replace(input, "↓", "", 1)
re := regexp.MustCompile(`\[.*?\]`)
return re.ReplaceAllString(input, "")
}