Skip to content

Commit

Permalink
feat: adds rover docs open and rover docs list
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Feb 25, 2021
1 parent a8c8195 commit b6cb4e9
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ git2 = "0.13.17"
git-url-parse = "0.3.1"
heck = "0.3.2"
humantime = "2.1.0"
os_info = "3.0"
prettytable-rs = "0.8.0"
serde = "1.0"
serde_json = "1.0"
structopt = "0.3.21"
tracing = "0.1.22"
regex = "1"
url = "2.2.0"
os_info = "3.0"
webbrowser = "0.5.5"

[dev-dependencies]
assert_cmd = "1.0.1"
Expand Down
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,14 @@ pub enum Command {
/// Federated schema/graph commands
Subgraph(command::Subgraph),

/// Interact with documentation
Docs(command::Docs),

/// Installs the current executable
#[structopt(setting(structopt::clap::AppSettings::Hidden))]
Install(command::Install),

/// Get system information
#[structopt(setting(structopt::clap::AppSettings::Hidden))]
Info(command::Info),
}
Expand All @@ -103,6 +108,7 @@ impl Rover {
Command::Config(command) => {
command.run(self.get_rover_config()?, self.get_client_config()?)
}
Command::Docs(command) => command.run(),
Command::Graph(command) => {
command.run(self.get_client_config()?, self.get_git_context()?)
}
Expand Down
17 changes: 17 additions & 0 deletions src/command/docs/list.rs
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(),
))
}
}
32 changes: 32 additions & 0 deletions src/command/docs/mod.rs
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(),
}
}
}
20 changes: 20 additions & 0 deletions src/command/docs/open.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::{command::RoverStdout, Result};

use super::shortlinks;

use serde::Serialize;
use structopt::StructOpt;

#[derive(Debug, Serialize, StructOpt)]
pub struct Open {
#[structopt(name = "slug", possible_values = &shortlinks::possible_shortlinks())]
shortlink: String,
}

impl Open {
pub fn run(&self) -> Result<RoverStdout> {
let url = format!("https://go.apollo.dev/r/{}", &self.shortlink);
webbrowser::open(&url)?;
Ok(RoverStdout::None)
}
}
26 changes: 26 additions & 0 deletions src/command/docs/shortlinks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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
}

mod tests {
#[test]
fn can_make_shortlink_vec_from_map() {
let shortlinks = super::possible_shortlinks();
assert!(!shortlinks.is_empty())
}
}
2 changes: 2 additions & 0 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod config;
mod docs;
mod graph;
mod info;
mod install;
mod output;
mod subgraph;

pub use config::Config;
pub use docs::Docs;
pub use graph::Graph;
pub use info::Info;
pub use install::Install;
Expand Down
15 changes: 15 additions & 0 deletions src/command/output.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;
use std::fmt::Debug;

use ansi_term::Colour::Yellow;
use prettytable::{cell, row, Table};
use rover_client::query::subgraph::list::ListDetails;

Expand All @@ -13,6 +15,7 @@ use rover_client::query::subgraph::list::ListDetails;
/// return something that is not described well in this enum, it should be added.
#[derive(Clone, PartialEq, Debug)]
pub enum RoverStdout {
DocsList(HashMap<&'static str, &'static str>),
SDL(String),
SchemaHash(String),
SubgraphList(ListDetails),
Expand All @@ -23,6 +26,18 @@ pub enum RoverStdout {
impl RoverStdout {
pub fn print(&self) {
match self {
RoverStdout::DocsList(shortlinks) => {
eprintln!(
"You can open any of these documentation pages by running {}.\n",
Yellow.normal().paint("rover docs open <slug>")
);
let mut table = Table::new();
table.add_row(row!["Slug", "Description"]);
for (shortlink_slug, shortlink_description) in shortlinks {
table.add_row(row![shortlink_slug, shortlink_description]);
}
println!("{}", table);
}
RoverStdout::SDL(sdl) => {
eprintln!("SDL:");
println!("{}", &sdl);
Expand Down

0 comments on commit b6cb4e9

Please sign in to comment.