diff --git a/extensions/env-injector/src/main.rs b/extensions/env-injector/src/main.rs index b78994497c..f9b78289b6 100644 --- a/extensions/env-injector/src/main.rs +++ b/extensions/env-injector/src/main.rs @@ -4,6 +4,7 @@ use hyper::{Method, Request}; use hyper_util::client::legacy::Client; use hyperlocal::{UnixClientExt, UnixConnector, Uri}; use std::error::Error; +use std::fmt::format; use std::process::Command; use std::collections::HashMap; use std::path::Path; @@ -19,7 +20,7 @@ async fn snapdapi_req() -> Result Result<(), Box std::io::Result> { - let path = Path::new(file_path); - - if !path.exists() { - eprintln!("File does not exist: {}", file_path); - return Err(std::io::Error::new(std::io::ErrorKind::NotFound, "File does not exist")); - } - - if !path.is_file() { - eprintln!("File is not readable: {}", file_path); - return Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, "File is not readable")); - } let file = std::fs::File::open(file_path)?; let reader = std::io::BufReader::new(file); @@ -128,16 +118,55 @@ fn set_env_vars_from_file(app: &str, json: &serde_json::Value) -> Result<(), Bo Ok(()) } + +fn validate(json: &serde_json::Value) -> Result<(), Box> { + + if let Some(global_envfile) = json["envfile"].as_str() { + file_validation(global_envfile)?; + } + + if let Some(app_envfile) = json["apps"]["envfile"].as_str() { + file_validation(app_envfile)?; + } + + Ok(()) +} + +fn file_validation(file_path: &str) -> std::io::Result<()> { + let path = Path::new(&file_path); + + if !path.exists() { + let err = format!("File does not exist: {}", file_path); + eprintln!("{}", &err); + return Err(std::io::Error::new(std::io::ErrorKind::NotFound, err, )); + } + + if !path.is_file() { + let err = format!("File is not readable: {}", file_path); + eprintln!("{}", &err); + return Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, err)); + } + + Ok(()) +} + + + #[tokio::main] async fn run() -> Result<(), Box> { + let dryrun = std::env::var("DRY_RUN").is_ok(); + let json = snapdapi_req().await?; let app = std::env::var("env_alias")?; - set_env_vars_from_file(&app, &json)?; - set_env_vars(&app, &json)?; + validate(&json)?; + if !dryrun { + set_env_vars_from_file(&app, &json)?; + set_env_vars(&app, &json)?; + } Ok(()) } diff --git a/snapcraft/extensions/env_injector.py b/snapcraft/extensions/env_injector.py index c669fa1f4a..d5fad95f38 100644 --- a/snapcraft/extensions/env_injector.py +++ b/snapcraft/extensions/env_injector.py @@ -71,10 +71,13 @@ def get_root_snippet(self) -> Dict[str, Any]: @overrides - def get_app_snippet(self) -> Dict[str, Any]: + def get_app_snippet(self, *, app_name: str) -> Dict[str, Any]: """Return the app snippet to apply.""" return { "command-chain": [ "bin/command-chain/env-exporter"], + "environment": { + "env_alias": f"{app_name}", + } } @overrides @@ -90,7 +93,7 @@ def get_parts_snippet(self) -> Dict[str, Any]: toolchain = self.get_toolchain() return { f"env-injector/env-injector": { - "source": f"{get_extensions_data_dir()}/envs", + "source": f"{get_extensions_data_dir()}/env-injector", "plugin": "nil", "build-snaps": [ f"rustup",