-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
173 lines (138 loc) · 5.87 KB
/
Program.cs
File metadata and controls
173 lines (138 loc) · 5.87 KB
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
using System.CommandLine;
using System.Diagnostics;
using sshx.Connections;
using sshx.Connections.Json;
using Console = System.Console;
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var connectionsOptions = new JsonConnectionRepositoryOptions($"{appData}/sshx/connections/");
var nameArgument = new Argument<string>("name") {
Description = "The connection name."
};
var userArgument = new Argument<string>("user") {
Description = "The SSH username used for authentication."
};
var hostArgument = new Argument<string>("host") {
Description = "The SSH host or IP address."
};
var nameOption = new Option<string>("name", "-n", "--name") {
Description = "New name for the connection (used for updates)."
};
var userOption = new Option<string>("user", "-u", "--user") {
Description = "The SSH username."
};
var hostOption = new Option<string>("host", "-a", "--host") {
Description = "The SSH host or IP address."
};
var portOption = new Option<int>("port", "-p", "--port") {
Description = "The SSH port. Default is 22."
};
var descriptionOption = new Option<string>("description", "-d", "--description") {
Description = "The description."
};
var createCommand = new Command("create", "Create a new SSH connection.") {
nameArgument, userArgument, hostArgument, portOption, descriptionOption
};
var updateCommand = new Command("update", "Update an existing SSH connection.") {
nameArgument, nameOption, userOption, hostOption, portOption, descriptionOption
};
var deleteCommand = new Command("delete", "Delete an SSH connection.") {
nameArgument
};
var connectCommand = new Command("connect", "Start an SSH session using the stored connection.") {
nameArgument
};
var showCommand = new Command("show", "Display details of a stored SSH connection.") {
nameArgument
};
var listCommand = new Command("list", "List all stored SSH connections.");
var aliasCommand = new Command("alias", "Add sshx alias to your shell RC file.");
connectCommand.SetAction(async result => {
var name = result.GetRequiredValue(nameArgument) ?? throw new Exception("Connection name is required.");
var repo = new JsonConnectionRepository(connectionsOptions);
var connection = await repo.GetConnectionAsync(name);
var ssh = new Process {
StartInfo = {
UseShellExecute = false,
FileName = "/usr/bin/ssh",
Arguments = $"{connection.User}@{connection.Host} -p {connection.Port}"
}
};
ssh.Start();
ssh.WaitForExit();
});
createCommand.SetAction(async result => {
var repo = new JsonConnectionRepository(connectionsOptions);
await repo.CreateConnectionAsync(
result.GetValue(nameArgument) ?? throw new Exception("Connection name is required."),
new CreateConnectionRequest(
result.GetResult(descriptionOption)?.GetValue(descriptionOption) ?? string.Empty,
result.GetRequiredValue(userArgument),
result.GetRequiredValue(hostArgument),
result.GetResult(portOption)?.GetValue(portOption) ?? 22
)
);
});
updateCommand.SetAction(async result => {
var repo = new JsonConnectionRepository(connectionsOptions);
await repo.UpdateConnectionAsync(
result.GetValue(nameArgument) ?? throw new Exception("Connection name is required."),
new UpdateConnectionRequest(
result.GetResult(nameOption)?.GetValue(nameOption),
result.GetResult(descriptionOption)?.GetValue(descriptionOption),
result.GetResult(userOption)?.GetValue(userOption),
result.GetResult(hostOption)?.GetValue(hostOption),
result.GetResult(portOption)?.GetValue(portOption)
)
);
});
deleteCommand.SetAction(async result => {
var repo = new JsonConnectionRepository(connectionsOptions);
await repo.DeleteConnectionAsync(result.GetValue(nameArgument) ??
throw new Exception("Connection name is required."));
});
showCommand.SetAction(async result => {
var repo = new JsonConnectionRepository(connectionsOptions);
var connection =
await repo.GetConnectionAsync(result.GetValue(nameArgument) ??
throw new Exception("Connection name is required."));
Console.WriteLine(connection);
});
listCommand.SetAction(async _ => {
var repo = new JsonConnectionRepository(connectionsOptions);
var connections = await repo.GetConnectionsAsync();
foreach (var (connection, index) in connections.Select((c, i) => (c, i))) {
Console.WriteLine($"[{index}] {connection.Name} | {connection.User}@{connection.Host}:{connection.Port}");
}
});
aliasCommand.SetAction(async _ => {
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var bashRc = Path.Combine(home, ".bashrc");
var zshRc = Path.Combine(home, ".zshrc");
var shellRcPaths = new List<string>();
if (File.Exists(zshRc)) {
shellRcPaths.Add(zshRc);
}
if (File.Exists(bashRc)) {
shellRcPaths.Add(bashRc);
}
if (shellRcPaths.Count < 1) {
Console.WriteLine("Could not determine shell RC file.");
return;
}
foreach (var shellRcPath in shellRcPaths) {
var aliasLine = $"alias sshx='{Path.GetFullPath(Environment.ProcessPath ?? throw new Exception("process path is null"))}'";
await File.AppendAllTextAsync(shellRcPath, $"{aliasLine}{Environment.NewLine}");
Console.WriteLine($"Alias added to {shellRcPath}. Reload your shell or run 'source {shellRcPath}' to apply.");
}
});
var rootCommand = new RootCommand(
"sshx is a modern SSH connection manager that lets you organize, create, and connect to servers effortlessly") {
connectCommand,
createCommand,
updateCommand,
deleteCommand,
showCommand,
listCommand,
aliasCommand,
};
return await rootCommand.Parse(args).InvokeAsync();