Skip to content

Commit d554a70

Browse files
🔀 Merge pull request #14 from AndreLeclercq/12-modularize-connection-commands
🎨 Modularize all connection commands
2 parents ce5c0d2 + c5209b6 commit d554a70

8 files changed

Lines changed: 241 additions & 197 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/commands/connection.rs‎

Lines changed: 0 additions & 196 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use anyhow::{Context, Result};
2+
use super::ConnectionError;
3+
use crate::config::{
4+
connection_exists,
5+
upsert_connection,
6+
Connection,
7+
};
8+
use dialoguer::Input;
9+
use std::{collections::HashMap, net::Ipv4Addr, str::FromStr};
10+
11+
pub fn add(name: &str) -> Result<()> {
12+
let name_lower: String = name.to_lowercase();
13+
14+
if connection_exists(name_lower.to_string()).context("Error when check if connections name is already used")? {
15+
return Err(ConnectionError::NameAlreadyExist(name_lower.to_string()).into());
16+
}
17+
eprintln!("Create new connection :");
18+
19+
let host: Ipv4Addr = loop {
20+
let host_input: String = Input::new()
21+
.with_prompt("Host IP (required)")
22+
.interact_text()
23+
.unwrap();
24+
25+
match Ipv4Addr::from_str(&host_input) {
26+
Ok(host) => break host,
27+
Err(_) => eprintln!("Invalid IP, Try again."),
28+
}
29+
};
30+
31+
let port: u16 = loop {
32+
let port_input: String = Input::new()
33+
.allow_empty(true)
34+
.with_prompt("Port (default: 22)")
35+
.interact_text()
36+
.unwrap();
37+
38+
if port_input.trim().is_empty() {
39+
break 22;
40+
}
41+
42+
match port_input.trim().parse::<u16>() {
43+
Ok(port) if port != 0 => break port,
44+
Ok(_) => break 22,
45+
Err(_) => eprintln!("Invalid Port, Try again."),
46+
}
47+
};
48+
49+
let user: String = Input::new()
50+
.with_prompt("User (required)")
51+
.interact_text()
52+
.unwrap();
53+
54+
let description: String = Input::new()
55+
.allow_empty(true)
56+
.with_prompt("Description (optionnal)")
57+
.interact_text()
58+
.unwrap();
59+
60+
let mut connection = HashMap::new();
61+
connection.insert(
62+
name_lower.to_string(),
63+
Connection {
64+
host: host,
65+
port: port,
66+
user: user,
67+
description: Some(description),
68+
}
69+
);
70+
71+
upsert_connection(connection).context("Error when add connection")?;
72+
eprintln!("New connection '{}' add with success !", name_lower);
73+
Ok(())
74+
}
75+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use anyhow::{Context, Result};
2+
use super::ConnectionError;
3+
use crate::config::{
4+
connection_exists,
5+
get_connection,
6+
upsert_connection,
7+
Connection
8+
};
9+
use dialoguer::Input;
10+
use std::{collections::HashMap, net::Ipv4Addr, str::FromStr};
11+
12+
13+
pub fn edit(name: &str) -> Result<()> {
14+
let name_lower: String = name.to_lowercase();
15+
16+
if !connection_exists(name_lower.to_string()).context("Error when check if connections name is already used")? {
17+
return Err(ConnectionError::NameNotFound(name_lower.to_string()).into());
18+
}
19+
20+
let existing_connection = get_connection(name_lower.to_string())?;
21+
if let Some(connection) = existing_connection.get(&name_lower) {
22+
23+
let host: Ipv4Addr = loop {
24+
let host_input: String = Input::new()
25+
.with_prompt("Host IP (required)")
26+
.with_initial_text(connection.host.to_string())
27+
.interact_text()
28+
.unwrap();
29+
30+
match Ipv4Addr::from_str(&host_input) {
31+
Ok(host) => break host,
32+
Err(_) => eprintln!("Invalid IP, Try again."),
33+
}
34+
};
35+
36+
let port: u16 = loop {
37+
let port_input: String = Input::new()
38+
.allow_empty(true)
39+
.with_prompt("Port (default: 22)")
40+
.with_initial_text(connection.port.to_string())
41+
.interact_text()
42+
.unwrap();
43+
44+
if port_input.trim().is_empty() {
45+
break 22;
46+
}
47+
48+
match port_input.trim().parse::<u16>() {
49+
Ok(port) if port != 0 => break port,
50+
Ok(_) => break 22,
51+
Err(_) => eprintln!("Invalid Port, Try again."),
52+
}
53+
};
54+
55+
let user: String = Input::new()
56+
.with_prompt("User (required)")
57+
.with_initial_text(connection.user.to_string())
58+
.interact_text()
59+
.unwrap();
60+
61+
let description: String = Input::new()
62+
.allow_empty(true)
63+
.with_prompt("Description (optionnal)")
64+
.with_initial_text(connection.description.as_deref().unwrap_or(""))
65+
.interact_text()
66+
.unwrap();
67+
68+
let mut connection = HashMap::new();
69+
connection.insert(
70+
name_lower.to_string(),
71+
Connection {
72+
host: host,
73+
port: port,
74+
user: user,
75+
description: Some(description),
76+
}
77+
);
78+
79+
upsert_connection(connection)?;
80+
}
81+
82+
eprintln!("Connection '{}' was edited with success !", name_lower.to_string());
83+
Ok(())
84+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use anyhow::{Context, Result};
2+
use crate::config::{
3+
get_connection_file_content,
4+
Config,
5+
};
6+
7+
pub fn list() -> Result<()> {
8+
let config_file: Config = get_connection_file_content().context("Error when getting connection file content.")?;
9+
if let Some(connections) = config_file.connection {
10+
eprintln!("List of connections:");
11+
for name in connections.keys() {
12+
eprintln!("{}", name);
13+
}
14+
}
15+
Ok(())
16+
}

0 commit comments

Comments
 (0)