-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds
rover docs open
and rover docs list
- Loading branch information
1 parent
8e382dd
commit 22f5dee
Showing
11 changed files
with
200 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use crate::{command::RoverStdout, Result}; | ||
|
||
use super::shortlinks; | ||
|
||
use serde::Serialize; | ||
use structopt::StructOpt; | ||
|
||
#[derive(Debug, Serialize, StructOpt)] | ||
pub struct List {} | ||
|
||
impl List { | ||
pub fn run(&self) -> Result<RoverStdout> { | ||
Ok(RoverStdout::DocsList( | ||
shortlinks::get_shortlinks_with_description(), | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
mod list; | ||
mod open; | ||
pub mod shortlinks; | ||
|
||
use serde::Serialize; | ||
use structopt::StructOpt; | ||
|
||
use crate::{command::RoverStdout, Result}; | ||
|
||
#[derive(Debug, Serialize, StructOpt)] | ||
pub struct Docs { | ||
#[structopt(subcommand)] | ||
command: Command, | ||
} | ||
|
||
#[derive(Debug, Serialize, StructOpt)] | ||
pub enum Command { | ||
/// List all available docs links | ||
List(list::List), | ||
|
||
/// Open a docs link | ||
Open(open::Open), | ||
} | ||
|
||
impl Docs { | ||
pub fn run(&self) -> Result<RoverStdout> { | ||
match &self.command { | ||
Command::List(command) => command.run(), | ||
Command::Open(command) => command.run(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::{anyhow, command::RoverStdout, Result}; | ||
|
||
use super::shortlinks; | ||
|
||
use ansi_term::Colour::{Cyan, Yellow}; | ||
use serde::Serialize; | ||
use structopt::StructOpt; | ||
|
||
use std::process::Command; | ||
|
||
#[derive(Debug, Serialize, StructOpt)] | ||
pub struct Open { | ||
#[structopt(name = "slug", default_value = "docs", possible_values = &shortlinks::possible_shortlinks())] | ||
slug: String, | ||
} | ||
|
||
impl Open { | ||
pub fn run(&self) -> Result<RoverStdout> { | ||
let url = shortlinks::get_url_from_slug(&self.slug); | ||
let yellow_browser_var = format!("{}", Yellow.normal().paint("$BROWSER")); | ||
let cyan_url = format!("{}", Cyan.normal().paint(&url)); | ||
if let Some(browser_override) = std::env::var_os("BROWSER") { | ||
eprintln!( | ||
"Opening {} with the application specified by {}.", | ||
&cyan_url, &yellow_browser_var | ||
); | ||
if let Err(e) = Command::new(&browser_override).arg(&url).status() { | ||
Err(anyhow!( | ||
"Couldn't open docs with {}: {}", | ||
browser_override.to_string_lossy(), | ||
e | ||
)) | ||
} else { | ||
Ok(()) | ||
} | ||
} else { | ||
eprintln!("Opening {} with your default browser. This can be overridden by setting the {} environment variable.", &cyan_url, &yellow_browser_var); | ||
webbrowser::open(&url)?; | ||
Ok(()) | ||
}?; | ||
Ok(RoverStdout::None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
pub const URL_BASE: &'static str = "https://go.apollo.dev/r"; | ||
|
||
use std::collections::HashMap; | ||
|
||
pub fn get_shortlinks_with_description() -> HashMap<&'static str, &'static str> { | ||
let mut links = HashMap::new(); | ||
links.insert("docs", "Rover's Documentation Homepage"); | ||
links.insert("api-keys", "Understanding Apollo's API Keys"); | ||
links.insert("contributing", "Contributing to Rover"); | ||
links.insert("start", "Getting Started with Rover"); | ||
links | ||
} | ||
|
||
pub fn possible_shortlinks() -> Vec<&'static str> { | ||
let mut res = Vec::new(); | ||
for (slug, _) in get_shortlinks_with_description() { | ||
res.push(slug); | ||
} | ||
res | ||
} | ||
|
||
pub fn get_url_from_slug(slug: &str) -> String { | ||
format!("{}/{}", URL_BASE, slug) | ||
} | ||
|
||
mod tests { | ||
#[test] | ||
fn can_make_shortlink_vec_from_map() { | ||
let shortlinks = super::possible_shortlinks(); | ||
assert!(!shortlinks.is_empty()) | ||
} | ||
|
||
#[test] | ||
fn can_get_url_from_slug() { | ||
let expected_link = "https://go.apollo.dev/r/start"; | ||
let actual_link = super::get_url_from_slug("start"); | ||
assert_eq!(expected_link, actual_link); | ||
} | ||
|
||
#[test] | ||
fn each_url_is_valid() { | ||
for link in super::possible_shortlinks() { | ||
let url = super::get_url_from_slug(link); | ||
assert!(reqwest::blocking::get(&url) | ||
.unwrap() | ||
.error_for_status() | ||
.is_ok()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters