1use clap::Parser;
2
3use crate::{
4 api_usage::{print_weather_information, search_city},
5 get_executable_directory,
6 program_info::ABOUT,
7 user_setup::setup_api,
8};
9
10#[derive(clap::Parser)]
11#[command(author, version, about = ABOUT, long_about = None)]
12struct Cli {
13 #[command(subcommand)]
14 command: Option<Commands>,
15}
16
17#[derive(clap::Subcommand)]
18enum Commands {
19 Check {},
21
22 SetLocation {
24 #[arg(short, long)]
26 query: String,
27 },
28
29 SetupApi {
32 #[arg(short, long)]
34 key: String,
35 },
36
37 About {},
39}
40
41pub async fn init() {
42 let cli = Cli::parse();
43
44 match &cli.command {
45 Some(Commands::Check {}) => {
46 match print_weather_information().await {
47 Ok(()) => {}
48 Err(e) => {
49 println!("ERROR: {}", e);
50 }
51 };
52 }
53 Some(Commands::SetLocation { query }) => {
54 search_city(query).await.unwrap_or_else(|e| {
55 println!("ERROR: {}", e);
56 });
57 }
58 Some(Commands::SetupApi { key }) => {
59 setup_api(key.to_string()).unwrap_or_else(|e| {
60 println!("ERROR: {}", e);
61 });
62 }
63 Some(Commands::About {}) => {
64 use crate::program_info::{
65 CRATES_IO_URL, PROGRAM_AUTHORS, PROGRAM_DESCRIPTION, PROGRAM_NAME, REPOSITORY_URL,
66 };
67
68 let splitted_author_list: Vec<&str> = PROGRAM_AUTHORS.split(',').collect();
69
70 let mut authors = String::new();
71 for (index, one) in splitted_author_list.into_iter().enumerate() {
72 if index == 0 {
73 authors += one.trim();
74 } else {
75 authors = authors + ", " + one.trim();
76 }
77 }
78
79 println!("# {}", PROGRAM_NAME);
80 println!("{}\n", PROGRAM_DESCRIPTION);
81 println!("Developed by: {}", authors);
82 println!("- crates.io: {}", CRATES_IO_URL);
83 println!("- Github: {}", REPOSITORY_URL);
84 }
85 None => {
86 println!("Please use \"weather-cli help\" command for help.");
87
88 let executable_directory = get_executable_directory().unwrap();
89 println!("- Program Executable Directory: {}", executable_directory);
90 }
91 }
92}