Skip to content

Commit

Permalink
Merge pull request #465 from nix-community/compression
Browse files Browse the repository at this point in the history
Make config file optional
  • Loading branch information
Mic92 authored Nov 25, 2024
2 parents d2686b8 + eeb5731 commit 43dc8d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::store::Store;
use anyhow::{Context, Result};
use serde::Deserialize;
use std::fs::read_to_string;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

fn default_bind() -> String {
"[::]:5000".into()
Expand Down Expand Up @@ -32,7 +32,7 @@ pub(crate) struct SigningKey {
}

// TODO(conni2461): users to restrict access
#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Default)]
pub(crate) struct Config {
#[serde(default = "default_bind")]
pub(crate) bind: String,
Expand Down Expand Up @@ -65,11 +65,17 @@ pub(crate) struct Config {

pub(crate) fn load() -> Result<Config> {
let settings_file = std::env::var("CONFIG_FILE").unwrap_or_else(|_| "settings.toml".to_owned());
let mut settings: Config = toml::from_str(
&read_to_string(&settings_file)
.with_context(|| format!("Couldn't read config file '{settings_file}'"))?,
)
.with_context(|| format!("Couldn't parse config file '{settings_file}'"))?;

let mut settings: Config = if Path::new(&settings_file).exists() {
toml::from_str(
&read_to_string(&settings_file)
.with_context(|| format!("Couldn't read config file '{settings_file}'"))?,
)
.with_context(|| format!("Couldn't parse config file '{settings_file}'"))?
} else {
Config::default()
};

if let Some(sign_key_path) = &settings.sign_key_path {
log::warn!(
"The sign_key_path configuration option is deprecated. Use sign_key_paths instead."
Expand Down
17 changes: 17 additions & 0 deletions tests/t00-simple.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
echo file > $out/dir/file
ln -s /etc/passwd $out/forbidden-symlink
ln -s $out/dir/file $out/allowed-symlink
# check unicode
echo test > "$out/🦄"
# check invalid utf-8
touch $(echo -e "test\x80")
'';
in
{
Expand Down Expand Up @@ -56,6 +61,9 @@
assert data["root"]["entries"]["bin"]["type"] == "directory", "expect bin directory in listing"
client01.succeed("${pkgs.hello}/bin/hello")
# test unicode
client01.succeed("nix copy --from http://harmonia:5000/ ${testServe}")
print("download ${testServe}")
out = client01.wait_until_succeeds("curl -v http://harmonia:5000/serve/${hashPart testServe}/")
print(out)
Expand All @@ -68,6 +76,15 @@
out = client01.wait_until_succeeds("curl -v http://harmonia:5000/serve/${hashPart testServe}/dir/file").strip()
print(out)
assert "file" == out, f"expected 'file', got '{out}'"
out = client01.wait_until_succeeds("curl -v http://harmonia:5000/serve/${hashPart testServe}/🦄").strip()
print(out)
assert "test" == out, f"expected 'test', got '{out}'"
# TODO: this is still broken
#out = client01.wait_until_succeeds("curl -v http://harmonia:5000/serve/${hashPart testServe}/test\\x80").strip()
#print(out)
#assert "test" == out, f"expected 'test', got '{out}'"
'';
}
)

0 comments on commit 43dc8d3

Please sign in to comment.