Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rust/crates/sift_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 58 additions & 24 deletions rust/crates/sift_cli/src/cmd/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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";

Expand All @@ -34,8 +40,8 @@ pub(super) enum AppUriState {
}

pub fn show() -> Result<ExitCode> {
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)
}
Expand Down Expand Up @@ -72,7 +78,8 @@ impl ProfileFields {
pub fn create(profile: Option<String>) -> Result<ExitCode> {
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 {
Expand All @@ -99,11 +106,7 @@ pub fn create(profile: Option<String>) -> Result<ExitCode> {
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) {
Expand Down Expand Up @@ -159,7 +162,7 @@ pub fn create(profile: Option<String>) -> Result<ExitCode> {

pub fn update(profile: Option<String>, args: ConfigUpdateArgs) -> Result<ExitCode> {
let path = get_config_file_path()?;
if metadata(&path).is_err() {
let Some(config) = load_config(&path)? else {
Output::new()
.line(format!(
"'{}' not found.",
Expand All @@ -171,8 +174,7 @@ pub fn update(profile: Option<String>, args: ConfigUpdateArgs) -> Result<ExitCod
))
.eprint();
return Ok(ExitCode::FAILURE);
}
let config = read_config(&path)?;
};
let profile = resolve_profile(&config, profile);

let non_empty = |value: Option<String>| value.filter(|v| !v.is_empty());
Expand Down Expand Up @@ -500,7 +502,7 @@ pub fn config_where() -> Result<ExitCode> {
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!(
Expand All @@ -515,16 +517,44 @@ pub fn config_where() -> Result<ExitCode> {
}

pub(super) fn get_config_file_path() -> Result<PathBuf> {
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<OsString>) -> Result<PathBuf> {
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<Option<String>> {
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<Option<Table>> {
load_config_text(path)?
.map(|text| {
text.parse::<Table>()
.with_context(|| format!("config file '{}' is invalid TOML", path.display()))
})
.transpose()
}

fn read_config(path: &Path) -> Result<Table> {
read_to_string(path)
.context("failed to read config file")?
.parse::<Table>()
.context("config file is invalid TOML")
fn config_not_found(path: &Path) -> anyhow::Error {
anyhow!("'{}' not found.", path.display())
}

fn apply_profile_updates(
Expand Down Expand Up @@ -581,7 +611,9 @@ fn apply_profile_updates(
}

pub(super) fn inspect_app_uri(profile: Option<&str>) -> Result<AppUriState> {
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<AppUriState> {
Expand Down Expand Up @@ -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()))
}
62 changes: 62 additions & 0 deletions rust/crates/sift_cli/src/cmd/config/tests.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf> {
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};
Expand Down
28 changes: 10 additions & 18 deletions rust/crates/sift_cli/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,25 +52,17 @@ impl Context {
profile: Option<String>,
disable_tls: bool,
) -> Result<Self> {
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::<Table>()
.context("failed to parse config file")?;

let profile = config::resolve_profile(&config_toml, profile);
let target_profile = match profile.as_deref() {
Some(prof) => {
Expand Down
Loading