fix format
This commit is contained in:
@@ -15,7 +15,8 @@ pub fn execute(connection: &str, name: Option<&str>) -> Result<()> {
|
|||||||
anyhow::bail!(
|
anyhow::bail!(
|
||||||
"a worker named '{}' is already registered\n\
|
"a worker named '{}' is already registered\n\
|
||||||
Use 'p default {}' to make it the default, or choose a different name with -n",
|
Use 'p default {}' to make it the default, or choose a different name with -n",
|
||||||
name, name
|
name,
|
||||||
|
name
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,28 @@ pub fn execute(check: bool) -> Result<()> {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let name_w = cfg.workers.iter().map(|w| w.name.len()).max().unwrap_or(0).max(4);
|
let name_w = cfg
|
||||||
let conn_w = cfg.workers.iter().map(|w| w.connection.len()).max().unwrap_or(0).max(10);
|
.workers
|
||||||
|
.iter()
|
||||||
|
.map(|w| w.name.len())
|
||||||
|
.max()
|
||||||
|
.unwrap_or(0)
|
||||||
|
.max(4);
|
||||||
|
let conn_w = cfg
|
||||||
|
.workers
|
||||||
|
.iter()
|
||||||
|
.map(|w| w.connection.len())
|
||||||
|
.max()
|
||||||
|
.unwrap_or(0)
|
||||||
|
.max(10);
|
||||||
|
|
||||||
if check {
|
if check {
|
||||||
println!(" {:<name_w$} {:<conn_w$} STATUS", "NAME", "CONNECTION");
|
println!(" {:<name_w$} {:<conn_w$} STATUS", "NAME", "CONNECTION");
|
||||||
println!(" {:<name_w$} {:<conn_w$} ------", "-".repeat(name_w), "-".repeat(conn_w));
|
println!(
|
||||||
|
" {:<name_w$} {:<conn_w$} ------",
|
||||||
|
"-".repeat(name_w),
|
||||||
|
"-".repeat(conn_w)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
println!(" {:<name_w$} CONNECTION", "NAME");
|
println!(" {:<name_w$} CONNECTION", "NAME");
|
||||||
println!(" {:<name_w$} ----------", "-".repeat(name_w));
|
println!(" {:<name_w$} ----------", "-".repeat(name_w));
|
||||||
@@ -26,8 +42,15 @@ pub fn execute(check: bool) -> Result<()> {
|
|||||||
let prefix = if is_default { "* " } else { " " };
|
let prefix = if is_default { "* " } else { " " };
|
||||||
|
|
||||||
if check {
|
if check {
|
||||||
let status = if ssh::is_reachable(worker) { "reachable" } else { "unreachable" };
|
let status = if ssh::is_reachable(worker) {
|
||||||
println!("{}{:<name_w$} {:<conn_w$} {}", prefix, worker.name, worker.connection, status);
|
"reachable"
|
||||||
|
} else {
|
||||||
|
"unreachable"
|
||||||
|
};
|
||||||
|
println!(
|
||||||
|
"{}{:<name_w$} {:<conn_w$} {}",
|
||||||
|
prefix, worker.name, worker.connection, status
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
println!("{}{:<name_w$} {}", prefix, worker.name, worker.connection);
|
println!("{}{:<name_w$} {}", prefix, worker.name, worker.connection);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,8 +35,7 @@ pub fn load() -> Result<Config> {
|
|||||||
}
|
}
|
||||||
let content = std::fs::read_to_string(&path)
|
let content = std::fs::read_to_string(&path)
|
||||||
.map_err(|e| anyhow::anyhow!("failed to read {}: {}", path.display(), e))?;
|
.map_err(|e| anyhow::anyhow!("failed to read {}: {}", path.display(), e))?;
|
||||||
serde_yaml::from_str(&content)
|
serde_yaml::from_str(&content).map_err(|e| anyhow::anyhow!("failed to parse config: {}", e))
|
||||||
.map_err(|e| anyhow::anyhow!("failed to parse config: {}", e))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(config: &Config) -> Result<()> {
|
pub fn save(config: &Config) -> Result<()> {
|
||||||
@@ -65,11 +64,11 @@ impl Config {
|
|||||||
Some(n) => self
|
Some(n) => self
|
||||||
.get_worker(n)
|
.get_worker(n)
|
||||||
.ok_or_else(|| anyhow::anyhow!("unknown worker '{}'", n)),
|
.ok_or_else(|| anyhow::anyhow!("unknown worker '{}'", n)),
|
||||||
None => self
|
None => self.default_worker().ok_or_else(|| {
|
||||||
.default_worker()
|
anyhow::anyhow!(
|
||||||
.ok_or_else(|| anyhow::anyhow!(
|
|
||||||
"no default worker configured — run 'p register <connection>' first"
|
"no default worker configured — run 'p register <connection>' first"
|
||||||
)),
|
)
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
p/src/db.rs
10
p/src/db.rs
@@ -37,8 +37,8 @@ pub fn load(id: &str) -> Result<Option<Job>> {
|
|||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
let content = std::fs::read_to_string(&path)
|
let content =
|
||||||
.with_context(|| format!("failed to read job {}", id))?;
|
std::fs::read_to_string(&path).with_context(|| format!("failed to read job {}", id))?;
|
||||||
serde_json::from_str(&content)
|
serde_json::from_str(&content)
|
||||||
.with_context(|| format!("failed to parse job {}", id))
|
.with_context(|| format!("failed to parse job {}", id))
|
||||||
.map(Some)
|
.map(Some)
|
||||||
@@ -91,7 +91,8 @@ pub fn list() -> Result<Vec<Job>> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
match std::fs::read_to_string(&path).and_then(|c| {
|
match std::fs::read_to_string(&path).and_then(|c| {
|
||||||
serde_json::from_str::<Job>(&c).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
|
serde_json::from_str::<Job>(&c)
|
||||||
|
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
|
||||||
}) {
|
}) {
|
||||||
Ok(job) => jobs.push(job),
|
Ok(job) => jobs.push(job),
|
||||||
Err(e) => eprintln!("warning: skipping {:?}: {}", path, e),
|
Err(e) => eprintln!("warning: skipping {:?}: {}", path, e),
|
||||||
@@ -106,8 +107,7 @@ pub fn list() -> Result<Vec<Job>> {
|
|||||||
pub fn delete(id: &str) -> Result<()> {
|
pub fn delete(id: &str) -> Result<()> {
|
||||||
let path = job_path(id)?;
|
let path = job_path(id)?;
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
std::fs::remove_file(&path)
|
std::fs::remove_file(&path).with_context(|| format!("failed to delete job {}", id))?;
|
||||||
.with_context(|| format!("failed to delete job {}", id))?;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,10 @@ fn main() -> Result<()> {
|
|||||||
match arg.as_str() {
|
match arg.as_str() {
|
||||||
"-n" | "--no-sync" => no_sync = true,
|
"-n" | "--no-sync" => no_sync = true,
|
||||||
flag if flag.starts_with('-') => {
|
flag if flag.starts_with('-') => {
|
||||||
eprintln!("error: unknown flag '{}'\nusage: p [-n] [<worker>] -- <command>", flag);
|
eprintln!(
|
||||||
|
"error: unknown flag '{}'\nusage: p [-n] [<worker>] -- <command>",
|
||||||
|
flag
|
||||||
|
);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
name => {
|
name => {
|
||||||
@@ -54,9 +57,11 @@ fn main() -> Result<()> {
|
|||||||
cli::Command::Attach { job_id } => commands::attach::execute(&job_id),
|
cli::Command::Attach { job_id } => commands::attach::execute(&job_id),
|
||||||
cli::Command::Logs { job_id, follow } => commands::logs::execute(&job_id, follow),
|
cli::Command::Logs { job_id, follow } => commands::logs::execute(&job_id, follow),
|
||||||
cli::Command::Stop { job_id } => commands::stop::execute(&job_id),
|
cli::Command::Stop { job_id } => commands::stop::execute(&job_id),
|
||||||
cli::Command::Pull { job_id, remote_path, local_dest } => {
|
cli::Command::Pull {
|
||||||
commands::pull::execute(&job_id, &remote_path, local_dest.as_deref())
|
job_id,
|
||||||
}
|
remote_path,
|
||||||
|
local_dest,
|
||||||
|
} => commands::pull::execute(&job_id, &remote_path, local_dest.as_deref()),
|
||||||
cli::Command::Rm { job_id, force } => commands::rm::execute(&job_id, force),
|
cli::Command::Rm { job_id, force } => commands::rm::execute(&job_id, force),
|
||||||
cli::Command::Register { connection, name } => {
|
cli::Command::Register { connection, name } => {
|
||||||
commands::register::execute(&connection, name.as_deref())
|
commands::register::execute(&connection, name.as_deref())
|
||||||
|
|||||||
@@ -35,8 +35,10 @@ pub fn hostname_from_connection(conn: &str) -> String {
|
|||||||
/// Check whether a worker is reachable over SSH (5 s timeout, no auth prompts).
|
/// Check whether a worker is reachable over SSH (5 s timeout, no auth prompts).
|
||||||
pub fn is_reachable(worker: &WorkerConfig) -> bool {
|
pub fn is_reachable(worker: &WorkerConfig) -> bool {
|
||||||
let mut args = vec![
|
let mut args = vec![
|
||||||
"-o".to_string(), "ConnectTimeout=5".to_string(),
|
"-o".to_string(),
|
||||||
"-o".to_string(), "BatchMode=yes".to_string(),
|
"ConnectTimeout=5".to_string(),
|
||||||
|
"-o".to_string(),
|
||||||
|
"BatchMode=yes".to_string(),
|
||||||
];
|
];
|
||||||
args.extend(ssh_args(worker));
|
args.extend(ssh_args(worker));
|
||||||
args.push("true".to_string());
|
args.push("true".to_string());
|
||||||
|
|||||||
Reference in New Issue
Block a user