use crate::cmd::utils::Cmd;
use crate::constants::CLI_PATH;
use eyre::ContextCompat;
use std::fs::File;
use std::io::prelude::Write;
use clap::Parser;
#[derive(Debug, Clone, Parser)]
pub struct InitArgs {
#[clap(short, long, help = "Private key to add to .env")]
private_key: Option<String>,
#[clap(
short,
long,
help = "Trustblock auth token to add to .env",
long_help = "Trustblock auth token, which you can get by navigating to /api/auth/session. If supplied, it will be added automatically to .env"
)]
auth_token: Option<String>,
}
impl Cmd for InitArgs {
fn run(self) -> eyre::Result<()> {
let home_dir = dirs::home_dir().wrap_err("Could not find home directory")?;
let private_key = self.private_key.unwrap_or_default();
let auth_token = self.auth_token.unwrap_or_default();
let cli_dir = home_dir.join(CLI_PATH);
if !cli_dir.exists() {
std::fs::create_dir(&cli_dir)?;
}
let env_path = cli_dir.join(".env");
if !env_path.exists() {
let mut env_file = File::create(&env_path)?;
let env_data = format!("WALLET_KEY={private_key}\nAUTH_TOKEN={auth_token}");
env_file.write_all(env_data.as_bytes())?;
println!("Created .env file at {env_path:?}");
}
Ok(())
}
}