Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env support #1425

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
48 changes: 48 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,48 @@ 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);
} else {
if checked_in && ignore {
println!(
"WARN: your .env file has been detected in the version control system! This poses a
significant security risk in case the source code becomes public."
);
}

if dotenvy::dotenv().is_ok() {
println!("INFO: loaded environment variables from .env file.");
}
}
}
Loading