main.rs (2540B)
1 use clap::{crate_version, App, Arg, SubCommand}; 2 3 use std::path::Path; 4 5 mod config; 6 mod entry; 7 mod follow; 8 mod timeline; 9 mod tweet; 10 use crate::config::Config; 11 12 fn main() { 13 let command = App::new("twixter") 14 .version(crate_version!()) 15 .about("A client for twtxt, microblog for hackers") 16 .arg( 17 Arg::with_name("config_dir") 18 .short("c") 19 .long("config") 20 .value_name("PATH") 21 .help("Specifies a custom config file location") 22 .takes_value(true), 23 ) 24 .subcommand( 25 SubCommand::with_name("tweet") 26 .version(crate_version!()) 27 .about("Append a new tweet to your twtxt file") 28 .arg(Arg::with_name("content").multiple(true)), 29 ) 30 .subcommand(SubCommand::with_name("timeline").about("Retrieves your timeline")) 31 .subcommand( 32 SubCommand::with_name("follow") 33 .version(crate_version!()) 34 .about("Adds a new source to your followings") 35 .arg( 36 Arg::with_name("nick") 37 .required(true) 38 .value_name("NICK") 39 .help("Specifies nickname to store source with"), 40 ) 41 .arg( 42 Arg::with_name("url") 43 .required(true) 44 .value_name("URL") 45 .help("Specifies source url"), 46 ), 47 ) 48 .get_matches(); 49 50 // Source config. 51 let mut config_dir = command 52 .args 53 .get("config_dir") 54 .and_then(|matched_arg| Some(Path::new(&matched_arg.vals[0]).to_path_buf())) 55 .unwrap_or({ 56 let mut config_dir = dirs::config_dir().unwrap(); 57 config_dir.push("twixter"); 58 config_dir 59 }); 60 config_dir.push("config"); 61 62 let config = Config::new(&config_dir); 63 64 // Check if twtfile exists and create one if necessary. 65 let twtfile_path = Path::new(&config.twtfile).parent().unwrap(); 66 if !twtfile_path.exists() { 67 std::fs::create_dir_all(twtfile_path).unwrap(); 68 } 69 70 // Parse subcommands. 71 match command.subcommand() { 72 ("tweet", Some(subcommand)) => tweet::tweet(&config, &subcommand), 73 ("timeline", Some(subcommand)) => timeline::timeline(&config, &subcommand), 74 ("follow", Some(subcommand)) => follow::follow(&config, &subcommand, &config_dir), 75 _ => {} 76 } 77 }