From 002ea82ae779df945bdbc35a5c33c06fed09a9df Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Thu, 21 Dec 2023 08:25:37 +0000 Subject: [PATCH] env guarding Signed-off-by: Alex Jones --- Cargo.lock | 39 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/analyze/mod.rs | 5 ++++- src/bedrock/mod.rs | 18 ++++++++++-------- src/main.rs | 21 +++++++++++++++++++++ 5 files changed, 76 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3a905fe..9b4ce6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1080,6 +1080,19 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" +[[package]] +name = "env_logger" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -1340,6 +1353,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.14.27" @@ -1478,8 +1497,10 @@ dependencies = [ "chrono", "clap", "colored", + "env_logger", "futures", "indicatif", + "log", "serde", "serde_json", "simple-home-dir", @@ -2110,6 +2131,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termcolor" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" +dependencies = [ + "winapi-util", +] + [[package]] name = "time" version = "0.3.30" @@ -2438,6 +2468,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index d270dff..88d49b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,8 @@ tokio = { version = "1", features = ["full"] } unescape = "0.1.0" v = "0.1.0" chrono = "0.4.31" +log = "0.4.20" +env_logger = "0.10.1" # Config for 'cargo dist' [workspace.metadata.dist] diff --git a/src/analyze/mod.rs b/src/analyze/mod.rs index 84f1cea..047ea9a 100644 --- a/src/analyze/mod.rs +++ b/src/analyze/mod.rs @@ -12,6 +12,7 @@ use std::error::Error; use std::sync::mpsc; use std::sync::mpsc::{Receiver, Sender}; use std::time::Duration; +use log::warn; pub async fn list_analyzers() -> Result<(), Box> { // Setup available providers @@ -149,7 +150,9 @@ pub async fn run_analysis( // check if the processed results analyzer exists as key // upsert the analysis result into the vector } - Err(_e) => (), + Err(e) => ( + warn!("{}", e) + ), } } } diff --git a/src/bedrock/mod.rs b/src/bedrock/mod.rs index 5bcca77..7d7d9f4 100644 --- a/src/bedrock/mod.rs +++ b/src/bedrock/mod.rs @@ -32,25 +32,27 @@ impl From for Blob { Blob::new(serde_json::to_string(&val).unwrap()) } } -pub struct BedrockClient {} +pub struct BedrockClient { + region: Region, + bedrock_model: String +} impl BedrockClient { pub fn new() -> Self { - Self {} + Self { + region: Region::new(env::var("BEDROCK_REGION").unwrap().clone()), + bedrock_model: env::var("BEDROCK_MODEL").unwrap().clone() + } } pub async fn enrich(&self, prompt: String) -> Result> { // force the config rejoin be set - let bedrock_region = env::var("BEDROCK_REGION")?.clone(); - let region = Region::new(bedrock_region); - let config = aws_config::from_env().region(region).load().await; + let config = aws_config::from_env().region(self.region.clone()).load().await; let client = aws_sdk_bedrockruntime::Client::new(&config); - let bedrock_model = env::var("BEDROCK_MODEL")?; - let question = format!("{} {}", prompt::DEFAULT_PROMPT, prompt.as_str()); let response = client .invoke_model() - .model_id(bedrock_model) + .model_id(self.bedrock_model.clone()) .content_type("application/json") .body(ClaudParams::new(question.as_str()).into()) .send() diff --git a/src/main.rs b/src/main.rs index 66f9d0c..86f80bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::env; +use std::process::exit; use clap::{Parser, Subcommand}; mod analyze; mod analyzer; @@ -40,8 +42,27 @@ enum Commands { resource: String, }, } + +const ENVS: &'static [&'static str] = &["BEDROCK_REGION", + "BEDROCK_MODEL", +"AWS_REGION","AWS_ACCESS_KEY","AWS_SECRET_ACCESS_KEY"]; + + #[tokio::main] async fn main() { + env_logger::init(); + + for e in ENVS { + let r = env::var(e); + match r { + Ok(x) => {}, + Err(_e) => { + println!("ENV: {} not found",e); + exit(1); + }, + } + } + let args = Args::parse(); match &args.command {