Browse Source

start with list-templates command

master
Schneider 5 years ago
parent
commit
4817426947
Signed by: schneider GPG Key ID: 3F50B02A50039F3B
  1. 947
      Cargo.lock
  2. 1
      Cargo.toml
  3. 22
      src/app.rs
  4. 2
      src/main.rs

947
Cargo.lock
File diff suppressed because it is too large
View File

1
Cargo.toml

@ -8,6 +8,7 @@ edition = "2018"
log = "0.4" log = "0.4"
stderrlog = "0.4" stderrlog = "0.4"
structopt = "0.3" structopt = "0.3"
reqwest = { version = "0.10", features = ["blocking", "json"] }
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
libc = "0.2" libc = "0.2"

22
src/app.rs

@ -5,6 +5,8 @@
use std::path::PathBuf; use std::path::PathBuf;
// 3rd-party crate imports // 3rd-party crate imports
use reqwest::blocking::Client;
use reqwest::header::{ACCEPT, CONTENT_TYPE, USER_AGENT};
use structopt::StructOpt; use structopt::StructOpt;
use log::{debug, error, info, trace, warn}; use log::{debug, error, info, trace, warn};
@ -16,6 +18,7 @@ use crate::helpers::{git_dir, BoilerplateOpts, HELP_TEMPLATE};
/// The verbosity level when no `-q` or `-v` arguments are given, with `0` being `-q` /// The verbosity level when no `-q` or `-v` arguments are given, with `0` being `-q`
pub const DEFAULT_VERBOSITY: u64 = 1; pub const DEFAULT_VERBOSITY: u64 = 1;
pub const DEFAULT_USER_AGENT: &str = "gitig";
/// Command-line argument schema /// Command-line argument schema
/// ///
@ -68,6 +71,8 @@ pub enum Command {
/// The language for which the gitignore should be downloaded /// The language for which the gitignore should be downloaded
lang: String, lang: String,
}, },
/// List all available templates that can be downloaded
ListTemplates,
} }
/// Runs the command `add` /// Runs the command `add`
@ -92,11 +97,28 @@ fn run_get(lang: &str) -> Result<()> {
unimplemented!(); unimplemented!();
} }
/// Runs the command `list-templates`
fn run_list_templates() -> Result<()> {
let client = Client::new();
let mut res = client
.get("https://api.github.com/repos/github/gitignore/contents//")
.header(ACCEPT, "application/jsonapplication/vnd.github.v3+json")
.header(CONTENT_TYPE, "application/json")
.header(USER_AGENT, format!("{} {}", DEFAULT_USER_AGENT, env!("CARGO_PKG_VERSION")))
.send()
.chain_err(|| "Error while sending request to query all templates")?;
println!("{:?}", &res);
let body = res.text().chain_err(|| "json")?;
println!("{:?}", &body);
Ok(())
}
/// The actual `main()` /// The actual `main()`
pub fn main(opts: CliOpts) -> Result<()> { pub fn main(opts: CliOpts) -> Result<()> {
match opts.cmd { match opts.cmd {
Command::Add { glob } => run_add(&glob)?, Command::Add { glob } => run_add(&glob)?,
Command::Get { lang } => run_get(&lang)?, Command::Get { lang } => run_get(&lang)?,
Command::ListTemplates => run_list_templates()?,
}; };
Ok(()) Ok(())

2
src/main.rs

@ -25,6 +25,8 @@ This project used [rust-cli-boilerplate](https://github.com/ssokolow/rust-cli-bo
#[macro_use] #[macro_use]
extern crate error_chain; extern crate error_chain;
extern crate reqwest;
// stdlib imports // stdlib imports
use std::convert::TryInto; use std::convert::TryInto;
use std::io; use std::io;

Loading…
Cancel
Save