-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
157 lines (133 loc) · 3.29 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
)
type model struct {
choices []connection
selected int
quit bool
}
type connection struct {
Username string `json:"username"`
Hostname string `json:"hostname"`
Comment string `json:"comment"`
Args []string `json:"args"`
Command string `json:"command"`
}
func initialModel() model {
file, err := os.Open("config.json")
if err != nil {
fmt.Println("Error opening file:", err)
os.Exit(1)
}
defer file.Close()
var choices []connection
decoder := json.NewDecoder(file)
if err := decoder.Decode(&choices); err != nil {
fmt.Println("Error decoding config:", err)
os.Exit(1)
}
return model{
choices: choices,
selected: 0,
quit: false,
}
}
func GetLoginTarget(conn connection) string {
return fmt.Sprintf("%s@%s", conn.Username, conn.Hostname)
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q":
m.quit = true
return m, tea.Quit
case "up":
m.selected = (m.selected - 1 + len(m.choices)) % len(m.choices)
case "down":
m.selected = (m.selected + 1) % len(m.choices)
case "enter":
return m, tea.Quit
}
for i := 0; i < len(m.choices) && i < 10; i++ {
if msg.String() == fmt.Sprintf("%d", i) {
m.selected = i
}
}
}
return m, nil
}
func (m model) View() string {
baseStyle := lipgloss.NewStyle().Padding(0, 1)
selectedStyle := baseStyle.Foreground(lipgloss.Color("10")).Bold(true)
targetStyle := baseStyle.Foreground(lipgloss.Color("205"))
commandStyle := baseStyle.Foreground(lipgloss.Color("240"))
commentStyle := baseStyle
t := table.New().
StyleFunc(func(row, col int) lipgloss.Style {
return lipgloss.NewStyle().Padding(0, 0)
}).Border(lipgloss.HiddenBorder())
for i, choice := range m.choices {
target := GetLoginTarget(choice)
number := fmt.Sprintf("%d.", i)
var indicatorCell, targetCell string
if i == m.selected {
indicatorCell = selectedStyle.Render("> " + number)
targetCell = selectedStyle.Render(target)
} else {
indicatorCell = targetStyle.Render(" " + number)
targetCell = targetStyle.Render(target)
}
commandCell := commandStyle.Render(choice.Command)
commentCell := commentStyle.Render(choice.Comment)
t.Row(
indicatorCell,
targetCell,
commandCell,
commentCell,
)
}
var b strings.Builder
b.WriteString(t.Render())
b.WriteString("\n")
b.WriteString("Press 'enter' to select, 'q' to quit.")
return b.String()
}
func main() {
final, err := tea.NewProgram(initialModel()).Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
os.Exit(1)
}
if m, ok := final.(model); ok {
if m.quit {
os.Exit(0)
}
selected := m.choices[m.selected]
fmt.Printf("Connecting...\n")
target := GetLoginTarget(selected)
selected.Args = append(selected.Args, target, selected.Command)
cmd := exec.Command("ssh", selected.Args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
fmt.Println("Error:", err)
}
} else {
fmt.Println("Error: Unexpected model type")
os.Exit(1)
}
}