Skip to content

Commit

Permalink
Support multi-doc yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
sed-i committed Aug 22, 2024
1 parent 89837cf commit 868d7bf
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::petgraph_wrappers::GraphAsCode;
use clap::{Parser, Subcommand};
use serde::Deserialize;
use std::error::Error;
use std::fs;
use std::io::IsTerminal;
use std::io::Read;
use std::io::Stdin;

mod bundle;
mod petgraph_wrappers;
Expand Down Expand Up @@ -34,15 +38,32 @@ enum Commands {
fn main() -> Result<(), Box<dyn Error>> {
let cli = Cli::parse();

let input = std::io::stdin();
let mut input = std::io::stdin();

let bundle: Bundle = if input.is_terminal() {
let contents: String = if input.is_terminal() {
// Empty stdin - try to read "bundle.yaml"
let f = std::fs::File::open("bundle.yaml")?;
serde_yaml::from_reader(f)?
// First reading to string because it's the only way to read a multi-doc yaml

//let f = std::fs::File::open("bundle.yaml")?;
fs::read_to_string("bundle.yaml")?
//serde_yaml::from_reader(f)?
} else {
// Read from stdin
serde_yaml::from_reader(input.lock())?
let mut contents: String = String::new();
Stdin::read_to_string(&mut input, &mut contents)?;
contents
//serde_yaml::from_reader(input.lock())?
};

let mut d = serde_yaml::Deserializer::from_str(&contents);
let bundle: Bundle = loop {
if let Some(dd) = d.next() {
if let Ok(bundle) = Bundle::deserialize(dd) {
break bundle;
}
} else {
panic!("Invalid yaml");
}
};

let graph = bundle.to_graph();
Expand Down

0 comments on commit 868d7bf

Please sign in to comment.