-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.go
More file actions
47 lines (37 loc) · 968 Bytes
/
list.go
File metadata and controls
47 lines (37 loc) · 968 Bytes
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
package list
import (
"errors"
"github.com/charmbracelet/huh"
"github.com/misha-ssh/kernel/pkg/connect"
)
var (
errNotFoundConnections = errors.New("not found connections")
)
// Run Get selected connection from list
func Run(connections *connect.Connections) (*connect.Connect, error) {
var aliases []string
var selectedAlias string
if len(connections.Connects) == 0 {
return nil, errNotFoundConnections
}
aliasToConn := make(map[string]*connect.Connect)
for _, conn := range connections.Connects {
aliases = append(aliases, conn.Alias)
aliasToConn[conn.Alias] = &conn
}
err := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title("Select Connection").
Options(huh.NewOptions(aliases...)...).
Value(&selectedAlias),
)).WithShowHelp(true).Run()
if err != nil {
return nil, err
}
selectedConn, exists := aliasToConn[selectedAlias]
if !exists {
return nil, errNotFoundConnections
}
return selectedConn, nil
}