Browse Source

add list-templates command

master
Schneider 4 years ago
parent
commit
651a51b106
Signed by: schneider GPG Key ID: 3F50B02A50039F3B
  1. 16
      Cargo.lock
  2. 2
      Cargo.toml
  3. 9
      src/app.rs
  4. 3
      src/main.rs
  5. 47
      src/template.rs

16
Cargo.lock

@ -269,6 +269,8 @@ dependencies = [
"libc",
"log",
"reqwest",
"serde",
"serde_json",
"stderrlog",
"structopt",
]
@ -875,6 +877,20 @@ name = "serde"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"

2
Cargo.toml

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

9
src/app.rs

@ -15,6 +15,7 @@ use log::{debug, error, info, trace, warn};
use crate::errors::*;
use crate::gitignore::Gitignore;
use crate::helpers::{git_dir, BoilerplateOpts, HELP_TEMPLATE};
use crate::template::*;
/// The verbosity level when no `-q` or `-v` arguments are given, with `0` being `-q`
pub const DEFAULT_VERBOSITY: u64 = 1;
@ -100,16 +101,16 @@ fn run_get(lang: &str) -> Result<()> {
/// Runs the command `list-templates`
fn run_list_templates() -> Result<()> {
let client = Client::new();
let mut res = client
let 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);
let body: Vec<Template> = res.json().chain_err(|| "json")?;
body.iter().filter(|t| t.is_gitignore_template()).for_each(|t| println!("{}", t.pretty_name()));
Ok(())
}

3
src/main.rs

@ -24,8 +24,8 @@ This project used [rust-cli-boilerplate](https://github.com/ssokolow/rust-cli-bo
#[macro_use]
extern crate error_chain;
extern crate reqwest;
extern crate serde;
// stdlib imports
use std::convert::TryInto;
@ -40,6 +40,7 @@ use structopt::{clap, StructOpt};
mod app;
mod gitignore;
mod helpers;
mod template;
/// Boilerplate to parse command-line arguments, set up logging, and handle bubbled-up `Error`s.
///

47
src/template.rs

@ -0,0 +1,47 @@
//! This module contains structs for working with the github templates
use serde::{Deserialize, Serialize};
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Template {
pub name: String,
pub path: String,
pub sha: String,
pub size: i64,
pub url: String,
#[serde(rename = "html_url")]
pub html_url: String,
#[serde(rename = "git_url")]
pub git_url: String,
#[serde(rename = "download_url")]
pub download_url: Option<String>,
#[serde(rename = "type")]
pub type_field: String,
#[serde(rename = "_links")]
pub links: Links,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Links {
#[serde(rename = "self")]
pub self_field: String,
pub git: String,
pub html: String,
}
impl Template {
/// Checks if this template file is a gitignore template file
pub fn is_gitignore_template(&self) -> bool { self.name.ends_with(".gitignore") }
/// Returns the name of the file without the .gitignore ending
///
/// if `!self.is_gitignore_template()` the whole `self.name` is returned
pub fn pretty_name(&self) -> &str {
if let Some(dot_index) = self.name.rfind(".") {
return &self.name[0..dot_index];
}
return self.name.as_str();
}
}
Loading…
Cancel
Save