-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexisting_connection.go
95 lines (73 loc) · 1.96 KB
/
existing_connection.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
package main
import (
"log"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)
type ExistingConnectionsModel struct {
list list.Model
connections []Connection
selectedConnection *Connection
back bool
}
func NewExistingConnectionsModel() ExistingConnectionsModel {
existingConnectionsModel := ExistingConnectionsModel{}
connections, err := ListConnections()
if err != nil {
return existingConnectionsModel
}
items := []list.Item{}
for _, conn := range connections {
items = append(items, item(conn.Name))
}
l := list.New(items, itemDelegate{}, defaultWidth, listHeight)
l.Title = "Choose a connection"
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
l.Styles.HelpStyle = helpStyle
existingConnectionsModel.list = l
existingConnectionsModel.connections = connections
return existingConnectionsModel
}
func (m ExistingConnectionsModel) Init() tea.Cmd {
return nil
}
func (m ExistingConnectionsModel) Update(msg tea.Msg) (ExistingConnectionsModel, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.list.SetWidth(msg.Width)
return m, nil
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "q", "ctrl+c":
m.back = true
return m, nil
case "enter":
i, ok := m.list.SelectedItem().(item)
if ok {
choice := string(i)
for _, v := range m.connections {
if v.Name == choice {
m.selectedConnection = &v
user, pass, err := GetConnectionFromKeyring(v.Name)
if err != nil {
log.Fatal("Could not get user and password for connection from keyring: ", err)
}
m.selectedConnection.User = user
m.selectedConnection.Pass = pass
break
}
}
}
return m, nil
}
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m ExistingConnectionsModel) View() string {
return m.list.View()
}