Skip to content

Commit 63cdbf9

Browse files
committed
Put example CLI data in one place
This is another change that I thought of when trying to code up the example component. What if we put all the CLI databases in one place. That way, we don't need to keep adding stuff to the .gitignore file and it keeps things a bit tidier. Also, add code in `cli-support` to do this with some extra capabilities like finding the workspace root and creating directories if they don't exist. Changed some examples to use the new system, but not all of them. From what I could tell, things like `sync-pass` don't create their own database. Instead, they're intended to use an existing database file, maybe one that you copied using `adb`.
1 parent fcef8e6 commit 63cdbf9

File tree

10 files changed

+67
-26
lines changed

10 files changed

+67
-26
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
website/build
22
target
3-
credentials.json
3+
/.cli-data
44
logins.jwk
55
*-engine.json
66
*.db
@@ -74,9 +74,6 @@ fml.py
7474
*.dylib
7575
*.so
7676

77-
# Generated by remote-settings-cli
78-
remote-settings-data
79-
8077
# Build static website files
8178
automation/swift-components-docs/.build
8279
automation/swift-components-docs/docs

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/autofill-utils/src/autofill-utils.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,8 @@ fn update_i64(field_name: &str, field: i64) -> i64 {
4848
#[structopt(name = "autofill-utils", about = "Command-line utilities for autofill")]
4949
pub struct Opts {
5050
/// Sets the path to the database
51-
#[structopt(
52-
name = "database_path",
53-
long,
54-
short = "d",
55-
default_value = "./autofill.db"
56-
)]
57-
pub database_path: String,
51+
#[structopt(name = "database_path", long, short = "d")]
52+
pub database_path: Option<String>,
5853

5954
/// Disables all logging (useful for performance evaluation)
6055
#[structopt(name = "no-logging", long)]
@@ -496,10 +491,13 @@ fn main() -> Result<()> {
496491
cli_support::init_trace_logging();
497492
}
498493

499-
let db_path = opts.database_path.clone();
500-
let store = Store::new(db_path)?;
494+
let db_path = opts
495+
.database_path
496+
.clone()
497+
.unwrap_or_else(|| cli_support::cli_data_path("autofill.db"));
498+
let store = Store::new(&db_path)?;
501499

502-
let key = get_encryption_key(&store, &opts.database_path, &opts)?;
500+
let key = get_encryption_key(&store, &db_path, &opts)?;
503501
log::trace!("Using encryption key {}", key);
504502

505503
match opts.cmd {

examples/cli-support/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#![allow(unknown_lints)]
66
#![warn(rust_2018_idioms)]
77

8+
use std::path::{Path, PathBuf};
9+
810
pub mod fxa_creds;
911
pub mod prompt;
1012

@@ -27,3 +29,37 @@ pub fn init_logging() {
2729
"info"
2830
})
2931
}
32+
33+
pub fn cli_data_dir() -> String {
34+
data_path(None).to_string_lossy().to_string()
35+
}
36+
37+
pub fn cli_data_subdir(relative_path: &str) -> String {
38+
data_path(Some(relative_path)).to_string_lossy().to_string()
39+
}
40+
41+
pub fn cli_data_path(filename: &str) -> String {
42+
data_path(None).join(filename).to_string_lossy().to_string()
43+
}
44+
45+
fn data_path(relative_path: Option<&str>) -> PathBuf {
46+
let dir = workspace_root_dir().join(".cli-data");
47+
let dir = match relative_path {
48+
None => dir,
49+
Some(relative_path) => dir.join(relative_path),
50+
};
51+
std::fs::create_dir_all(&dir).expect("Error creating dir {dir:?}");
52+
dir
53+
}
54+
55+
pub fn workspace_root_dir() -> PathBuf {
56+
let cargo_output = std::process::Command::new(env!("CARGO"))
57+
.arg("locate-project")
58+
.arg("--workspace")
59+
.arg("--message-format=plain")
60+
.output()
61+
.unwrap()
62+
.stdout;
63+
let cargo_toml_path = Path::new(std::str::from_utf8(&cargo_output).unwrap().trim());
64+
cargo_toml_path.parent().unwrap().to_path_buf()
65+
}

examples/fxa-client/src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use clap::{Parser, Subcommand, ValueEnum};
1111
use cli_support::fxa_creds;
1212
use fxa_client::{FirefoxAccount, FxaConfig, FxaServer};
1313

