fmt: format merged cli changes

This commit is contained in:
Keisuke Hirata 2026-07-27 01:43:08 +09:00
parent 0f2289c539
commit 55204478c6
No known key found for this signature in database

View File

@ -1,5 +1,5 @@
use std::net::SocketAddr;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::net::SocketAddr;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::ExitCode; use std::process::ExitCode;
use std::sync::Arc; use std::sync::Arc;
@ -11,9 +11,8 @@ use worker_runtime::auth::{RuntimeIdentityMaterial, decode_public_key};
use yoi_workspace_server::hosts::{RemoteRuntimeAuthConfig, RemoteRuntimeConfig}; use yoi_workspace_server::hosts::{RemoteRuntimeAuthConfig, RemoteRuntimeConfig};
use yoi_workspace_server::store::{RepositoryRecord, SqliteWorkspaceStore, TrustedRuntimeRecord}; use yoi_workspace_server::store::{RepositoryRecord, SqliteWorkspaceStore, TrustedRuntimeRecord};
use yoi_workspace_server::{ use yoi_workspace_server::{
BackendRuntimesConfigFile, ControlPlaneStore, ServerConfig, BackendRuntimesConfigFile, ControlPlaneStore, ServerConfig, WORKSPACE_BACKEND_CONFIG_TEMPLATE,
WORKSPACE_BACKEND_CONFIG_TEMPLATE, WorkspaceBackendConfigFile, WorkspaceIdentity, WorkspaceBackendConfigFile, WorkspaceIdentity, WorkspaceRecord, serve,
WorkspaceRecord, serve,
}; };
#[derive(Debug)] #[derive(Debug)]
@ -195,7 +194,9 @@ fn server_identity_path() -> PathBuf {
ServerConfig::default_server_data_root().join("identity.toml") ServerConfig::default_server_data_root().join("identity.toml")
} }
fn read_server_identity_file(path: &Path) -> Result<Option<ServerIdentityFile>, Box<dyn std::error::Error>> { fn read_server_identity_file(
path: &Path,
) -> Result<Option<ServerIdentityFile>, Box<dyn std::error::Error>> {
if !path.exists() { if !path.exists() {
return Ok(None); return Ok(None);
} }
@ -203,7 +204,10 @@ fn read_server_identity_file(path: &Path) -> Result<Option<ServerIdentityFile>,
Ok(Some(toml::from_str(&contents)?)) Ok(Some(toml::from_str(&contents)?))
} }
fn write_server_identity_file(path: &Path, identity: &ServerIdentityFile) -> Result<(), Box<dyn std::error::Error>> { fn write_server_identity_file(
path: &Path,
identity: &ServerIdentityFile,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(parent) = path.parent() { if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?; std::fs::create_dir_all(parent)?;
} }
@ -241,7 +245,9 @@ fn public_identity_view(identity: &RuntimeIdentityMaterial) -> PublicIdentityVie
fn run_identity_command(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { fn run_identity_command(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
let mut args = VecDeque::from(args); let mut args = VecDeque::from(args);
let subcommand = args.pop_front().ok_or_else(|| CliError("identity requires `init` or `show`".to_string()))?; let subcommand = args
.pop_front()
.ok_or_else(|| CliError("identity requires `init` or `show`".to_string()))?;
match subcommand.as_str() { match subcommand.as_str() {
"init" => { "init" => {
let mut server_id = None; let mut server_id = None;
@ -254,12 +260,15 @@ fn run_identity_command(args: Vec<String>) -> Result<(), Box<dyn std::error::Err
ensure_no_inline_value(&flag, inline_value.as_deref())?; ensure_no_inline_value(&flag, inline_value.as_deref())?;
replace = true; replace = true;
} }
_ => return Err(Box::new(CliError(format!("unknown identity init argument `{flag}`")))), _ => {
return Err(Box::new(CliError(format!(
"unknown identity init argument `{flag}`"
))));
}
} }
} }
let server_id = server_id.ok_or_else(|| { let server_id = server_id
CliError("identity init requires --server-id".to_string()) .ok_or_else(|| CliError("identity init requires --server-id".to_string()))?;
})?;
let path = server_identity_path(); let path = server_identity_path();
if read_server_identity_file(&path)?.is_some() && !replace { if read_server_identity_file(&path)?.is_some() && !replace {
return Err(Box::new(CliError(format!( return Err(Box::new(CliError(format!(
@ -268,7 +277,12 @@ fn run_identity_command(args: Vec<String>) -> Result<(), Box<dyn std::error::Err
)))); ))));
} }
let identity = RuntimeIdentityMaterial::generate(server_id)?; let identity = RuntimeIdentityMaterial::generate(server_id)?;
write_server_identity_file(&path, &ServerIdentityFile { identity: identity.clone() })?; write_server_identity_file(
&path,
&ServerIdentityFile {
identity: identity.clone(),
},
)?;
println!("server_id={}", identity.identity_id); println!("server_id={}", identity.identity_id);
println!("public_key={}", identity.public_key); println!("public_key={}", identity.public_key);
println!("identity_file={}", path.display()); println!("identity_file={}", path.display());
@ -283,12 +297,19 @@ fn run_identity_command(args: Vec<String>) -> Result<(), Box<dyn std::error::Err
ensure_no_inline_value(&flag, inline_value.as_deref())?; ensure_no_inline_value(&flag, inline_value.as_deref())?;
json = true; json = true;
} }
_ => return Err(Box::new(CliError(format!("unknown identity show argument `{flag}`")))), _ => {
return Err(Box::new(CliError(format!(
"unknown identity show argument `{flag}`"
))));
}
} }
} }
let path = server_identity_path(); let path = server_identity_path();
let identity = read_server_identity_file(&path)?.ok_or_else(|| { let identity = read_server_identity_file(&path)?.ok_or_else(|| {
CliError(format!("server identity is not initialized at {}", path.display())) CliError(format!(
"server identity is not initialized at {}",
path.display()
))
})?; })?;
let view = public_identity_view(&identity.identity); let view = public_identity_view(&identity.identity);
if json { if json {
@ -300,13 +321,17 @@ fn run_identity_command(args: Vec<String>) -> Result<(), Box<dyn std::error::Err
} }
Ok(()) Ok(())
} }
_ => Err(Box::new(CliError(format!("unknown identity subcommand `{subcommand}`")))), _ => Err(Box::new(CliError(format!(
"unknown identity subcommand `{subcommand}`"
)))),
} }
} }
fn run_trust_runtime_command(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { fn run_trust_runtime_command(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
let mut args = VecDeque::from(args); let mut args = VecDeque::from(args);
let subcommand = args.pop_front().ok_or_else(|| CliError("trust-runtime requires `add`, `list`, or `revoke`".to_string()))?; let subcommand = args
.pop_front()
.ok_or_else(|| CliError("trust-runtime requires `add`, `list`, or `revoke`".to_string()))?;
let database_path = ServerConfig::default_server_database_path(); let database_path = ServerConfig::default_server_database_path();
if let Some(parent) = database_path.parent() { if let Some(parent) = database_path.parent() {
std::fs::create_dir_all(parent)?; std::fs::create_dir_all(parent)?;
@ -322,20 +347,35 @@ fn run_trust_runtime_command(args: Vec<String>) -> Result<(), Box<dyn std::error
while let Some(arg) = args.pop_front() { while let Some(arg) = args.pop_front() {
let (flag, inline_value) = split_flag_value(arg)?; let (flag, inline_value) = split_flag_value(arg)?;
match flag.as_str() { match flag.as_str() {
"--runtime-id" => runtime_id = Some(take_value(&flag, inline_value, &mut args)?), "--runtime-id" => {
"--base-url" | "--endpoint" => base_url = Some(take_value(&flag, inline_value, &mut args)?), runtime_id = Some(take_value(&flag, inline_value, &mut args)?)
"--public-key" => public_key = Some(take_value(&flag, inline_value, &mut args)?), }
"--display-name" => display_name = Some(take_value(&flag, inline_value, &mut args)?), "--base-url" | "--endpoint" => {
base_url = Some(take_value(&flag, inline_value, &mut args)?)
}
"--public-key" => {
public_key = Some(take_value(&flag, inline_value, &mut args)?)
}
"--display-name" => {
display_name = Some(take_value(&flag, inline_value, &mut args)?)
}
"--replace" => { "--replace" => {
ensure_no_inline_value(&flag, inline_value.as_deref())?; ensure_no_inline_value(&flag, inline_value.as_deref())?;
replace = true; replace = true;
} }
_ => return Err(Box::new(CliError(format!("unknown trust-runtime add argument `{flag}`")))), _ => {
return Err(Box::new(CliError(format!(
"unknown trust-runtime add argument `{flag}`"
))));
}
} }
} }
let runtime_id = runtime_id.ok_or_else(|| CliError("trust-runtime add requires --runtime-id".to_string()))?; let runtime_id = runtime_id
let base_url = base_url.ok_or_else(|| CliError("trust-runtime add requires --base-url".to_string()))?; .ok_or_else(|| CliError("trust-runtime add requires --runtime-id".to_string()))?;
let public_key = public_key.ok_or_else(|| CliError("trust-runtime add requires --public-key".to_string()))?; let base_url = base_url
.ok_or_else(|| CliError("trust-runtime add requires --base-url".to_string()))?;
let public_key = public_key
.ok_or_else(|| CliError("trust-runtime add requires --public-key".to_string()))?;
decode_public_key(&public_key)?; decode_public_key(&public_key)?;
ensure_trusted_runtime_replace_allowed(&store, &runtime_id, replace)?; ensure_trusted_runtime_replace_allowed(&store, &runtime_id, replace)?;
let now = Utc::now().to_rfc3339(); let now = Utc::now().to_rfc3339();
@ -366,7 +406,11 @@ fn run_trust_runtime_command(args: Vec<String>) -> Result<(), Box<dyn std::error
ensure_no_inline_value(&flag, inline_value.as_deref())?; ensure_no_inline_value(&flag, inline_value.as_deref())?;
include_revoked = true; include_revoked = true;
} }
_ => return Err(Box::new(CliError(format!("unknown trust-runtime list argument `{flag}`")))), _ => {
return Err(Box::new(CliError(format!(
"unknown trust-runtime list argument `{flag}`"
))));
}
} }
} }
let records = store.list_trusted_runtimes(include_revoked)?; let records = store.list_trusted_runtimes(include_revoked)?;
@ -390,19 +434,31 @@ fn run_trust_runtime_command(args: Vec<String>) -> Result<(), Box<dyn std::error
while let Some(arg) = args.pop_front() { while let Some(arg) = args.pop_front() {
let (flag, inline_value) = split_flag_value(arg)?; let (flag, inline_value) = split_flag_value(arg)?;
match flag.as_str() { match flag.as_str() {
"--runtime-id" => runtime_id = Some(take_value(&flag, inline_value, &mut args)?), "--runtime-id" => {
_ => return Err(Box::new(CliError(format!("unknown trust-runtime revoke argument `{flag}`")))), runtime_id = Some(take_value(&flag, inline_value, &mut args)?)
}
_ => {
return Err(Box::new(CliError(format!(
"unknown trust-runtime revoke argument `{flag}`"
))));
}
} }
} }
let runtime_id = runtime_id.ok_or_else(|| CliError("trust-runtime revoke requires --runtime-id".to_string()))?; let runtime_id = runtime_id.ok_or_else(|| {
CliError("trust-runtime revoke requires --runtime-id".to_string())
})?;
let now = Utc::now().to_rfc3339(); let now = Utc::now().to_rfc3339();
if !store.revoke_trusted_runtime(&runtime_id, &now)? { if !store.revoke_trusted_runtime(&runtime_id, &now)? {
return Err(Box::new(CliError(format!("trusted runtime `{runtime_id}` is not registered or is already revoked")))); return Err(Box::new(CliError(format!(
"trusted runtime `{runtime_id}` is not registered or is already revoked"
))));
} }
println!("revoked_runtime_id={runtime_id}"); println!("revoked_runtime_id={runtime_id}");
Ok(()) Ok(())
} }
_ => Err(Box::new(CliError(format!("unknown trust-runtime subcommand `{subcommand}`")))), _ => Err(Box::new(CliError(format!(
"unknown trust-runtime subcommand `{subcommand}`"
)))),
} }
} }