orcs 0.0.8

Microservices monorepo orchestration tool
Documentation
use crate::Error;
use crate::{get_store, Project, StageContext, Store};
use clap::ArgMatches;
use std::fs::File;
use std::io::Read;
use tracing::{info, instrument};

#[instrument(skip(subcommand))]
pub fn set(project_path: &str, subcommand: &ArgMatches) -> Result<(), Error> {
    info!("Set parameter value");
    // Load the project
    let project = Project::from_path(project_path)?;

    // Retrieve the param name
    let param_name = subcommand.value_of("name").unwrap();
    let param_value = subcommand.value_of("value").unwrap();

    // Fail if we are not in a stage context
    let context = match StageContext::from_env() {
        Some(context) => context,
        None => {
            return Err(Error::SetParameterError(
                "cannot run 'set' outside a stage context",
                param_name.to_string(),
            ))
        }
    };
    // Prefix the parameter with the service name
    let param_name = format!("{}:{}", context.service, &param_name);

    // Load the data store
    let mut store = get_store(project)?;

    // Store the value
    store.set(&param_name, param_value)?;

    Ok(())
}

#[instrument(skip(subcommand))]
pub fn set_file(project_path: &str, subcommand: &ArgMatches) -> Result<(), Error> {
    info!("Set parameter value file");
    // Load the project
    let project = Project::from_path(project_path)?;

    // Retrieve the param name
    let param_name = subcommand.value_of("name").unwrap();
    let filename = subcommand.value_of("filename").unwrap();

    // Fail if we are not in a stage context
    let context = match StageContext::from_env() {
        Some(context) => context,
        None => {
            return Err(Error::SetParameterError(
                "cannot run 'set' outside a stage context",
                param_name.to_string(),
            ))
        }
    };
    // Prefix the parameter with the service name
    let param_name = format!("{}:{}", context.service, &param_name);

    // Load the data store
    let mut store = get_store(project)?;

    // Read the file
    let mut file = File::open(filename)?;
    let mut data = Vec::new();
    file.read_to_end(&mut data)?;

    // Store the value
    store.set_bytes(&param_name, &data)?;

    Ok(())
}