fix format
This commit is contained in:
@@ -15,7 +15,8 @@ pub fn execute(connection: &str, name: Option<&str>) -> Result<()> {
|
||||
anyhow::bail!(
|
||||
"a worker named '{}' is already registered\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(());
|
||||
}
|
||||
|
||||
let name_w = cfg.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);
|
||||
let name_w = cfg
|
||||
.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 {
|
||||
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 {
|
||||
println!(" {:<name_w$} CONNECTION", "NAME");
|
||||
println!(" {:<name_w$} ----------", "-".repeat(name_w));
|
||||
@@ -26,8 +42,15 @@ pub fn execute(check: bool) -> Result<()> {
|
||||
let prefix = if is_default { "* " } else { " " };
|
||||
|
||||
if check {
|
||||
let status = if ssh::is_reachable(worker) { "reachable" } else { "unreachable" };
|
||||
println!("{}{:<name_w$} {:<conn_w$} {}", prefix, worker.name, worker.connection, status);
|
||||
let status = if ssh::is_reachable(worker) {
|
||||
"reachable"
|
||||
} else {
|
||||
"unreachable"
|
||||
};
|
||||
println!(
|
||||
"{}{:<name_w$} {:<conn_w$} {}",
|
||||
prefix, worker.name, worker.connection, status
|
||||
);
|
||||
} else {
|
||||
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)
|
||||
.map_err(|e| anyhow::anyhow!("failed to read {}: {}", path.display(), e))?;
|
||||
serde_yaml::from_str(&content)
|
||||
.map_err(|e| anyhow::anyhow!("failed to parse config: {}", e))
|
||||
serde_yaml::from_str(&content).map_err(|e| anyhow::anyhow!("failed to parse config: {}", e))
|
||||
}
|
||||
|
||||
pub fn save(config: &Config) -> Result<()> {
|
||||
@@ -65,11 +64,11 @@ impl Config {
|
||||
Some(n) => self
|
||||
.get_worker(n)
|
||||
.ok_or_else(|| anyhow::anyhow!("unknown worker '{}'", n)),
|
||||
None => self
|
||||
.default_worker()
|
||||
.ok_or_else(|| anyhow::anyhow!(
|
||||
None => self.default_worker().ok_or_else(|| {
|
||||
anyhow::anyhow!(
|
||||
"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() {
|
||||
return Ok(None);
|
||||
}
|
||||
let content = std::fs::read_to_string(&path)
|
||||
.with_context(|| format!("failed to read job {}", id))?;
|
||||
let content =
|
||||
std::fs::read_to_string(&path).with_context(|| format!("failed to read job {}", id))?;
|
||||
serde_json::from_str(&content)
|
||||
.with_context(|| format!("failed to parse job {}", id))
|
||||
.map(Some)
|
||||
@@ -91,7 +91,8 @@ pub fn list() -> Result<Vec<Job>> {
|
||||
continue;
|
||||
}
|
||||
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),
|
||||
Err(e) => eprintln!("warning: skipping {:?}: {}", path, e),
|
||||
@@ -106,8 +107,7 @@ pub fn list() -> Result<Vec<Job>> {
|
||||
pub fn delete(id: &str) -> Result<()> {
|
||||
let path = job_path(id)?;
|
||||
if path.exists() {
|
||||
std::fs::remove_file(&path)
|
||||
.with_context(|| format!("failed to delete job {}", id))?;
|
||||
std::fs::remove_file(&path).with_context(|| format!("failed to delete job {}", id))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -31,7 +31,10 @@ fn main() -> Result<()> {
|
||||
match arg.as_str() {
|
||||
"-n" | "--no-sync" => no_sync = true,
|
||||
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);
|
||||
}
|
||||
name => {
|
||||
@@ -54,9 +57,11 @@ fn main() -> Result<()> {
|
||||
cli::Command::Attach { job_id } => commands::attach::execute(&job_id),
|
||||
cli::Command::Logs { job_id, follow } => commands::logs::execute(&job_id, follow),
|
||||
cli::Command::Stop { job_id } => commands::stop::execute(&job_id),
|
||||
cli::Command::Pull { job_id, remote_path, local_dest } => {
|
||||
commands::pull::execute(&job_id, &remote_path, local_dest.as_deref())
|
||||
}
|
||||
cli::Command::Pull {
|
||||
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::Register { connection, name } => {
|
||||
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).
|
||||
pub fn is_reachable(worker: &WorkerConfig) -> bool {
|
||||
let mut args = vec![
|
||||
"-o".to_string(), "ConnectTimeout=5".to_string(),
|
||||
"-o".to_string(), "BatchMode=yes".to_string(),
|
||||
"-o".to_string(),
|
||||
"ConnectTimeout=5".to_string(),
|
||||
"-o".to_string(),
|
||||
"BatchMode=yes".to_string(),
|
||||
];
|
||||
args.extend(ssh_args(worker));
|
||||
args.push("true".to_string());
|
||||
|
||||
Reference in New Issue
Block a user