14-
static CREDENTIALS_PATH: &str = "credentials.json";
14+
static CREDENTIALS_FILENAME: &str = "credentials.json";
1515
static CLIENT_ID: &str = "a2270f727f45f648";
1616
static REDIRECT_URI: &str = "https://accounts.firefox.com/oauth/success/a2270f727f45f648";
1717

@@ -111,10 +111,14 @@ fn load_account(cli: &Cli, scopes: &[&str]) -> Result<FirefoxAccount> {
111111
client_id: CLIENT_ID.into(),
112112
token_server_url_override: None,
113113
};
114-
fxa_creds::get_cli_fxa(config, CREDENTIALS_PATH, scopes).map(|cli| cli.account)
114+
fxa_creds::get_cli_fxa(config, &credentials_path(), scopes).map(|cli| cli.account)
115115
}
116116

117117
pub fn persist_fxa_state(acct: &FirefoxAccount) -> Result<()> {
118118
let json = acct.to_json().unwrap();
119-
Ok(fs::write(CREDENTIALS_PATH, json)?)
119+
Ok(fs::write(credentials_path(), json)?)
120+
}
121+
122+
fn credentials_path() -> String {
123+
cli_support::cli_data_path(CREDENTIALS_FILENAME)
120124
}

examples/relevancy-cli/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use sync15::engine::SyncEngineId;
1515

1616
use anyhow::{bail, Result};
1717

18-
static CREDENTIALS_PATH: &str = "credentials.json";
18+
static CREDENTIALS_PATH: &str = ".cli-data/credentials.json";
1919

2020
#[derive(Parser)]
2121
#[command(about, long_about = None)]
@@ -34,6 +34,9 @@ struct Cli {
3434
fn main() -> Result<()> {
3535
let cli = Cli::parse();
3636
viaduct_reqwest::use_reqwest_backend();
37+
if let Some(dir) = std::path::PathBuf::from(CREDENTIALS_PATH).parent() {
38+
std::fs::create_dir_all(dir)?;
39+
}
3740
let mut builder = Builder::new();
3841
builder.filter_level(log::LevelFilter::Info);
3942
if cli.verbose {

examples/remote-settings-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ name = "dump"
1010
path = "src/dump/lib.rs"
1111

1212
[dependencies]
13+
cli-support = { path = "../cli-support" }
1314
remote_settings = { features = ["signatures"], path = "../../components/remote_settings" }
1415
viaduct-reqwest = { path = "../../components/support/viaduct-reqwest" }
1516
log = "0.4"

examples/remote-settings-cli/src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,11 @@ fn build_service(cli: &Cli) -> Result<RemoteSettingsService> {
117117
}),
118118
bucket_name: cli.bucket.clone(),
119119
};
120-
Ok(RemoteSettingsService::new(
121-
cli.storage_dir
122-
.clone()
123-
.unwrap_or_else(|| "remote-settings-data".into()),
124-
config,
125-
)?)
120+
let storage_dir = cli
121+
.storage_dir
122+
.clone()
123+
.unwrap_or_else(|| cli_support::cli_data_subdir("remote-settings-data"));
124+
Ok(RemoteSettingsService::new(storage_dir, config)?)
126125
}
127126

128127
fn sync(service: RemoteSettingsService, collections: Vec<String>) -> Result<()> {

examples/suggest-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
publish = false
77

88
[dependencies]
9+
cli-support = { path = "../cli-support" }
910
remote_settings = { path = "../../components/remote_settings" }
1011
viaduct-reqwest = { path = "../../components/support/viaduct-reqwest" }
1112
log = "0.4"

examples/suggest-cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use suggest::{
1313
SuggestionProvider, SuggestionProviderConstraints, SuggestionQuery,
1414
};
1515

16-
static DB_PATH: &str = "suggest.db";
16+
static DB_FILENAME: &str = "suggest.db";
1717

1818
const DEFAULT_LOG_FILTER: &str = "suggest::store=info";
1919
const DEFAULT_LOG_FILTER_VERBOSE: &str = "suggest::store=trace";
@@ -146,7 +146,7 @@ fn main() -> Result<()> {
146146

147147
fn build_store(cli: &Cli) -> Arc<SuggestStore> {
148148
Arc::new(SuggestStoreBuilder::default())
149-
.data_path(DB_PATH.to_string())
149+
.data_path(cli_support::cli_data_path(DB_FILENAME))
150150
.remote_settings_server(match cli.remote_settings_server {
151151
None => RemoteSettingsServer::Prod,
152152
Some(RemoteSettingsServerArg::Dev) => RemoteSettingsServer::Dev,

0 commit comments

Comments
 (0)