Reject --offline where it has no effect

This commit is contained in:
Archipelago 2026-11-18 13:38:47 +00:00
parent f96f3a23d4
commit dacbfed4d7

View file

@ -30,7 +30,7 @@ fn map(fields: Dictionary) -> Result<Value,String> {
.collect::<Result<serde_json::Map<_,_>,_>>().map(Value::Object)
}
struct Client { connection: Connection, interactive: bool, offline: bool, request_id: String, output: RefCell<super::client_output::Output> }
struct Client { connection: Connection, interactive: bool, offline: bool, offline_explicit: bool, request_id: String, output: RefCell<super::client_output::Output> }
impl Client {
fn proxy(&self, packages: bool) -> Result<Proxy<'_>,String> {
Proxy::new(&self.connection,BUS_NAME,if packages {"/org/archipelago/System1/HostExtensions"} else {MANAGER_PATH},
@ -112,13 +112,22 @@ impl Client {
let mut cursor = String::new();
let mut lines = Vec::new();
loop {
let page = self.dispatch(&["transaction", "log", id, &cursor])?;
let page = self.log_page(id, &cursor)?;
lines.extend(page["lines"].as_array().ok_or("Invalid log page")?.iter().cloned());
cursor = page["next_cursor"].as_str().unwrap_or("").into();
if cursor.is_empty() || lines.len() >= 256 { break; }
}
Ok(json!({"operation_id":id,"lines":lines}))
}
fn log_page(&self, id: &str, cursor: &str) -> Result<Value, String> {
let path: OwnedObjectPath = self.proxy(false)?.call("GetTransaction", &(id,)).map_err(error)?;
let proxy = Proxy::new(&self.connection, BUS_NAME, path.as_str(), "org.archipelago.System1.Transaction").map_err(error)?;
let options = Dictionary::from([("interactive".into(), self.interactive.into())]);
let flags = if self.interactive { MethodFlags::AllowInteractiveAuth.into() } else { Default::default() };
let (lines, next): (Vec<String>, String) = proxy.call_with_flags("GetLog", flags, &(cursor, 100u32, options))
.map_err(error)?.ok_or("Missing log reply")?;
Ok(json!({"lines":lines,"next_cursor":next}))
}
fn query(&self, packages: bool, method: &str) -> Result<Value,String> {
map(self.proxy(packages)?.call(method,&()).map_err(error)?)
}
@ -187,6 +196,13 @@ impl Client {
}
}
fn dispatch(&self, args: &[&str]) -> Result<Value,String> {
if self.offline_explicit && !offline_applies(args) {
return Err(if super::client_output::russian() {
format!("Опция --offline не применяется к команде «{}»", args.first().copied().unwrap_or(""))
} else {
format!("Option --offline does not apply to command \"{}\"", args.first().copied().unwrap_or(""))
});
}
match args {
["kargs"] => self.query(false, "GetKernelArguments"),
["kargs", edits @ ..] => {
@ -230,12 +246,7 @@ impl Client {
let path: OwnedObjectPath = self.proxy(false)?.call("GetTransaction", &(id,)).map_err(error)?;
let proxy = Proxy::new(&self.connection, BUS_NAME, path.as_str(), "org.archipelago.System1.Transaction").map_err(error)?;
if args[1] == "status" { return map(proxy.call("GetSnapshot", &()).map_err(error)?); }
let cursor = args.get(3).copied().unwrap_or("");
let options = Dictionary::from([("interactive".into(), self.interactive.into())]);
let flags = if self.interactive { MethodFlags::AllowInteractiveAuth.into() } else { Default::default() };
let (lines, next): (Vec<String>, String) = proxy.call_with_flags("GetLog", flags, &(cursor, 100u32, options))
.map_err(error)?.ok_or("Missing log reply")?;
Ok(json!({"lines":lines,"next_cursor":next}))
self.log_page(id, args.get(3).copied().unwrap_or(""))
}
["transaction","wait",id] => self.wait(id),
["transaction","cancel",id] => {
@ -453,13 +464,14 @@ pub fn dispatch(args: &[String]) -> Result<(),String> {
let output = RefCell::new(super::client_output::Output::new(json, debug)?);
let mut interactive = std::io::stdin().is_terminal() && !json;
let mut offline = false;
let mut offline_explicit = false;
let mut request = fs::read_to_string("/proc/sys/kernel/random/uuid").map_err(error)?.trim().to_owned();
let mut positional = Vec::new();
let mut iter = args.iter();
while let Some(arg) = iter.next() {
match arg.as_str() {
"--interactive" => interactive = true,
"--offline" => offline = true,
"--offline" => { offline = true; offline_explicit = true; },
"--json" | "--debug" => (),
"--non-interactive" => interactive = false,
"--request-id" => request = iter.next().ok_or("Missing request ID")?.clone(),
@ -472,7 +484,7 @@ pub fn dispatch(args: &[String]) -> Result<(),String> {
} else {
format!("The management service is unavailable. No changes were applied. Check `systemctl status archipelagod`. ({e})")
})?;
let client = Client { connection,interactive,offline,request_id:request,output };
let client = Client { connection,interactive,offline,offline_explicit,request_id:request,output };
client.output.borrow_mut().record("command", &json!({"arguments":args,"request_id":client.request_id}));
let result = match client.dispatch(&positional) {
Ok(result) => result,
@ -536,6 +548,10 @@ pub fn error_exit_code(message: &str) -> u8 {
.map(|code| code.exit_code()).unwrap_or(1)
}
fn offline_applies(args: &[&str]) -> bool {
matches!(args.first().copied(), Some("update" | "extension" | "overlay"))
}
fn karg_edits(args: &[&str]) -> Result<Vec<Dictionary>, String> {
let mut result = Vec::new();
let mut iter = args.iter();
@ -570,6 +586,16 @@ mod interface_tests {
assert!(karg_edits(&args).is_err());
}
}
#[test]
fn offline_is_limited_to_commands_that_can_use_a_cache() {
assert!(offline_applies(&["update", "check"]));
assert!(offline_applies(&["extension", "status"]));
assert!(offline_applies(&["overlay", "list"]));
for command in [&["status"][..], &["kargs"][..], &["transaction", "list"][..]] {
assert!(!offline_applies(command));
}
}
#[test]
fn operation_log_cause_prefers_the_real_error_line() {
let logs = json!({"lines":[