Skip to content

Commit

Permalink
rename SchemaLocation -> SchemaSource
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeDawkins committed Dec 4, 2020
1 parent 5dc753a commit 631a887
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/command/graph/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use rover_client::query::schema::push;
use crate::client::get_studio_client;
use crate::command::RoverStdout;
use crate::utils::loaders::load_schema_from_flag;
use crate::utils::parsers::{parse_schema_location, SchemaLocation};
use crate::utils::parsers::{parse_schema_source, SchemaSource};

#[derive(Debug, Serialize, StructOpt)]
pub struct Push {
/// The schema file to push
/// Can pass `-` to use stdin instead of a file
#[structopt(long, short = "s", parse(try_from_str = parse_schema_location))]
#[structopt(long, short = "s", parse(try_from_str = parse_schema_source))]
#[serde(skip_serializing)]
schema: SchemaLocation,
schema: SchemaSource,

/// Name of graph variant in Apollo Studio to push to
#[structopt(long, default_value = "current")]
Expand Down
6 changes: 3 additions & 3 deletions src/command/subgraph/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use crate::client::get_studio_client;
use crate::command::RoverStdout;

use crate::utils::loaders::load_schema_from_flag;
use crate::utils::parsers::{parse_schema_location, SchemaLocation};
use crate::utils::parsers::{parse_schema_source, SchemaSource};

#[derive(Debug, Serialize, StructOpt)]
pub struct Push {
/// The schema file to push
/// Can pass `-` to use stdin instead of a file
#[structopt(long, short = "s", parse(try_from_str = parse_schema_location))]
#[structopt(long, short = "s", parse(try_from_str = parse_schema_source))]
#[serde(skip_serializing)]
schema: SchemaLocation,
schema: SchemaSource,

/// Name of graph variant in Apollo Studio to push to
#[structopt(long, default_value = "current")]
Expand Down
16 changes: 8 additions & 8 deletions src/utils/loaders.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use crate::utils::parsers::SchemaLocation;
use crate::utils::parsers::SchemaSource;
use anyhow::{Context, Result};
use std::io::Read;
use std::path::Path;

/// this fn takes 2 args: the first, an enum describing where to look to load
/// a schema - from stdin or a file's PathBuf, and the second, the reference to
/// stdin to load from, should it be needed.
pub fn load_schema_from_flag(loc: &SchemaLocation, mut stdin: impl Read) -> Result<String> {
pub fn load_schema_from_flag(loc: &SchemaSource, mut stdin: impl Read) -> Result<String> {
match loc {
SchemaLocation::Stdin => {
SchemaSource::Stdin => {
let mut buffer = String::new();
stdin
.read_to_string(&mut buffer)
.context("Failed while attempting to read SDL from stdin")?;
Ok(buffer)
}
SchemaLocation::File(path) => {
SchemaSource::File(path) => {
if Path::exists(&path) {
let contents = std::fs::read_to_string(path)?;
Ok(contents)
Expand All @@ -31,7 +31,7 @@ pub fn load_schema_from_flag(loc: &SchemaLocation, mut stdin: impl Read) -> Resu

#[cfg(test)]
mod tests {
use super::{load_schema_from_flag, SchemaLocation};
use super::{load_schema_from_flag, SchemaSource};
use assert_fs::prelude::*;
use std::path::PathBuf;

Expand All @@ -45,7 +45,7 @@ mod tests {
.unwrap();

let test_path = test_file.path().to_path_buf();
let loc = SchemaLocation::File(test_path);
let loc = SchemaSource::File(test_path);

let schema = load_schema_from_flag(&loc, std::io::stdin()).unwrap();
assert_eq!(schema, "type Query { hello: String! }".to_string());
Expand All @@ -54,7 +54,7 @@ mod tests {
#[test]
fn load_schema_from_flag_errs_on_bad_path() {
let empty_path = "./wow.graphql";
let loc = SchemaLocation::File(PathBuf::from(empty_path));
let loc = SchemaSource::File(PathBuf::from(empty_path));

let schema = load_schema_from_flag(&loc, std::io::stdin());
assert_eq!(schema.is_err(), true);
Expand All @@ -65,7 +65,7 @@ mod tests {
// input implements std::io::Read, so it should be a suitable
// replacement for stdin
let input = b"type Query { hello: String! }";
let loc = SchemaLocation::Stdin;
let loc = SchemaSource::Stdin;

let schema = load_schema_from_flag(&loc, &input[..]).unwrap();
assert_eq!(schema, std::str::from_utf8(input).unwrap());
Expand Down
19 changes: 9 additions & 10 deletions src/utils/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@ use anyhow::Result;
use std::path::PathBuf;

#[derive(Debug, PartialEq)]
pub enum SchemaLocation {
pub enum SchemaSource {
Stdin,
File(PathBuf),
}

// Stdin(Box<dyn std::io::Read>),
pub fn parse_schema_location(loc: &str) -> Result<SchemaLocation> {
pub fn parse_schema_source(loc: &str) -> Result<SchemaSource> {
if loc == "-" {
Ok(SchemaLocation::Stdin)
Ok(SchemaSource::Stdin)
} else if loc.is_empty() {
Err(anyhow::anyhow!(
"The path provided to find a schema is empty"
))
} else {
let path = PathBuf::from(loc);
Ok(SchemaLocation::File(path))
Ok(SchemaSource::File(path))
}
}

#[cfg(test)]
mod tests {
use super::{parse_schema_location, SchemaLocation};
use super::{parse_schema_source, SchemaSource};

#[test]
fn it_correctly_parses_stdin_flag() {
assert_eq!(parse_schema_location("-").unwrap(), SchemaLocation::Stdin);
assert_eq!(parse_schema_source("-").unwrap(), SchemaSource::Stdin);
}

#[test]
fn it_correctly_parses_path_option() {
let loc = parse_schema_location("./schema.graphql").unwrap();
let loc = parse_schema_source("./schema.graphql").unwrap();
match loc {
SchemaLocation::File(buf) => {
SchemaSource::File(buf) => {
assert_eq!(buf.to_str().unwrap(), "./schema.graphql");
}
_ => panic!("parsed incorrectly as stdin"),
Expand All @@ -43,7 +42,7 @@ mod tests {

#[test]
fn it_errs_with_empty_path() {
let loc = parse_schema_location("");
let loc = parse_schema_source("");
assert!(loc.is_err());
}
}

0 comments on commit 631a887

Please sign in to comment.