cli_support/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#![allow(unknown_lints)]
6#![warn(rust_2018_idioms)]
7
8use std::{
9    path::{Path, PathBuf},
10    sync::Arc,
11};
12
13use remote_settings::{RemoteSettingsConfig2, RemoteSettingsService};
14
15pub mod fxa_creds;
16pub mod prompt;
17
18pub use env_logger;
19
20pub fn init_logging_with(s: &str) {
21    let noisy = "tokio_threadpool=warn,tokio_reactor=warn,tokio_core=warn,tokio=warn,hyper=warn,want=warn,mio=warn,reqwest=warn";
22    let spec = format!("{},{}", s, noisy);
23    env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", spec));
24}
25
26pub fn init_trace_logging() {
27    init_logging_with("trace")
28}
29
30pub fn init_logging() {
31    init_logging_with(if cfg!(debug_assertions) {
32        "debug"
33    } else {
34        "info"
35    })
36}
37
38pub fn cli_data_dir() -> String {
39    data_path(None).to_string_lossy().to_string()
40}
41
42pub fn ensure_cli_data_dir_exists() {
43    let dir = data_path(None);
44    if !dir.exists() {
45        std::fs::create_dir(&dir).unwrap_or_else(|_| panic!("Error creating dir: {dir:?}"))
46    }
47}
48
49pub fn cli_data_subdir(relative_path: &str) -> String {
50    data_path(Some(relative_path)).to_string_lossy().to_string()
51}
52
53pub fn cli_data_path(filename: &str) -> String {
54    data_path(None).join(filename).to_string_lossy().to_string()
55}
56
57fn data_path(relative_path: Option<&str>) -> PathBuf {
58    let dir = workspace_root_dir().join(".cli-data");
59    match relative_path {
60        None => dir,
61        Some(relative_path) => dir.join(relative_path),
62    }
63}
64
65pub fn workspace_root_dir() -> PathBuf {
66    let cargo_output = std::process::Command::new(env!("CARGO"))
67        .arg("locate-project")
68        .arg("--workspace")
69        .arg("--message-format=plain")
70        .output()
71        .unwrap()
72        .stdout;
73    let cargo_toml_path = Path::new(std::str::from_utf8(&cargo_output).unwrap().trim());
74    cargo_toml_path.parent().unwrap().to_path_buf()
75}
76
77pub fn remote_settings_service() -> Arc<RemoteSettingsService> {
78    Arc::new(RemoteSettingsService::new(
79        data_path(Some("remote-settings"))
80            .to_string_lossy()
81            .to_string(),
82        RemoteSettingsConfig2::default(),
83    ))
84}