git.delta.rocks / jrsonnet / refs/commits / bde2637fa85f

difftreelog

source

crates/jrsonnet-cli/src/ext.rs2.8 KiBsourcehistory
1use std::{fs::read_to_string, str::FromStr};23use clap::Parser;4use jrsonnet_evaluator::{error::Result, State};56use crate::ConfigureState;78#[derive(Clone)]9pub struct ExtStr {10	pub name: String,11	pub value: String,12}1314impl FromStr for ExtStr {15	type Err = &'static str;16	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {17		let out: Vec<_> = s.split('=').collect();18		match out.len() {19			1 => Ok(ExtStr {20				name: out[0].to_owned(),21				value: std::env::var(out[0]).or(Err("missing env var"))?,22			}),23			2 => Ok(ExtStr {24				name: out[0].to_owned(),25				value: out[1].to_owned(),26			}),2728			_ => Err("bad ext-str syntax"),29		}30	}31}3233#[derive(Clone)]34pub struct ExtFile {35	pub name: String,36	pub value: String,37}3839impl FromStr for ExtFile {40	type Err = String;4142	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {43		let out: Vec<&str> = s.split('=').collect();44		if out.len() != 2 {45			return Err("bad ext-file syntax".to_owned());46		}47		let file = read_to_string(&out[1]);48		match file {49			Ok(content) => Ok(Self {50				name: out[0].into(),51				value: content,52			}),53			Err(e) => Err(format!("{}", e)),54		}55	}56}5758#[derive(Parser)]59#[clap(next_help_heading = "EXTERNAL VARIABLES")]60pub struct ExtVarOpts {61	/// Add string external variable.62	/// External variables are globally available so it is preferred63	/// to use top level arguments whenever it's possible.64	/// If [=data] is not set then it will be read from `name` env variable.65	/// Can be accessed from code via `std.extVar("name")`.66	#[clap(67		long,68		short = 'V',69		name = "name[=var data]",70		number_of_values = 1,71		multiple_occurrences = true72	)]73	ext_str: Vec<ExtStr>,74	/// Read string external variable from file.75	/// See also `--ext-str`76	#[clap(77		long,78		name = "name=var path",79		number_of_values = 1,80		multiple_occurrences = true81	)]82	ext_str_file: Vec<ExtFile>,83	/// Add external variable from code.84	/// See also `--ext-str`85	#[clap(86		long,87		name = "name[=var source]",88		number_of_values = 1,89		multiple_occurrences = true90	)]91	ext_code: Vec<ExtStr>,92	/// Read string external variable from file.93	/// See also `--ext-str`94	#[clap(95		long,96		name = "name=var code path",97		number_of_values = 1,98		multiple_occurrences = true99	)]100	ext_code_file: Vec<ExtFile>,101}102impl ConfigureState for ExtVarOpts {103	fn configure(&self, s: &State) -> Result<()> {104		for ext in self.ext_str.iter() {105			s.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());106		}107		for ext in self.ext_str_file.iter() {108			s.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());109		}110		for ext in self.ext_code.iter() {111			s.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;112		}113		for ext in self.ext_code_file.iter() {114			s.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;115		}116		Ok(())117	}118}