1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
use errors::{Error, ErrorKind, Result}; use clap::ArgMatches; use client::{Client, StageResponse}; use config::Config; use repo::trekker_name; use std::env::current_dir; use std::path::Path; use websocket; pub fn run<C: Client>(matches: &ArgMatches, config: &Config, client: &C, optional_path: Option<&Path>) -> Result<()> { let default_path = current_dir()?; let path = optional_path.unwrap_or(&default_path); let stage_name = matches.value_of("stage").expect("Expected required stage"); let trekker = match matches.value_of("trekker") { Some(trekker) => trekker.into(), None => trekker_name(path)?, }; let stage = fetch_stage(stage_name, &trekker, client)?; let connection = websocket::client::build(stage.id, &config.token)?; websocket::client::run(connection)?; Ok(()) } pub fn fetch_stage<C: Client>(stage_name: &str, trekker: &str, client: &C) -> Result<StageResponse> { debug!("Beginning request for stages"); let stages = client.list_stages(&trekker)?; debug!("Listing stages: {:?}", stages); let stage = stages.into_iter() .find(|stage_response| stage_response.name == stage_name) .ok_or(Error::from_kind(ErrorKind::StageNotFound(stage_name.into())))?; debug!("Found Stage: {:?}", stage); Ok(stage) } #[cfg(test)] mod test { use client::{default_sherpa_stage, fetch_host_from_sherpa_stage, Client, MockClient, StageResponse}; use super::fetch_stage; #[test] fn test_fetch_stage() { let list_stages_response = vec![ StageResponse { id: 1, name: "staging1".into() }, StageResponse { id: 2, name: "staging2".into() }, ]; let host = fetch_host_from_sherpa_stage(default_sherpa_stage()).unwrap(); let mut mock_client = MockClient::new(&host[..], None); mock_client.list_stages_response = list_stages_response; let stage = fetch_stage("staging2", "procore", &mock_client).unwrap(); assert_eq!(stage.id, 2); assert_eq!(stage.name, "staging2"); } }