Read the shared ~/.config/inbuxa profile for import targets #78
No files matched your search
@@ -22,6 +22,15 @@ candidates before a series ships (`27.1-rc.1`). The tag is the version with a
|
||||
proactive throttle to stay under a rate-limited Microsoft 365 tenant's
|
||||
limit, alongside the existing honoring of 429/`Retry-After`. Off by
|
||||
default (uncapped).
|
||||
- `import jmap` and `import-directory` read the inbuxa server's address and
|
||||
API key from a shared profile, `~/.config/inbuxa/config.toml`, so the
|
||||
target need not be given each time. `[profile.<name>]` sections hold a
|
||||
`url` and either an `api-key` or an `api-key-file` (a path, so the key can
|
||||
live in its own file); `--profile` or `$INBUXA_PROFILE` pick a section, and
|
||||
the default is `default`. `inbuxa-cli` reads the same file. A `--url` or an
|
||||
`INBUXA_MIGRATE_*` variable still wins, so nothing changes when they are
|
||||
given; `--url` is now optional on these two commands when a profile
|
||||
supplies it.
|
||||
|
||||
### Changed
|
||||
- The `guide` page shows a real progress bar with a running count and
|
||||
|
||||
Generated
+53
@@ -1375,6 +1375,7 @@ dependencies = [
|
||||
"testcontainers",
|
||||
"thiserror",
|
||||
"time",
|
||||
"toml",
|
||||
"ureq",
|
||||
"url",
|
||||
]
|
||||
@@ -2418,6 +2419,15 @@ dependencies = [
|
||||
"syn 3.0.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_spanned"
|
||||
version = "0.6.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_urlencoded"
|
||||
version = "0.7.1"
|
||||
@@ -2806,6 +2816,40 @@ dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.8.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"toml_edit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "0.6.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_edit"
|
||||
version = "0.22.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a"
|
||||
dependencies = [
|
||||
"indexmap 2.14.2",
|
||||
"serde",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tonic"
|
||||
version = "0.14.6"
|
||||
@@ -3280,6 +3324,15 @@ version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.7.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen"
|
||||
version = "0.57.1"
|
||||
|
||||
@@ -21,6 +21,7 @@ bundled-sqlite = ["rusqlite/bundled"]
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
indexmap = { version = "2.14", features = ["serde"] }
|
||||
toml = { version = "0.8", default-features = false, features = ["parse"] }
|
||||
rusqlite = { version = "0.41", features = ["blob"] }
|
||||
blake3 = "1.8.5"
|
||||
time = { version = "0.3.47", features = ["serde-well-known", "macros"] }
|
||||
|
||||
@@ -112,6 +112,29 @@ already a file: `--google-key <path>`.)
|
||||
The command line takes them too, but a secret there ends up in the shell's
|
||||
history and in the process list. Use a file, the environment, or the prompt.
|
||||
|
||||
### A shared profile for the inbuxa server
|
||||
|
||||
`import` (into a JMAP server) and `import-directory` write to an inbuxa
|
||||
server. Instead of giving its address and key each time, name it once in
|
||||
`~/.config/inbuxa/config.toml`, which `inbuxa-cli` reads too:
|
||||
|
||||
[profile.default]
|
||||
url = "https://mail.example.org"
|
||||
api-key = "API_…"
|
||||
|
||||
[profile.staging]
|
||||
url = "https://staging.example.org"
|
||||
api-key-file = "~/.secrets/staging.key"
|
||||
|
||||
A section gives `url` and either `api-key` or `api-key-file` (a path to a file
|
||||
holding the key, so the key stays out of the config). Keep the file to
|
||||
yourself -- `chmod 600 ~/.config/inbuxa/config.toml`. Without `--profile` or
|
||||
`$INBUXA_PROFILE`, the `default` section is used; `--profile staging` picks
|
||||
another. A `--url`, or any `INBUXA_MIGRATE_*` variable, still wins, so the
|
||||
profile is only the fallback and `--url` becomes optional on these two
|
||||
commands when it is set. The other commands connect to IMAP, DAV and the like,
|
||||
not to the inbuxa server, so they don't read the profile.
|
||||
|
||||
## Installing
|
||||
|
||||
Linux, on amd64 or arm64. Each release carries one archive per architecture
|
||||
|
||||
@@ -63,6 +63,23 @@ by the same variable with `_FILE` appended (the Docker and systemd secrets
|
||||
convention), or a prompt; see [Credentials](../README.md#credentials). The
|
||||
command line takes them too, but should not be given them.
|
||||
|
||||
`import` and `import-directory` write to an inbuxa server. Its address and API
|
||||
key can be named once in a shared profile, `~/.config/inbuxa/config.toml`
|
||||
(which `inbuxa-cli` reads too), so `--url` and the key need not be repeated:
|
||||
|
||||
```toml
|
||||
[profile.default]
|
||||
url = "https://mail.example.org"
|
||||
api-key = "API_…"
|
||||
```
|
||||
|
||||
`--profile` or `$INBUXA_PROFILE` pick a `[profile.<name>]` section, and the
|
||||
default is `default`; a section gives `url` and either `api-key` or
|
||||
`api-key-file` (a path to a file holding the key). A `--url`, or any
|
||||
`INBUXA_MIGRATE_*` variable, still takes precedence, so `--url` is optional on
|
||||
these two commands only when a profile supplies it. See
|
||||
[Credentials](../README.md#a-shared-profile-for-the-inbuxa-server).
|
||||
|
||||
## Import
|
||||
|
||||
```
|
||||
|
||||
+74
-17
@@ -23,6 +23,7 @@ use crate::jmap::account::AccountSelector;
|
||||
use crate::jmap::http::Auth;
|
||||
use crate::logging::Logger;
|
||||
use crate::migrate::MigrateConfig;
|
||||
use crate::profile::Profile;
|
||||
use crate::secret;
|
||||
use crate::sync::export_dav::ExportDavConfig;
|
||||
use crate::sync::export_imap::ExportImapConfig;
|
||||
@@ -215,11 +216,22 @@ struct GlobalArgs {
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
#[command(group(clap::ArgGroup::new("auth").required(true).args(["auth_basic", "auth_bearer"])))]
|
||||
#[command(group(clap::ArgGroup::new("auth").args(["auth_basic", "auth_bearer"])))]
|
||||
#[command(group(clap::ArgGroup::new("account").required(true).args(["account_id", "account_name"])))]
|
||||
struct JmapImportArgs {
|
||||
#[arg(long, value_name = "URL", help = "Base URL or full JMAP session URL")]
|
||||
url: String,
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "URL",
|
||||
help = "The target inbuxa server: base URL or full JMAP session URL (else the profile's url)"
|
||||
)]
|
||||
url: Option<String>,
|
||||
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "NAME",
|
||||
help = "Profile in ~/.config/inbuxa/config.toml (or $INBUXA_PROFILE) for the url and API key not given here"
|
||||
)]
|
||||
profile: Option<String>,
|
||||
|
||||
#[arg(
|
||||
long,
|
||||
@@ -701,9 +713,16 @@ struct ImportDirectoryArgs {
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "URL",
|
||||
help = "The inbuxa server: base URL or full JMAP session URL"
|
||||
help = "The inbuxa server: base URL or full JMAP session URL (else the profile's url)"
|
||||
)]
|
||||
url: String,
|
||||
url: Option<String>,
|
||||
|
||||
#[arg(
|
||||
long,
|
||||
value_name = "NAME",
|
||||
help = "Profile in ~/.config/inbuxa/config.toml (or $INBUXA_PROFILE) for the url and API key not given here"
|
||||
)]
|
||||
profile: Option<String>,
|
||||
|
||||
#[arg(
|
||||
long,
|
||||
@@ -1420,10 +1439,17 @@ fn resolve_dav_auth(
|
||||
|
||||
fn resolve_jmap_import(args: JmapImportArgs) -> Result<Action, Error> {
|
||||
let common = common_config(&args.global, args.archive);
|
||||
let profile = Profile::load(args.profile.as_deref())?;
|
||||
let url = args.url.or(profile.url).ok_or_else(|| {
|
||||
Error::Usage(
|
||||
"no target server: pass --url, or name one in ~/.config/inbuxa/config.toml".to_owned(),
|
||||
)
|
||||
})?;
|
||||
let auth = resolve_auth(
|
||||
args.auth_basic.as_deref(),
|
||||
args.auth_password.as_deref(),
|
||||
args.auth_bearer.as_ref(),
|
||||
profile.api_key.as_deref(),
|
||||
)?;
|
||||
let account = resolve_account(args.account_id, args.account_name)?;
|
||||
let objects = args.objects.as_deref().map(parse_object_list).transpose()?;
|
||||
@@ -1433,7 +1459,7 @@ fn resolve_jmap_import(args: JmapImportArgs) -> Result<Action, Error> {
|
||||
ImportConfig {
|
||||
connect: ConnectConfig {
|
||||
stay_on_url: args.stay_on_url,
|
||||
url: args.url,
|
||||
url,
|
||||
auth,
|
||||
account,
|
||||
},
|
||||
@@ -1557,6 +1583,7 @@ fn resolve_export(args: ExportArgs) -> Result<Action, Error> {
|
||||
args.auth_basic.as_deref(),
|
||||
args.auth_password.as_deref(),
|
||||
args.auth_bearer.as_ref(),
|
||||
None,
|
||||
)?;
|
||||
let account = resolve_account(args.account_id, args.account_name)?;
|
||||
let objects = args.objects.as_deref().map(parse_object_list).transpose()?;
|
||||
@@ -1647,6 +1674,7 @@ fn resolve_export_directory(args: ExportDirectoryArgs) -> Result<Action, Error>
|
||||
args.auth_basic.as_deref(),
|
||||
args.auth_password.as_deref(),
|
||||
args.auth_bearer.as_ref(),
|
||||
None,
|
||||
)?;
|
||||
Ok(Action::ExportDirectory(ExportDirectoryConfig {
|
||||
stay_on_url: args.stay_on_url,
|
||||
@@ -1662,14 +1690,21 @@ fn resolve_export_directory(args: ExportDirectoryArgs) -> Result<Action, Error>
|
||||
}
|
||||
|
||||
fn resolve_import_directory(args: ImportDirectoryArgs) -> Result<Action, Error> {
|
||||
let profile = Profile::load(args.profile.as_deref())?;
|
||||
let url = args.url.or(profile.url).ok_or_else(|| {
|
||||
Error::Usage(
|
||||
"no inbuxa server: pass --url, or name one in ~/.config/inbuxa/config.toml".to_owned(),
|
||||
)
|
||||
})?;
|
||||
let auth = resolve_auth(
|
||||
args.auth_basic.as_deref(),
|
||||
args.auth_password.as_deref(),
|
||||
args.auth_bearer.as_ref(),
|
||||
profile.api_key.as_deref(),
|
||||
)?;
|
||||
Ok(Action::ImportDirectory(ImportDirectoryConfig {
|
||||
stay_on_url: args.stay_on_url,
|
||||
url: args.url,
|
||||
url,
|
||||
auth,
|
||||
dir: args.dir,
|
||||
domains: args.domain,
|
||||
@@ -2236,6 +2271,7 @@ fn resolve_auth(
|
||||
auth_basic: Option<&str>,
|
||||
auth_password: Option<&str>,
|
||||
auth_bearer: Option<&Option<String>>,
|
||||
profile_key: Option<&str>,
|
||||
) -> Result<Auth, Error> {
|
||||
if let Some(user) = auth_basic {
|
||||
let password = secret::resolve(auth_password, "INBUXA_MIGRATE_PASSWORD", "password")?;
|
||||
@@ -2249,12 +2285,15 @@ fn resolve_auth(
|
||||
"--auth-password is only valid together with --auth-basic".to_owned(),
|
||||
));
|
||||
}
|
||||
let bearer = auth_bearer.ok_or_else(|| {
|
||||
Error::Usage("exactly one of --auth-basic / --auth-bearer is required".to_owned())
|
||||
})?;
|
||||
let token = secret::resolve_any(
|
||||
bearer.as_deref(),
|
||||
// No --auth-basic, so a bearer token: from --auth-bearer's value, the
|
||||
// environment, the profile, or a prompt. --auth-bearer may be given with
|
||||
// no value, and need not be given at all when a profile or the
|
||||
// environment supplies the key.
|
||||
let bearer_flag = auth_bearer.and_then(|b| b.as_deref());
|
||||
let token = secret::resolve_any_or(
|
||||
bearer_flag,
|
||||
secret::API_KEY_VARS,
|
||||
profile_key,
|
||||
"API key or bearer token",
|
||||
)?;
|
||||
Ok(Auth::Bearer { token })
|
||||
@@ -2390,7 +2429,7 @@ mod tests {
|
||||
env::remove_var("INBUXA_MIGRATE_PASSWORD");
|
||||
}
|
||||
let bearer: Option<String> = Some("t".to_owned());
|
||||
let r = resolve_auth(None, Some("hunter2"), Some(&bearer));
|
||||
let r = resolve_auth(None, Some("hunter2"), Some(&bearer), None);
|
||||
match r {
|
||||
Err(Error::Usage(m)) => {
|
||||
assert!(m.contains("auth-password"), "msg was: {m}");
|
||||
@@ -2407,7 +2446,7 @@ mod tests {
|
||||
env::remove_var("INBUXA_MIGRATE_TOKEN");
|
||||
env::remove_var("INBUXA_MIGRATE_PASSWORD");
|
||||
}
|
||||
let auth = resolve_auth(Some("alice"), Some("pw"), None).unwrap();
|
||||
let auth = resolve_auth(Some("alice"), Some("pw"), None, None).unwrap();
|
||||
match auth {
|
||||
Auth::Basic { user, password } => {
|
||||
assert_eq!(user, "alice");
|
||||
@@ -2426,7 +2465,7 @@ mod tests {
|
||||
env::remove_var("INBUXA_MIGRATE_PASSWORD");
|
||||
}
|
||||
let bearer: Option<String> = Some("t".to_owned());
|
||||
let auth = resolve_auth(None, None, Some(&bearer)).unwrap();
|
||||
let auth = resolve_auth(None, None, Some(&bearer), None).unwrap();
|
||||
assert!(matches!(auth, Auth::Bearer { token } if token == "t"));
|
||||
}
|
||||
|
||||
@@ -2437,14 +2476,32 @@ mod tests {
|
||||
env::set_var("INBUXA_MIGRATE_API_KEY", "key");
|
||||
env::set_var("INBUXA_MIGRATE_TOKEN", "old");
|
||||
}
|
||||
let auth = resolve_auth(None, None, Some(&None)).unwrap();
|
||||
let auth = resolve_auth(None, None, Some(&None), None).unwrap();
|
||||
assert!(matches!(auth, Auth::Bearer { token } if token == "key"));
|
||||
unsafe { env::remove_var("INBUXA_MIGRATE_API_KEY") };
|
||||
let auth = resolve_auth(None, None, Some(&None)).unwrap();
|
||||
let auth = resolve_auth(None, None, Some(&None), None).unwrap();
|
||||
assert!(matches!(auth, Auth::Bearer { token } if token == "old"));
|
||||
unsafe { env::remove_var("INBUXA_MIGRATE_TOKEN") };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auth_bearer_uses_the_profile_key_when_flag_and_env_are_absent() {
|
||||
let _g = lock();
|
||||
unsafe {
|
||||
env::remove_var("INBUXA_MIGRATE_API_KEY");
|
||||
env::remove_var("INBUXA_MIGRATE_TOKEN");
|
||||
}
|
||||
// No --auth-bearer and nothing in the environment: the profile's key
|
||||
// is used, without a prompt.
|
||||
let auth = resolve_auth(None, None, None, Some("from-profile")).unwrap();
|
||||
assert!(matches!(auth, Auth::Bearer { token } if token == "from-profile"));
|
||||
// The environment still wins over the profile.
|
||||
unsafe { env::set_var("INBUXA_MIGRATE_API_KEY", "from-env") };
|
||||
let auth = resolve_auth(None, None, None, Some("from-profile")).unwrap();
|
||||
assert!(matches!(auth, Auth::Bearer { token } if token == "from-env"));
|
||||
unsafe { env::remove_var("INBUXA_MIGRATE_API_KEY") };
|
||||
}
|
||||
|
||||
fn ews_auth(extra: &[&str]) -> Result<EwsAuth, Error> {
|
||||
let mut argv = vec!["inbuxa-migrate", "import", "exchange-ews", "archive.sqlite"];
|
||||
argv.extend_from_slice(extra);
|
||||
|
||||
@@ -24,6 +24,7 @@ pub mod logging;
|
||||
pub mod managesieve;
|
||||
pub mod migrate;
|
||||
pub mod net;
|
||||
pub mod profile;
|
||||
pub mod secret;
|
||||
pub mod sync;
|
||||
pub mod types;
|
||||
|
||||
+243
@@ -0,0 +1,243 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! The shared `~/.config/inbuxa` profile. A `config.toml` names an inbuxa
|
||||
//! server once, so this and `inbuxa-cli` read the same server address and API
|
||||
//! key instead of each being told again. For `inbuxa-migrate` it supplies the
|
||||
//! inbuxa server the `import` and `import-directory` commands write to:
|
||||
//!
|
||||
//! ```toml
|
||||
//! [profile.default]
|
||||
//! url = "https://mail.example.org"
|
||||
//! api-key = "inbuxa_pat_…"
|
||||
//!
|
||||
//! [profile.staging]
|
||||
//! url = "https://staging.example.org"
|
||||
//! api-key-file = "~/.secrets/staging.key"
|
||||
//! ```
|
||||
//!
|
||||
//! `INBUXA_PROFILE` or `--profile` picks a section; without one the `default`
|
||||
//! section is used if it is there. A flag or an `INBUXA_MIGRATE_*` variable
|
||||
//! always wins, so the file is only a fallback. The format is the same one
|
||||
//! `inbuxa-cli` reads.
|
||||
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::error::Error;
|
||||
|
||||
/// What a profile can supply: the pieces a flag or variable didn't.
|
||||
#[derive(Debug, Default, PartialEq, Eq)]
|
||||
pub struct Profile {
|
||||
pub url: Option<String>,
|
||||
pub api_key: Option<String>,
|
||||
}
|
||||
|
||||
impl Profile {
|
||||
/// The active profile. `flag` is `--profile` if it was given; otherwise
|
||||
/// `$INBUXA_PROFILE` is consulted. When either names a profile, a missing
|
||||
/// file or section is an error; with neither, the `default` section is
|
||||
/// read if the file has one, and otherwise an empty profile is returned.
|
||||
pub fn load(flag: Option<&str>) -> Result<Profile, Error> {
|
||||
let from_env = std::env::var("INBUXA_PROFILE")
|
||||
.ok()
|
||||
.filter(|v| !v.is_empty());
|
||||
let explicit = flag.is_some() || from_env.is_some();
|
||||
let name = flag.or(from_env.as_deref()).unwrap_or("default");
|
||||
let Some(path) = config_path() else {
|
||||
return Ok(Profile::default());
|
||||
};
|
||||
let text = match fs::read_to_string(&path) {
|
||||
Ok(text) => text,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
||||
return if explicit {
|
||||
Err(Error::Usage(format!(
|
||||
"no profile file at {}",
|
||||
path.display()
|
||||
)))
|
||||
} else {
|
||||
Ok(Profile::default())
|
||||
};
|
||||
}
|
||||
Err(e) => return Err(Error::Usage(format!("{}: {e}", path.display()))),
|
||||
};
|
||||
from_toml(&text, name, explicit, &path, home_dir().as_deref())
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses one section out of the file's text. Split out from [`Profile::load`]
|
||||
/// so it can be tested without a real config directory; `home` resolves a
|
||||
/// leading `~/` in an `api-key-file`.
|
||||
fn from_toml(
|
||||
text: &str,
|
||||
name: &str,
|
||||
explicit: bool,
|
||||
path: &Path,
|
||||
home: Option<&Path>,
|
||||
) -> Result<Profile, Error> {
|
||||
let at = |e: String| Error::Usage(format!("{}: {e}", path.display()));
|
||||
let doc: toml::Table = toml::from_str(text).map_err(|e| at(e.to_string()))?;
|
||||
let section = doc
|
||||
.get("profile")
|
||||
.and_then(|p| p.as_table())
|
||||
.and_then(|t| t.get(name));
|
||||
let Some(section) = section else {
|
||||
return if explicit {
|
||||
Err(at(format!("no [profile.{name}]")))
|
||||
} else {
|
||||
Ok(Profile::default())
|
||||
};
|
||||
};
|
||||
let field = |key: &str| -> Result<Option<String>, Error> {
|
||||
match section.get(key) {
|
||||
None => Ok(None),
|
||||
Some(value) => value
|
||||
.as_str()
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| Some(s.to_owned()))
|
||||
.ok_or_else(|| at(format!("profile.{name}.{key} must be a non-empty string"))),
|
||||
}
|
||||
};
|
||||
let url = field("url")?;
|
||||
let api_key = match (field("api-key")?, field("api-key-file")?) {
|
||||
(Some(_), Some(_)) => {
|
||||
return Err(at(format!(
|
||||
"profile.{name} sets both api-key and api-key-file; use one"
|
||||
)));
|
||||
}
|
||||
(Some(key), None) => Some(key),
|
||||
(None, Some(file)) => Some(read_key_file(&file, home, path, name)?),
|
||||
(None, None) => None,
|
||||
};
|
||||
Ok(Profile { url, api_key })
|
||||
}
|
||||
|
||||
/// Reads an `api-key-file`: a `~/` is expanded against `home`, and one
|
||||
/// trailing newline is dropped so an edited file still works.
|
||||
fn read_key_file(
|
||||
spec: &str,
|
||||
home: Option<&Path>,
|
||||
path: &Path,
|
||||
name: &str,
|
||||
) -> Result<String, Error> {
|
||||
let file = expand_tilde(spec, home);
|
||||
let raw = fs::read_to_string(&file).map_err(|e| {
|
||||
Error::Usage(format!(
|
||||
"{}: profile.{name}.api-key-file {}: {e}",
|
||||
path.display(),
|
||||
file.display()
|
||||
))
|
||||
})?;
|
||||
let key = raw
|
||||
.strip_suffix('\n')
|
||||
.map(|s| s.strip_suffix('\r').unwrap_or(s))
|
||||
.unwrap_or(&raw);
|
||||
if key.is_empty() {
|
||||
return Err(Error::Usage(format!(
|
||||
"{}: profile.{name}.api-key-file {} is empty",
|
||||
path.display(),
|
||||
file.display()
|
||||
)));
|
||||
}
|
||||
Ok(key.to_owned())
|
||||
}
|
||||
|
||||
fn expand_tilde(spec: &str, home: Option<&Path>) -> PathBuf {
|
||||
match (spec.strip_prefix("~/"), home) {
|
||||
(Some(rest), Some(home)) => home.join(rest),
|
||||
_ => PathBuf::from(spec),
|
||||
}
|
||||
}
|
||||
|
||||
/// `~/.config/inbuxa/config.toml`, honoring `XDG_CONFIG_HOME`.
|
||||
fn config_path() -> Option<PathBuf> {
|
||||
let base = match std::env::var_os("XDG_CONFIG_HOME") {
|
||||
Some(x) if !x.is_empty() => PathBuf::from(x),
|
||||
_ => home_dir()?.join(".config"),
|
||||
};
|
||||
Some(base.join("inbuxa").join("config.toml"))
|
||||
}
|
||||
|
||||
fn home_dir() -> Option<PathBuf> {
|
||||
std::env::var_os("HOME")
|
||||
.filter(|h| !h.is_empty())
|
||||
.map(PathBuf::from)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn path() -> PathBuf {
|
||||
PathBuf::from("/home/me/.config/inbuxa/config.toml")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_url_and_inline_key_from_the_named_section() {
|
||||
let text = "\
|
||||
[profile.default]
|
||||
url = \"https://mail.example.org\"
|
||||
api-key = \"secret-default\"
|
||||
|
||||
[profile.staging]
|
||||
url = \"https://staging.example.org\"
|
||||
api-key = \"secret-staging\"
|
||||
";
|
||||
let default = from_toml(text, "default", false, &path(), None).unwrap();
|
||||
assert_eq!(default.url.as_deref(), Some("https://mail.example.org"));
|
||||
assert_eq!(default.api_key.as_deref(), Some("secret-default"));
|
||||
let staging = from_toml(text, "staging", true, &path(), None).unwrap();
|
||||
assert_eq!(staging.api_key.as_deref(), Some("secret-staging"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_default_is_empty_but_a_missing_named_one_is_an_error() {
|
||||
let text = "[profile.work]\nurl = \"https://work.example.org\"\n";
|
||||
assert_eq!(
|
||||
from_toml(text, "default", false, &path(), None).unwrap(),
|
||||
Profile::default()
|
||||
);
|
||||
let err = from_toml(text, "other", true, &path(), None).unwrap_err();
|
||||
assert!(err.to_string().contains("no [profile.other]"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn both_api_key_forms_set_is_an_error() {
|
||||
let text = "[profile.default]\napi-key = \"inline\"\napi-key-file = \"/tmp/x\"\n";
|
||||
let err = from_toml(text, "default", false, &path(), None).unwrap_err();
|
||||
assert!(
|
||||
err.to_string().contains("both api-key and api-key-file"),
|
||||
"{err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_non_string_field_is_an_error() {
|
||||
let text = "[profile.default]\nurl = 42\n";
|
||||
let err = from_toml(text, "default", false, &path(), None).unwrap_err();
|
||||
assert!(
|
||||
err.to_string().contains("url must be a non-empty string"),
|
||||
"{err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn api_key_file_is_read_tilde_expanded_with_one_trailing_newline_dropped() {
|
||||
let dir =
|
||||
std::env::temp_dir().join(format!("inbuxa-migrate-profile-{}", std::process::id()));
|
||||
fs::create_dir_all(dir.join(".secrets")).unwrap();
|
||||
fs::write(dir.join(".secrets/k.key"), "file-key\n").unwrap();
|
||||
let text = "[profile.default]\napi-key-file = \"~/.secrets/k.key\"\n";
|
||||
let p = from_toml(text, "default", false, &path(), Some(&dir)).unwrap();
|
||||
assert_eq!(p.api_key.as_deref(), Some("file-key"));
|
||||
|
||||
fs::write(dir.join(".secrets/empty.key"), "\n").unwrap();
|
||||
let empty = "[profile.default]\napi-key-file = \"~/.secrets/empty.key\"\n";
|
||||
let err = from_toml(empty, "default", false, &path(), Some(&dir)).unwrap_err();
|
||||
assert!(err.to_string().contains("is empty"), "{err}");
|
||||
fs::remove_dir_all(&dir).ok();
|
||||
}
|
||||
}
|
||||
+15
-1
@@ -26,6 +26,17 @@ pub fn resolve_any(
|
||||
flag_value: Option<&str>,
|
||||
env_vars: &[&str],
|
||||
prompt_label: &str,
|
||||
) -> Result<String, Error> {
|
||||
resolve_any_or(flag_value, env_vars, None, prompt_label)
|
||||
}
|
||||
|
||||
/// Like [`resolve_any`], but a non-empty `fallback` (from a profile) is used
|
||||
/// when the flag and the variables give nothing, before the prompt.
|
||||
pub fn resolve_any_or(
|
||||
flag_value: Option<&str>,
|
||||
env_vars: &[&str],
|
||||
fallback: Option<&str>,
|
||||
prompt_label: &str,
|
||||
) -> Result<String, Error> {
|
||||
if let Some(value) = flag_value {
|
||||
return Ok(value.to_owned());
|
||||
@@ -33,11 +44,14 @@ pub fn resolve_any(
|
||||
if let Some(value) = from_env(env_vars)? {
|
||||
return Ok(value);
|
||||
}
|
||||
if let Some(value) = fallback.filter(|v| !v.is_empty()) {
|
||||
return Ok(value.to_owned());
|
||||
}
|
||||
if std::io::stdin().is_terminal() {
|
||||
return prompt_no_echo(prompt_label);
|
||||
}
|
||||
Err(Error::Usage(format!(
|
||||
"no {prompt_label} provided: pass the flag, set ${0} or ${0}_FILE, or run on a TTY for an interactive prompt",
|
||||
"no {prompt_label} provided: pass the flag, set ${0} or ${0}_FILE, name one in a profile, or run on a TTY for an interactive prompt",
|
||||
env_vars[0]
|
||||
)))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user