mirror of
https://github.com/bitbaum/openclaw.git
synced 2026-12-05 02:42:03 +01:00
The Linux desktop companion could complete a CLI install and still claim 'Installation did not finish' with circular update advice, discarding the real failure. Verified end-to-end in a clean Ubuntu VM across all three release channels: - cli.rs: failed CLI commands now surface their stderr tail (deduped, last 12 lines) instead of being mislabeled as JSON parse failures. - gateway.rs: missing dashboard --json support maps to an honest curated message pointing at Beta/Development channels, not a circular npm-update hint. - main.rs: run 'doctor --fix --non-interactive' right after install so the CLI repairs config/state before Gateway readiness checks; wrap post-install failures as 'installed, but connecting failed: <reason>'. - installer.rs: keep structured step events out of the prose failure tail. - ui/main.js: humanize streamed install steps, render real errors on the failure screen, and always offer Reinstall from connection failures. - scripts/install-cli.sh: service refresh uses 'gateway status --json' with the bundled node runtime, corepack failure falls back to npm, dev channel clones with --filter=blob:none.
179 lines
5.5 KiB
Rust
179 lines
5.5 KiB
Rust
use serde::de::DeserializeOwned;
|
|
use std::env;
|
|
use std::ffi::OsString;
|
|
use std::fmt;
|
|
use std::path::PathBuf;
|
|
use std::process::{Command, Output, Stdio};
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct OpenClawCli {
|
|
executable: PathBuf,
|
|
openclaw_home: PathBuf,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum CliError {
|
|
Missing,
|
|
Environment(String),
|
|
Spawn(String),
|
|
CommandFailed(String),
|
|
InvalidJson(String),
|
|
}
|
|
|
|
impl fmt::Display for CliError {
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Self::Missing => write!(formatter, "OpenClaw CLI not found"),
|
|
Self::Environment(message)
|
|
| Self::Spawn(message)
|
|
| Self::CommandFailed(message)
|
|
| Self::InvalidJson(message) => formatter.write_str(message),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for CliError {}
|
|
|
|
impl OpenClawCli {
|
|
pub fn discover() -> Result<Self, CliError> {
|
|
let home = openclaw_home()?;
|
|
if let Some(override_path) = env::var_os("OPENCLAW_DESKTOP_CLI") {
|
|
let cli = Self::new(PathBuf::from(override_path), home);
|
|
cli.verify()?;
|
|
return Ok(cli);
|
|
}
|
|
|
|
let managed = home.join("bin/openclaw");
|
|
if managed.is_file() {
|
|
let cli = Self::new(managed, home);
|
|
cli.verify()?;
|
|
return Ok(cli);
|
|
}
|
|
|
|
let cli = Self::new(PathBuf::from("openclaw"), home);
|
|
match cli.verify() {
|
|
Ok(()) => Ok(cli),
|
|
Err(_) => Err(CliError::Missing),
|
|
}
|
|
}
|
|
|
|
fn new(executable: PathBuf, openclaw_home: PathBuf) -> Self {
|
|
Self {
|
|
executable,
|
|
openclaw_home,
|
|
}
|
|
}
|
|
|
|
fn verify(&self) -> Result<(), CliError> {
|
|
let output = self.output(["--version"])?;
|
|
if output.status.success() {
|
|
return Ok(());
|
|
}
|
|
Err(CliError::Spawn(format!(
|
|
"OpenClaw CLI exited with {}",
|
|
output.status
|
|
)))
|
|
}
|
|
|
|
pub fn command<I, S>(&self, args: I) -> Result<Command, CliError>
|
|
where
|
|
I: IntoIterator<Item = S>,
|
|
S: AsRef<std::ffi::OsStr>,
|
|
{
|
|
let mut command = Command::new(&self.executable);
|
|
command.args(args);
|
|
command.env("PATH", self.command_path()?);
|
|
command.stdin(Stdio::null());
|
|
Ok(command)
|
|
}
|
|
|
|
pub fn output<I, S>(&self, args: I) -> Result<Output, CliError>
|
|
where
|
|
I: IntoIterator<Item = S>,
|
|
S: AsRef<std::ffi::OsStr>,
|
|
{
|
|
self.command(args)?
|
|
.output()
|
|
.map_err(|error| CliError::Spawn(format!("Failed to run OpenClaw CLI: {error}")))
|
|
}
|
|
|
|
pub fn json<T, I, S>(&self, args: I) -> Result<(T, Output), CliError>
|
|
where
|
|
T: DeserializeOwned,
|
|
I: IntoIterator<Item = S>,
|
|
S: AsRef<std::ffi::OsStr>,
|
|
{
|
|
let output = self.output(args)?;
|
|
// Failed commands own their stderr; parsing first would mislabel real
|
|
// failures as missing CLI dashboard support.
|
|
if !output.status.success() {
|
|
let message = output_tail(&output.stderr)
|
|
.or_else(|| output_tail(&output.stdout))
|
|
.unwrap_or_else(|| format!("OpenClaw CLI exited with {}", output.status));
|
|
return Err(CliError::CommandFailed(message));
|
|
}
|
|
let value = serde_json::from_slice(&output.stdout).map_err(|error| {
|
|
CliError::InvalidJson(format!("OpenClaw CLI returned invalid JSON: {error}"))
|
|
})?;
|
|
Ok((value, output))
|
|
}
|
|
|
|
fn command_path(&self) -> Result<OsString, CliError> {
|
|
let mut paths = vec![
|
|
self.openclaw_home.join("bin"),
|
|
self.openclaw_home.join("tools/node/bin"),
|
|
];
|
|
if let Some(current) = env::var_os("PATH") {
|
|
paths.extend(env::split_paths(¤t));
|
|
}
|
|
env::join_paths(paths)
|
|
.map_err(|error| CliError::Environment(format!("Could not construct PATH: {error}")))
|
|
}
|
|
}
|
|
|
|
pub(crate) fn output_tail(output: &[u8]) -> Option<String> {
|
|
let text = String::from_utf8_lossy(output);
|
|
let mut lines: Vec<&str> = Vec::new();
|
|
for line in text.lines().filter(|line| !line.trim().is_empty()) {
|
|
// The CLI repeats identical progress lines while waiting; one occurrence
|
|
// carries the same information in a user-facing failure message.
|
|
if lines.last() != Some(&line) {
|
|
lines.push(line);
|
|
}
|
|
}
|
|
let start = lines.len().saturating_sub(12);
|
|
let tail = &lines[start..];
|
|
(!tail.is_empty()).then(|| tail.join("\n"))
|
|
}
|
|
|
|
pub fn openclaw_home() -> Result<PathBuf, CliError> {
|
|
#[cfg(target_os = "windows")]
|
|
let home = env::var_os("HOME")
|
|
.filter(|value| !value.is_empty())
|
|
.or_else(|| env::var_os("USERPROFILE").filter(|value| !value.is_empty()));
|
|
#[cfg(not(target_os = "windows"))]
|
|
let home = env::var_os("HOME").filter(|value| !value.is_empty());
|
|
let home = home.ok_or_else(|| CliError::Environment("HOME is not set".to_string()))?;
|
|
Ok(PathBuf::from(home).join(".openclaw"))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::output_tail;
|
|
|
|
#[test]
|
|
fn output_tail_keeps_the_last_twelve_nonempty_lines() {
|
|
let output = (1..=15)
|
|
.map(|line| format!("message {line}"))
|
|
.collect::<Vec<_>>()
|
|
.join("\n\n");
|
|
let expected = (4..=15)
|
|
.map(|line| format!("message {line}"))
|
|
.collect::<Vec<_>>()
|
|
.join("\n");
|
|
|
|
assert_eq!(output_tail(output.as_bytes()), Some(expected));
|
|
assert_eq!(output_tail(b"\n \n"), None);
|
|
}
|
|
}
|