-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
214 lines (175 loc) · 4.62 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"github.com/manifoldco/promptui"
)
// Connect to SSH
func connectSSH(conn map[string]string) {
fmt.Println("Connecting to SSH...")
// whereis sshpass
sshpass, err := exec.LookPath("sshpass")
if err != nil {
fmt.Println("sshpass not found. Please install sshpass.")
return
}
if err := syscall.Exec(sshpass, []string{"sshpass", "-p", conn["password"], "ssh", conn["username"] + "@" + conn["host"]}, os.Environ()); err != nil {
fmt.Println(err)
}
}
// Prompt user to add SSH connection
func addSSHConnection(defaultFilePath, keyFilePath string) error {
sshConnection, err := addSSHConnectionPrompt()
if err != nil {
return err
}
key, err := loadKey(keyFilePath)
if err != nil {
return err
}
fileContents, _ := readFile(defaultFilePath, key)
/**
* If fileContents is not empty, append the new SSH connection details
* to the existing file contents. Otherwise, set the new SSH connection
* details as the file contents.
*/
if fileContents != "" {
fileContents = fileContents + "\n" + sshConnection
} else {
fileContents = sshConnection
}
err = storeFile(fileContents, defaultFilePath, key)
if err != nil {
return err
}
fmt.Println("SSH connection details added successfully!")
return nil
}
func findKeyOfSelectedSSHOption(choice string, conns map[string]map[string]string) (error, string) {
index := strings.Split(choice, ".")[0]
for key, value := range conns {
if value["index"] == index {
return nil, key
}
}
return errors.New("The selected options not in connections"), ""
}
// Show SSH connections and prompt for action
func showConnections(defaultFilePath, keyFilePath string) {
connections, err := readAllConnections(defaultFilePath, keyFilePath)
if err != nil {
fmt.Println("No SSH connections found.")
return
}
items := connToStrSlice(connections)
prompt := promptui.Select{
Label: "Select an SSH connection",
Items: items,
}
_, result, err := prompt.Run()
if err != nil {
fmt.Println("Invalid connection selection. Please try again.")
return
}
switch result {
case "Back to main menu":
return
default:
err, index := findKeyOfSelectedSSHOption(result, connections)
if err != nil {
return
}
conn := connections[index]
connectSSH(conn)
}
}
// Remove SSH connection
func removeSSHConnection(defaultFilePath, keyFilePath string) {
connections, err := readAllConnections(defaultFilePath, keyFilePath)
if err != nil {
fmt.Println("No SSH connections found.")
return
}
items := connToStrSlice(connections)
prompt := promptui.Select{
Label: "Select an SSH connection to remove",
Items: items,
}
_, result, err := prompt.Run()
if err != nil {
fmt.Println("Invalid connection selection. Please try again.")
return
}
switch result {
case "Back to main menu":
return
default:
err, index := findKeyOfSelectedSSHOption(result, connections)
if err != nil {
return
}
delete(connections, index)
var newConns []string
for _, value := range connections {
newConns = append(newConns, value["username"]+"@"+value["host"]+"\t"+value["password"]+"\t"+value["description"])
}
key, err := loadKey(keyFilePath)
if err != nil {
fmt.Println("Failed to remove SSH connection.")
return
}
err = storeFile(strings.Join(newConns, "\n"), defaultFilePath, key)
if err != nil {
fmt.Println("Failed to remove SSH connection.")
return
}
fmt.Println("SSH connection removed successfully.")
}
}
// Main menu options
func main() {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Could not determine home directory: %v", err)
}
defaultFilePath := filepath.Join(homeDir, ".sshmanager", "conn")
keyFilePath := filepath.Join(homeDir, ".sshmanager", "secret.key")
cleanFlag := flag.Bool("clean", false, "Resets the connections and key file")
flag.Parse()
if *cleanFlag {
os.Remove(defaultFilePath)
os.Remove(keyFilePath)
fmt.Println("Connections and key file have been reset.")
return
}
menuOptions := []string{"Exit", "Connect to SSH", "Add SSH Connection", "Remove SSH Connection"}
for {
prompt := promptui.Select{
Label: "Menu Options | v0.1.2",
Items: menuOptions,
}
_, choice, err := prompt.Run()
if err != nil {
fmt.Println("Invalid option selected. Please try again.")
// print err
continue
}
switch choice {
case "Exit":
return
case "Connect to SSH":
showConnections(defaultFilePath, keyFilePath)
case "Add SSH Connection":
addSSHConnection(defaultFilePath, keyFilePath)
case "Remove SSH Connection":
removeSSHConnection(defaultFilePath, keyFilePath)
}
}
}