Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(ov-cli): report missing CLI config (#2279)

Hao Zhe22446fa2488b
diff --git a/crates/ov_cli/src/config.rs b/crates/ov_cli/src/config.rs
index 2fff9d881..dd6178311 100644
--- a/crates/ov_cli/src/config.rs
+++ b/crates/ov_cli/src/config.rs
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use crate::error::{Error, Result};
@@ -126,6 +126,16 @@ impl Config {
Self::load_default()
}
+ pub fn load_required() -> Result<Self> {
+ // Resolution order: env var > default path
+ if let Ok(env_path) = std::env::var(OPENVIKING_CLI_CONFIG_ENV) {
+ return Self::load_required_from_path(&PathBuf::from(env_path));
+ }
+
+ let config_path = default_config_path()?;
+ Self::load_required_from_path(&config_path)
+ }
+
pub fn load_default() -> Result<Self> {
// Resolution order: env var > default path
if let Ok(env_path) = std::env::var(OPENVIKING_CLI_CONFIG_ENV) {
@@ -136,8 +146,23 @@ impl Config {
}
let config_path = default_config_path()?;
- if config_path.exists() {
- Self::from_file(&config_path.to_string_lossy())
+ Self::load_default_from_path(&config_path)
+ }
+
+ pub fn load_required_from_path(path: &Path) -> Result<Self> {
+ if path.exists() {
+ Self::from_file(&path.to_string_lossy())
+ } else {
+ Err(Error::Config(
+ "No CLI config file detected, please use `ov config setup-cli` to initialize ovcli.conf"
+ .to_string(),
+ ))
+ }
+ }
+
+ pub fn load_default_from_path(path: &Path) -> Result<Self> {
+ if path.exists() {
+ Self::from_file(&path.to_string_lossy())
} else {
Ok(Self::default())
}
@@ -185,6 +210,31 @@ pub fn get_or_create_machine_id() -> Result<String> {
mod tests {
use super::{Config, merge_csv_options};
+ #[test]
+ fn load_required_from_path_reports_missing_cli_config() {
+ let dir = tempfile::tempdir().expect("tempdir should be created");
+ let path = dir.path().join("missing-ovcli.conf");
+
+ let error = Config::load_required_from_path(&path)
+ .expect_err("missing required config should fail")
+ .to_string();
+
+ assert!(error.contains("No CLI config file detected"));
+ assert!(error.contains("ov config setup-cli"));
+ assert!(error.contains("ovcli.conf"));
+ }
+
+ #[test]
+ fn load_default_from_path_keeps_default_fallback() {
+ let dir = tempfile::tempdir().expect("tempdir should be created");
+ let path = dir.path().join("missing-ovcli.conf");
+
+ let config = Config::load_default_from_path(&path)
+ .expect("default-loading path should still fall back");
+
+ assert_eq!(config.url, "http://localhost:1933");
+ }
+
#[test]
fn config_deserializes_account_and_user_fields() {
let config: Config = serde_json::from_str(
diff --git a/crates/ov_cli/src/main.rs b/crates/ov_cli/src/main.rs
index 4e00aea5c..112c9ff1f 100644
--- a/crates/ov_cli/src/main.rs
+++ b/crates/ov_cli/src/main.rs
@@ -10,7 +10,6 @@ mod utils;
use clap::{ArgAction, Args, Parser, Subcommand};
use config::Config;
-use error::Result;
use output::OutputFormat;
use std::ffi::OsString;
@@ -29,32 +28,6 @@ pub struct CliContext {
}
impl CliContext {
- pub fn new(
- output_format: OutputFormat,
- compact: bool,
- account: Option<String>,
- user: Option<String>,
- agent_id: Option<String>,
- sudo: bool,
- show_progress: Option<bool>,
- verbose: Option<bool>,
- profile: Option<bool>,
- ) -> Result<Self> {
- let config = Config::load()?;
- Ok(Self::from_config(
- config,
- output_format,
- compact,
- account,
- user,
- agent_id,
- sudo,
- show_progress,
- verbose,
- profile,
- ))
- }
-
fn from_config(
mut config: Config,
output_format: OutputFormat,
@@ -1019,6 +992,17 @@ enum AdminCommands {
},
}
+impl Commands {
+ fn requires_cli_config_file(&self) -> bool {
+ !matches!(
+ self,
+ Commands::Config {
+ action: ConfigCommands::SetupCli | ConfigCommands::Switch,
+ } | Commands::Version
+ )
+ }
+}
+
#[derive(Subcommand)]
enum ConfigCommands {
/// Show current configuration
@@ -1162,7 +1146,19 @@ async fn main() {
std::process::exit(2);
}
- let ctx = match CliContext::new(
+ let config = match if cli.command.requires_cli_config_file() {
+ Config::load_required()
+ } else {
+ Config::load_default()
+ } {
+ Ok(config) => config,
+ Err(e) => {
+ eprintln!("Error: {}", e);
+ std::process::exit(2);
+ }
+ };
+ let ctx = CliContext::from_config(
+ config,
output_format,
compact,
cli.account.clone(),
@@ -1172,13 +1168,7 @@ async fn main() {
None,
None,
if cli.profile { Some(true) } else { None },
- ) {
- Ok(ctx) => ctx,
- Err(e) => {
- eprintln!("Error: {}", e);
- std::process::exit(2);
- }
- };
+ );
// Check if --sudo is used but root_api_key is not configured
if ctx.sudo && ctx.config.root_api_key.is_none() {
@@ -1546,6 +1536,28 @@ mod tests {
assert_eq!(cli.agent_id.as_deref(), Some("assistant-1"));
}
+ #[test]
+ fn server_commands_require_existing_cli_config() {
+ let cli = Cli::try_parse_from(["ov", "ls"]).expect("ls should parse");
+ let health = Cli::try_parse_from(["ov", "health"]).expect("health should parse");
+
+ assert!(cli.command.requires_cli_config_file());
+ assert!(health.command.requires_cli_config_file());
+ }
+
+ #[test]
+ fn setup_switch_and_version_do_not_require_existing_cli_config() {
+ let setup = Cli::try_parse_from(["ov", "config", "setup-cli"])
+ .expect("config setup-cli should parse");
+ let switch =
+ Cli::try_parse_from(["ov", "config", "switch"]).expect("config switch should parse");
+ let version = Cli::try_parse_from(["ov", "version"]).expect("version should parse");
+
+ assert!(!setup.command.requires_cli_config_file());
+ assert!(!switch.command.requires_cli_config_file());
+ assert!(!version.command.requires_cli_config_file());
+ }
+
#[test]
fn cli_tree_help_hides_upload_and_admin_only_flags() {
let err = Cli::command()