summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorSam Tay <sam.chong.tay@gmail.com>2020-06-05 21:29:35 -0700
committerSam Tay <sam.chong.tay@gmail.com>2020-06-06 19:56:12 -0700
commitefb2e0908b7f71a3f6ee6678c423c72b105f99ab (patch)
treec3bf0a25ca9fe0a201adb567024a7ba1deb3ccdc /src/main.rs
parentb06f305db319b90ff55e159a8538bac853ca2168 (diff)
Implement --list-sites and --update-sites
and validation on supplied site argument.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs40
1 files changed, 31 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index f6f4399..d5568cf 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,22 +6,44 @@ use config::Config;
use stackexchange::{LocalStorage, StackExchange};
fn main() {
- let config = config::user_config();
- let matches = cli::mk_app(&config).get_matches();
+ // TODO wrap inner function with Result<(), ErrorMessage>, propagate, print to stderr at the top level.
+ let opts = cli::get_opts();
+ let config = opts.config;
+ let site = &config.site;
+ let mut ls = LocalStorage::new();
- if matches.is_present("update-sites") {
- LocalStorage::new().update_sites();
+ if opts.update_sites {
+ ls.update_sites();
+ }
+
+ if opts.list_sites {
+ for s in ls.sites() {
+ println!("{}: {}", s.api_site_parameter, s.site_url);
+ }
+ return;
+ }
+
+ // TODO make this validation optional
+ if !ls.validate_site(site) {
+ // TODO tooling for printing to stderr with color, etc.
+ println!(
+ "{} is not a valid StackExchange site. If you think this
+ is in error, try running `so --update-sites` to update
+ the cached site listing. Run `so --list-sites` for all
+ available sites.",
+ site
+ );
+ return;
}
- // TODO merge config from ArgMatch
let se = StackExchange::new(Config {
- api_key: Some(String::from("8o9g7WcfwnwbB*Qp4VsGsw((")),
- limit: 1,
- site: String::from("stackoverflow"),
+ api_key: Some(String::from("8o9g7WcfwnwbB*Qp4VsGsw((")), // TODO stash this
+ ..config
});
+ let query = opts.query;
(|| -> Option<_> {
- let q = cli::get_query(matches)?;
+ let q = query?;
let que = se.search(&q).unwrap(); // TODO eventually be graceful
let ans = que.first()?.answers.first()?;
println!("{}", ans.body);