add sfdx_auth_url setting
This commit is contained in:
+164
-114
@@ -1,122 +1,124 @@
|
||||
use crate::logger;
|
||||
use anyhow::{Context, Result};
|
||||
use clap::{Parser, ValueEnum};
|
||||
use log::error;
|
||||
use clap::{CommandFactory, Parser, ValueEnum};
|
||||
use log::{error, info};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Cli {
|
||||
/// Number of seconds to wait
|
||||
#[arg(short = 'w', long, default_value_t = 20, env = "IMOJEFS_WAIT")]
|
||||
pub wait: u64,
|
||||
/// Number of seconds to wait
|
||||
#[arg(short = 'w', long, default_value_t = 20, env = "IMOJEFS_WAIT")]
|
||||
pub wait: u64,
|
||||
|
||||
/// The logging level
|
||||
#[arg(short = 'l', long, value_enum, default_value_t = LogLevel::Info, env = "IMOJEFS_LOG_LEVEL")]
|
||||
pub log_level: LogLevel,
|
||||
/// The logging level
|
||||
#[arg(short = 'l', long, value_enum, default_value_t = LogLevel::Info, env = "IMOJEFS_LOG_LEVEL"
|
||||
)]
|
||||
pub log_level: LogLevel,
|
||||
|
||||
/// The configuration to use
|
||||
#[arg(short = 'c', long, default_value_t = String::from("imoje"))]
|
||||
config: String,
|
||||
/// The configuration to use
|
||||
#[arg(short = 'c', long, default_value_t = String::from("imoje"))]
|
||||
config: String,
|
||||
|
||||
#[command(subcommand)]
|
||||
command: Option<Commands>,
|
||||
#[command(subcommand)]
|
||||
command: Option<Commands>,
|
||||
}
|
||||
|
||||
#[derive(clap::Subcommand, Debug)]
|
||||
enum Commands {
|
||||
/// Controls testing features
|
||||
FileSync,
|
||||
/// Controls testing features
|
||||
FileSync,
|
||||
|
||||
/// Change Settings
|
||||
Set {
|
||||
#[command(subcommand)]
|
||||
command: Option<SettingsCommands>,
|
||||
},
|
||||
/// Change Settings
|
||||
Set {
|
||||
#[command(subcommand)]
|
||||
command: Option<SettingsCommands>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(clap::Subcommand, Debug)]
|
||||
enum SettingsCommands {
|
||||
/// Set the Salesforce Authentication Url
|
||||
SfdxAuthUrl {
|
||||
/// Salesforce Authentication Url
|
||||
#[arg(
|
||||
short = 'x',
|
||||
long,
|
||||
env = "IMOJEEH_SFDX_AUTH_URL",
|
||||
hide_env_values = true
|
||||
)]
|
||||
url: String,
|
||||
},
|
||||
|
||||
/// Set the Salesforce Authentication Url
|
||||
SfdxAuthUrl {
|
||||
/// Salesforce Authentication Url
|
||||
#[arg(
|
||||
short = 'x',
|
||||
long,
|
||||
env = "IMOJEFS_SFDX_AUTH_URL",
|
||||
hide_env_values = true
|
||||
)]
|
||||
url: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
pub struct Args {
|
||||
/// AWS Bucket name
|
||||
#[arg(short = 'b', long, env = "IMOJEFS_S3_BUCKET")]
|
||||
pub s3_bucket: String,
|
||||
/// AWS Bucket name
|
||||
#[arg(short = 'b', long, env = "IMOJEFS_S3_BUCKET")]
|
||||
pub s3_bucket: String,
|
||||
|
||||
/// AWS Access Key Id
|
||||
#[arg(short = 'k', long, env = "IMOJEFS_S3_CLIENT_ID")]
|
||||
pub s3_client_id: String,
|
||||
/// AWS Access Key Id
|
||||
#[arg(short = 'k', long, env = "IMOJEFS_S3_CLIENT_ID")]
|
||||
pub s3_client_id: String,
|
||||
|
||||
/// AWS Secret Access Key
|
||||
#[arg(
|
||||
short = 's',
|
||||
long,
|
||||
env = "IMOJEFS_S3_CLIENT_SECRET",
|
||||
hide_env_values = true
|
||||
)]
|
||||
pub s3_client_secret: String,
|
||||
/// AWS Secret Access Key
|
||||
#[arg(
|
||||
short = 's',
|
||||
long,
|
||||
env = "IMOJEFS_S3_CLIENT_SECRET",
|
||||
hide_env_values = true
|
||||
)]
|
||||
pub s3_client_secret: String,
|
||||
|
||||
/// AWS Endpoint URL, [default: AWS url]
|
||||
#[arg(short = 'e', long, env = "IMOJEFS_S3_ENDPOINT")]
|
||||
pub s3_endpoint: Option<String>,
|
||||
/// AWS Endpoint URL, [default: AWS url]
|
||||
#[arg(short = 'e', long, env = "IMOJEFS_S3_ENDPOINT")]
|
||||
pub s3_endpoint: Option<String>,
|
||||
|
||||
/// AWS Region
|
||||
#[arg(
|
||||
short = 'r',
|
||||
long,
|
||||
default_value = "us-east-1",
|
||||
env = "IMOJEFS_S3_REGION"
|
||||
)]
|
||||
pub s3_region: String,
|
||||
/// AWS Region
|
||||
#[arg(
|
||||
short = 'r',
|
||||
long,
|
||||
default_value = "us-east-1",
|
||||
env = "IMOJEFS_S3_REGION"
|
||||
)]
|
||||
pub s3_region: String,
|
||||
|
||||
/// Salesforce Authentication Url
|
||||
#[arg(
|
||||
short = 'x',
|
||||
long,
|
||||
env = "IMOJEFS_SFDX_AUTH_URL",
|
||||
hide_env_values = true
|
||||
)]
|
||||
pub sfdx_auth_url: String,
|
||||
/// Salesforce Authentication Url
|
||||
#[arg(
|
||||
short = 'x',
|
||||
long,
|
||||
env = "IMOJEFS_SFDX_AUTH_URL",
|
||||
hide_env_values = true
|
||||
)]
|
||||
pub sfdx_auth_url: String,
|
||||
|
||||
/// Retry failures after X runs
|
||||
#[arg(long, default_value_t = 180, env = "IMOJEFS_RETRY")]
|
||||
pub retry: u64,
|
||||
/// Retry failures after X runs
|
||||
#[arg(long, default_value_t = 180, env = "IMOJEFS_RETRY")]
|
||||
pub retry: u64,
|
||||
|
||||
/// A comma separated list of fallback email address,
|
||||
/// used when Salesforce cannot be reached to deliver log messages
|
||||
#[arg(short = 'a', long, env = "IMOJEFS_ADMIN_EMAIL")]
|
||||
pub admin_email: Option<String>,
|
||||
/// A comma separated list of fallback email address,
|
||||
/// used when Salesforce cannot be reached to deliver log messages
|
||||
#[arg(short = 'a', long, env = "IMOJEFS_ADMIN_EMAIL")]
|
||||
pub admin_email: Option<String>,
|
||||
}
|
||||
|
||||
impl Args {
|
||||
pub(crate) fn s3_bucket(&self) -> &str {
|
||||
&self.s3_bucket
|
||||
}
|
||||
pub(crate) fn s3_client_id(&self) -> &str {
|
||||
&self.s3_client_id
|
||||
}
|
||||
pub(crate) fn s3_client_secret(&self) -> &str {
|
||||
&self.s3_client_secret
|
||||
}
|
||||
pub(crate) fn s3_endpoint(&self) -> Option<&str> {
|
||||
self.s3_endpoint.as_deref()
|
||||
}
|
||||
pub(crate) fn s3_region(&self) -> &str {
|
||||
&self.s3_region
|
||||
}
|
||||
pub(crate) fn s3_bucket(&self) -> &str {
|
||||
&self.s3_bucket
|
||||
}
|
||||
pub(crate) fn s3_client_id(&self) -> &str {
|
||||
&self.s3_client_id
|
||||
}
|
||||
pub(crate) fn s3_client_secret(&self) -> &str {
|
||||
&self.s3_client_secret
|
||||
}
|
||||
pub(crate) fn s3_endpoint(&self) -> Option<&str> {
|
||||
self.s3_endpoint.as_deref()
|
||||
}
|
||||
pub(crate) fn s3_region(&self) -> &str {
|
||||
&self.s3_region
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the different levels of logging that can be used in an application.
|
||||
@@ -131,39 +133,87 @@ impl Args {
|
||||
/// as well as `Clone` and `Debug` for efficient copying and debugging purposes.
|
||||
#[derive(ValueEnum, Clone, Debug)]
|
||||
pub enum LogLevel {
|
||||
Debug,
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
Trace,
|
||||
Debug,
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
Trace,
|
||||
}
|
||||
|
||||
fn load_configuration_file(config_file: &str) -> Result<()> {
|
||||
let app_config =
|
||||
crate::config::load(config_file).context("Failed to load application configuration")?;
|
||||
crate::APP_CONFIG
|
||||
.set(app_config)
|
||||
.map_err(|_| anyhow::anyhow!("Application configuration has already been initialized"))?;
|
||||
Ok(())
|
||||
let app_config =
|
||||
crate::config::load(config_file).context("Failed to load application configuration")?;
|
||||
crate::APP_CONFIG
|
||||
.set(app_config)
|
||||
.map_err(|_| anyhow::anyhow!("Application configuration has already been initialized"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn set_sfdx_auth_url(config_file: &str, url: String) -> Result<()> {
|
||||
let mut app_config =
|
||||
crate::config::load(config_file).context("Failed to load application configuration")?;
|
||||
|
||||
app_config.sfdx_auth_url = Some(url);
|
||||
|
||||
crate::config::save(config_file, &app_config)
|
||||
.context("Failed to save Salesforce authentication URL")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn run() {
|
||||
let cli = Cli::parse();
|
||||
let cli = Cli::parse();
|
||||
|
||||
logger::init(&cli.log_level);
|
||||
match load_configuration_file(&cli.config) {
|
||||
Ok(()) => {}
|
||||
Err(e) => {
|
||||
error!("Error loading configuration file: {:#}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
match cli.command {
|
||||
Some(subcommand) => match subcommand {
|
||||
Commands::FileSync { .. } => {}
|
||||
Commands::Set { .. } => {}
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
match &cli.command {
|
||||
Some(command) => match command {
|
||||
Commands::Set { command } => {
|
||||
run_set_command(&cli, command);
|
||||
},
|
||||
Commands::FileSync { .. } => {
|
||||
run_file_sync(&cli);
|
||||
}
|
||||
},
|
||||
None => {
|
||||
println!("Expected subcommand, none given. Use --help for more information");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_file_sync(cli: &Cli) {
|
||||
logger::init(&cli.log_level);
|
||||
|
||||
match load_configuration_file(&cli.config) {
|
||||
Ok(()) => {}
|
||||
Err(e) => {
|
||||
error!("Error loading configuration file: {:#}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
crate::file_sync::run();
|
||||
}
|
||||
|
||||
fn run_set_command(cli: &Cli, commands: &Option<SettingsCommands>) {
|
||||
match commands {
|
||||
Some(command) => match command {
|
||||
SettingsCommands::SfdxAuthUrl { url } => {
|
||||
match set_sfdx_auth_url(&cli.config, url.clone()) {
|
||||
Ok(()) => {
|
||||
println!("Salesforce authentication URL saved successfully");
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Error saving Salesforce authentication URL: {:#}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
println!("Expected subcommand, none given. Use --help for more information");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-2
@@ -15,13 +15,13 @@ const NONCE_LEN: usize = 12;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AppConfig {
|
||||
pub dummy_setting: String,
|
||||
pub sfdx_auth_url: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for AppConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
dummy_setting: "dummy-value".to_string(),
|
||||
sfdx_auth_url: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,12 @@ pub fn load(config_file: &str) -> Result<AppConfig> {
|
||||
.with_context(|| format!("Unable to decrypt configuration file {}", config_path.display()))
|
||||
}
|
||||
|
||||
pub fn save(config_file: &str, config: &AppConfig) -> Result<()> {
|
||||
let config_path = config_path(config_file)?;
|
||||
save_encrypted(&config_path, config)
|
||||
.with_context(|| format!("Unable to save configuration file {}", config_path.display()))
|
||||
}
|
||||
|
||||
fn config_path(config_file: &str) -> Result<PathBuf> {
|
||||
let home_dir = dirs::home_dir().ok_or_else(|| anyhow!("Unable to determine home directory"))?;
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
pub fn run() {
|
||||
unimplemented!("File sync not yet implemented");
|
||||
}
|
||||
@@ -14,6 +14,8 @@ mod logger;
|
||||
mod salesforce;
|
||||
mod terminator;
|
||||
|
||||
mod file_sync;
|
||||
|
||||
|
||||
pub static APP_CONFIG: OnceLock<config::AppConfig> = OnceLock::new();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user