use crate::{
get_executable_directory,
program_info::{PROGRAM_AUTHORS, PROGRAM_DESCRIPTION, PROGRAM_NAME},
weather::{api_setup, check, search_city},
};
use clap::{Parser, Subcommand};
const ABOUT: &str = "# weather-cli : Weather for command-line fans!";
#[derive(Parser)]
#[command(author, version, about = ABOUT, long_about = None)]
struct Cli {
name: Option<String>,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Check {},
SetLocation {
#[arg(short, long)]
query: String,
},
ApiSetup {
#[arg(short, long)]
key: String,
},
About {},
}
pub async fn init() {
let cli = Cli::parse();
match &cli.command {
Some(Commands::Check {}) => {
match check().await {
Ok(()) => {}
Err(e) => {
println!("ERROR: {}", e);
if e.to_string().contains("No such file or directory") {
println!("ERROR: Try checking if \"settings.json\" exists. If not, you can create one with \"set-location\" command.");
}
}
};
}
Some(Commands::SetLocation { query }) => {
search_city(query).await.unwrap();
}
Some(Commands::ApiSetup { key }) => {
api_setup(key.to_string()).unwrap();
}
Some(Commands::About {}) => {
let splited_author_list: Vec<&str> = PROGRAM_AUTHORS.split(',').collect();
let mut authors = String::new();
for (index, one) in splited_author_list.into_iter().enumerate() {
if index == 0 {
authors += one.trim();
} else {
authors = authors + ", " + one.trim();
}
}
println!("# {}:", PROGRAM_NAME);
println!("{}\n", PROGRAM_DESCRIPTION);
println!("Developed by: {}", authors);
}
None => {
println!("Please use \"weather-cli help\" command for help.");
let executable_directory = get_executable_directory().unwrap();
println!("Program Executable Directory: {}", executable_directory);
}
}
}