Skip to content

Commit

Permalink
.env support
Browse files Browse the repository at this point in the history
- if there's a .env file in CWD, parse it and set env vars from it
- create-package should generate a .gitignore file that contains .env
  file
- check if the .env file is checked in, panic and proceed only when
  FASTN_DANGER_ACCEPT_CHECKED_IN_ENV is set
  • Loading branch information
siddhantk232 authored and amitu committed Oct 27, 2023
1 parent efa7779 commit 03fc09f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions fastn-core/src/commands/create_package.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
async fn template_contents(
project_name: &str,
download_base_url: Option<&str>,
) -> (String, String) {
) -> (String, String, String) {
let ftd = format!(
r#"-- import: fastn
Expand All @@ -14,8 +14,12 @@ async fn template_contents(
.unwrap_or_default()
);
let index = "-- ftd.text: Hello world".to_string();
let gitignore = r#".build/
.env
"#
.to_string();

(ftd, index)
(ftd, index, gitignore)
}

pub async fn create_package(
Expand Down Expand Up @@ -53,12 +57,11 @@ pub async fn create_package(
// Create all directories if not present
tokio::fs::create_dir_all(final_dir.as_str()).await?;

let tmp_contents = template_contents(name, download_base_url).await;
let tmp_fastn = tmp_contents.0;
let tmp_index = tmp_contents.1;
let (tmp_fastn, tmp_index, tmp_gitignore) = template_contents(name, download_base_url).await;

fastn_core::utils::update(&final_dir.join("FASTN.ftd"), tmp_fastn.as_bytes()).await?;
fastn_core::utils::update(&final_dir.join("index.ftd"), tmp_index.as_bytes()).await?;
fastn_core::utils::update(&final_dir.join(".gitignore"), tmp_gitignore.as_bytes()).await?;

// Note: Not required for now
// let sync_message = "Initial sync".to_string();
Expand Down
1 change: 1 addition & 0 deletions fastn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
dotenvy = "0.15.7"
41 changes: 41 additions & 0 deletions fastn/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod commands;

pub fn main() {
fastn_observer::observe();

Expand Down Expand Up @@ -27,6 +28,8 @@ pub enum Error {
async fn async_main() -> Result<(), Error> {
let matches = app(version()).get_matches();

set_env_vars();

if cloud_commands(&matches).await? {
return Ok(());
}
Expand Down Expand Up @@ -543,3 +546,41 @@ pub fn version() -> &'static str {
}
}
}

fn set_env_vars() -> () {
let checked_in = {
if let Ok(status) = std::process::Command::new("git")
.arg("ls-files")
.arg("--error-unmatch")
.arg(".env")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
{
status.success() // .env is checked in
} else {
false
}
};

let ignore = {
if let Ok(val) = std::env::var("FASTN_DANGER_ACCEPT_CHECKED_IN_ENV") {
val != "false"
} else {
false
}
};

if checked_in && !ignore {
eprintln!(
"ERROR: the .env file is checked in to version control! This is a security risk.
Remove it from your version control system or run fastn again with
FASTN_DANGER_ACCEPT_CHECKED_IN_ENV set"
);
std::process::exit(1);
}

if let Ok(_) = dotenvy::dotenv() {
println!("INFO: loaded environment variables from .env file.");
}
}

0 comments on commit 03fc09f

Please sign in to comment.