From b21634f0994fafc3d64be36b3b98f1de952c91eb Mon Sep 17 00:00:00 2001 From: Bill Raj-Derouin Date: Tue, 17 Nov 2026 12:11:10 -0800 Subject: [PATCH] rust(feat): read the sift-cli config file from SIFT_CONFIG_FILE --- rust/crates/sift_cli/CHANGELOG.md | 4 + .../docs/src/getting-started/configuration.md | 15 ++++ rust/crates/sift_cli/src/cmd/config/mod.rs | 82 +++++++++++++------ rust/crates/sift_cli/src/cmd/config/tests.rs | 62 ++++++++++++++ rust/crates/sift_cli/src/cmd/mod.rs | 28 +++---- 5 files changed, 149 insertions(+), 42 deletions(-) diff --git a/rust/crates/sift_cli/CHANGELOG.md b/rust/crates/sift_cli/CHANGELOG.md index 78e247f5a..64195bdf2 100644 --- a/rust/crates/sift_cli/CHANGELOG.md +++ b/rust/crates/sift_cli/CHANGELOG.md @@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). - `import csv` accepts `--run-id`, the id of an existing run. `--run` creates a new run each time, because run names are not unique. Use `--run-id` to add a file to a run left open with `--leave-run-open`. +- `SIFT_CONFIG_FILE` sets the path of the config file. When it is set and not + empty, every command reads that file, and `config create` and `config + update` write to it. Otherwise the CLI uses `sift.toml` in the user config + directory, as before. `sift_client` reads the same variable. ## [v0.9.0] - November 16, 2026 diff --git a/rust/crates/sift_cli/assets/docs/src/getting-started/configuration.md b/rust/crates/sift_cli/assets/docs/src/getting-started/configuration.md index da91f697d..8e0d6dac9 100644 --- a/rust/crates/sift_cli/assets/docs/src/getting-started/configuration.md +++ b/rust/crates/sift_cli/assets/docs/src/getting-started/configuration.md @@ -35,6 +35,21 @@ Settings live in a TOML file named `sift.toml` inside your OS config directory: | Linux | `~/.config/sift.toml` | | Windows | `%APPDATA%\sift.toml` | +To keep the file somewhere else, set `SIFT_CONFIG_FILE` to its path: + +```sh +export SIFT_CONFIG_FILE=/path/to/sift.toml +``` + +When `SIFT_CONFIG_FILE` is set and not empty, every command reads that file, +and `config create` and `config update` write to it. `config create` creates +any missing parent directories. The CLI uses the path as given. It does not +expand `~`, and a relative path resolves against the directory you run the +command from. The Python client, `sift_client`, reads the same variable. + +`sift-cli agent install` does not pass the variable to MCP clients, so +`sift-cli mcp` reads it only when the MCP client itself runs with it set. + Find the exact path on your machine: ```sh diff --git a/rust/crates/sift_cli/src/cmd/config/mod.rs b/rust/crates/sift_cli/src/cmd/config/mod.rs index 2e2cf48f0..da8fe865c 100644 --- a/rust/crates/sift_cli/src/cmd/config/mod.rs +++ b/rust/crates/sift_cli/src/cmd/config/mod.rs @@ -5,7 +5,10 @@ use crate::BIN_NAME; use anyhow::{Context, Result, anyhow}; use crossterm::style::Stylize; use std::{ - fs::{create_dir_all, metadata, read_to_string, write}, + env, + ffi::OsString, + fs::{create_dir_all, read_to_string, write}, + io::ErrorKind, path::{Path, PathBuf}, process::ExitCode, }; @@ -22,6 +25,9 @@ use crate::{ pub const CONFIG_FILE_NAME: &str = "sift.toml"; +/// Names the config file explicitly, in place of the user config directory. +const CONFIG_FILE_ENV: &str = "SIFT_CONFIG_FILE"; + /// The name shown for the top-level profile. const DEFAULT_PROFILE: &str = "default"; @@ -34,8 +40,8 @@ pub(super) enum AppUriState { } pub fn show() -> Result { - let p = get_config_file_path()?; - let contents = read_to_string(p).context("failed to read config file")?; + let path = get_config_file_path()?; + let contents = load_config_text(&path)?.ok_or_else(|| config_not_found(&path))?; Output::new().line(contents).print(); Ok(ExitCode::SUCCESS) } @@ -72,7 +78,8 @@ impl ProfileFields { pub fn create(profile: Option) -> Result { let path = get_config_file_path()?; let p = path.display().to_string(); - let exists = metadata(&path).is_ok(); + let existing = load_config(&path)?; + let exists = existing.is_some(); if !is_interactive() { if exists { @@ -99,11 +106,7 @@ pub fn create(profile: Option) -> Result { return Ok(ExitCode::SUCCESS); } - let config = if exists { - read_config(&path)? - } else { - Table::new() - }; + let config = existing.unwrap_or_default(); let explicit_default = profile.as_deref() == Some(DEFAULT_PROFILE); let profile = match resolve_profile(&config, profile) { @@ -159,7 +162,7 @@ pub fn create(profile: Option) -> Result { pub fn update(profile: Option, args: ConfigUpdateArgs) -> Result { let path = get_config_file_path()?; - if metadata(&path).is_err() { + let Some(config) = load_config(&path)? else { Output::new() .line(format!( "'{}' not found.", @@ -171,8 +174,7 @@ pub fn update(profile: Option, args: ConfigUpdateArgs) -> Result| value.filter(|v| !v.is_empty()); @@ -500,7 +502,7 @@ pub fn config_where() -> Result { let expected_path = get_config_file_path()?; let p = expected_path.display().to_string(); - if metadata(&expected_path).is_err() { + if load_config_text(&expected_path)?.is_none() { Output::new() .line(format!("'{}' not found.", p.yellow())) .tip(format!( @@ -515,16 +517,44 @@ pub fn config_where() -> Result { } pub(super) fn get_config_file_path() -> Result { - dirs::config_dir() - .map(|p| p.join(CONFIG_FILE_NAME)) - .ok_or(anyhow!("user config directory not found")) + config_file_path(env::var_os(CONFIG_FILE_ENV)) +} + +/// `SIFT_CONFIG_FILE` as given when it is set and non-empty, otherwise +/// `sift.toml` in the user config directory. +fn config_file_path(from_env: Option) -> Result { + match from_env { + Some(path) if !path.is_empty() => Ok(PathBuf::from(path)), + _ => dirs::config_dir() + .map(|p| p.join(CONFIG_FILE_NAME)) + .ok_or(anyhow!("user config directory not found")), + } +} + +/// The config file's text, or None when it does not exist. Any other failure, +/// such as a directory at the path, is an error that names the path. +fn load_config_text(path: &Path) -> Result> { + match read_to_string(path) { + Ok(text) => Ok(Some(text)), + Err(error) if error.kind() == ErrorKind::NotFound => Ok(None), + Err(error) => { + Err(error).with_context(|| format!("failed to read config file '{}'", path.display())) + } + } +} + +/// The parsed config file, or None when it does not exist. +pub(super) fn load_config(path: &Path) -> Result> { + load_config_text(path)? + .map(|text| { + text.parse::() + .with_context(|| format!("config file '{}' is invalid TOML", path.display())) + }) + .transpose() } -fn read_config(path: &Path) -> Result
{ - read_to_string(path) - .context("failed to read config file")? - .parse::
() - .context("config file is invalid TOML") +fn config_not_found(path: &Path) -> anyhow::Error { + anyhow!("'{}' not found.", path.display()) } fn apply_profile_updates( @@ -581,7 +611,9 @@ fn apply_profile_updates( } pub(super) fn inspect_app_uri(profile: Option<&str>) -> Result { - app_uri_state(&read_config(&get_config_file_path()?)?, profile) + let path = get_config_file_path()?; + let config = load_config(&path)?.ok_or_else(|| config_not_found(&path))?; + app_uri_state(&config, profile) } fn app_uri_state(config_toml: &Table, profile: Option<&str>) -> Result { @@ -640,7 +672,9 @@ fn print_app_uri_guidance(profile: Option<&str>, state: &AppUriState) { fn write_config(path: &Path, config: &Table) -> Result<()> { if let Some(parent_dir) = path.parent() { - create_dir_all(parent_dir).context("failed to create parent directories")?; + create_dir_all(parent_dir) + .with_context(|| format!("failed to create directory '{}'", parent_dir.display()))?; } - write(path, config.to_string()).context("failed to write config file") + write(path, config.to_string()) + .with_context(|| format!("failed to write config file '{}'", path.display())) } diff --git a/rust/crates/sift_cli/src/cmd/config/tests.rs b/rust/crates/sift_cli/src/cmd/config/tests.rs index 7076fd4c2..ec7d860c3 100644 --- a/rust/crates/sift_cli/src/cmd/config/tests.rs +++ b/rust/crates/sift_cli/src/cmd/config/tests.rs @@ -1,3 +1,65 @@ +mod config_file_path { + use super::super::{CONFIG_FILE_NAME, config_file_path, load_config, write_config}; + use std::{ffi::OsString, path::PathBuf}; + use tempdir::TempDir; + use toml::Table; + + fn user_config_file() -> Option { + dirs::config_dir().map(|dir| dir.join(CONFIG_FILE_NAME)) + } + + #[test] + fn uses_the_variable_as_given_when_set() { + for path in [ + "/tmp/agent-session/sift.toml", + "relative/sift.toml", + "~/sift.toml", + ] { + assert_eq!( + config_file_path(Some(OsString::from(path))).unwrap(), + PathBuf::from(path) + ); + } + } + + #[test] + fn treats_an_empty_variable_as_unset() { + assert_eq!( + config_file_path(Some(OsString::new())).ok(), + user_config_file() + ); + } + + #[test] + fn falls_back_to_the_user_config_directory_when_unset() { + assert_eq!(config_file_path(None).ok(), user_config_file()); + } + + #[test] + fn writing_creates_missing_parent_directories() { + let directory = TempDir::new("sift-cli-config-file").unwrap(); + let path = directory.path().join("missing/nested/sift.toml"); + write_config(&path, &Table::new()).unwrap(); + assert!(path.is_file()); + } + + #[test] + fn loading_tells_a_missing_file_from_an_unreadable_path() { + let directory = TempDir::new("sift-cli-config-file").unwrap(); + let path = directory.path().join("sift.toml"); + assert_eq!(load_config(&path).unwrap(), None); + + write_config(&path, &Table::new()).unwrap(); + assert_eq!(load_config(&path).unwrap(), Some(Table::new())); + + let message = format!("{:#}", load_config(directory.path()).unwrap_err()); + assert!( + message.contains(&directory.path().display().to_string()), + "error: {message}" + ); + } +} + mod app_uri { use super::super::{AppUriState, app_uri_state, apply_profile_updates}; use toml::{Table, Value}; diff --git a/rust/crates/sift_cli/src/cmd/mod.rs b/rust/crates/sift_cli/src/cmd/mod.rs index 48428fe89..c3b9b1cb7 100644 --- a/rust/crates/sift_cli/src/cmd/mod.rs +++ b/rust/crates/sift_cli/src/cmd/mod.rs @@ -2,7 +2,7 @@ use crate::BIN_NAME; use crate::util::app_uri::{infer_app_uri, normalize_app_uri}; use anyhow::{Context as AnyhowContext, Result, anyhow}; use crossterm::style::Stylize; -use std::{fs::read_to_string, io::ErrorKind, path::Path}; +use std::path::Path; use toml::{Table, Value}; pub mod agent; @@ -52,25 +52,17 @@ impl Context { profile: Option, disable_tls: bool, ) -> Result { - let p = config_path.display().to_string(); - - let config_txt = match read_to_string(config_path) { - Ok(txt) => txt, - Err(err) => match err.kind() { - ErrorKind::NotFound => { - return Err(anyhow!("expected to find '{}'.", p.yellow())).context(format!( - "Create a config using '{}'.", - format!("{BIN_NAME} config create").green() - )); - } - _ => return Err(anyhow!("failed to read config file")), - }, + let Some(config_toml) = config::load_config(config_path)? else { + return Err(anyhow!( + "expected to find '{}'.", + config_path.display().to_string().yellow() + )) + .context(format!( + "Create a config using '{}'.", + format!("{BIN_NAME} config create").green() + )); }; - let config_toml = config_txt - .parse::
() - .context("failed to parse config file")?; - let profile = config::resolve_profile(&config_toml, profile); let target_profile = match profile.as_deref() { Some(prof) => {