use std::env;
use std::path::PathBuf;
use clap::{Parser, Subcommand};
use clap::builder::Styles;
use lazy_static::lazy_static;
use crate::app::files::utils::expand_tilde;
use crate::panic_error;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None, styles = Styles::styled())]
pub struct Args {
#[arg(short, long)]
pub directory: Option<PathBuf>,
#[command(subcommand)]
pub command: Option<Command>,
#[arg(global = true, long, default_value_t = false)]
pub dry_run: bool,
}
#[derive(Subcommand, Debug, PartialEq)]
pub enum Command {
Import {
#[command(subcommand)]
import_type: ImportType,
},
}
#[derive(Subcommand, Debug, PartialEq)]
pub enum ImportType {
Postman {
import_path: PathBuf,
#[arg(long)]
max_depth: Option<u16>,
},
Curl {
import_path: PathBuf,
collection_name: String,
request_name: Option<String>,
#[arg(short, long, conflicts_with = "request_name")]
recursive: bool,
#[arg(long, requires = "recursive", conflicts_with = "request_name")]
max_depth: Option<u16>,
},
}
pub struct ParsedArgs {
pub directory: PathBuf,
#[allow(dead_code)]
pub is_directory_from_env: bool,
pub command: Option<Command>,
pub should_save: bool
}
lazy_static! {
pub static ref ARGS: ParsedArgs = {
let args = Args::parse();
let (directory, is_directory_from_env) = match args.directory {
Some(arg_directory) => (expand_tilde(arg_directory), false),
None => match env::var("ATAC_MAIN_DIR") {
Ok(env_directory) => (expand_tilde(PathBuf::from(env_directory)), true),
Err(_) => panic_error("No directory provided, provide one either with `--directory <dir>` or via the environment variable `ATAC_MAIN_DIR`")
}
};
ParsedArgs {
directory,
is_directory_from_env,
command: args.command,
should_save: !args.dry_run
}
};
}