Skip to content

Commit

Permalink
Config versioning
Browse files Browse the repository at this point in the history
Closes #14
  • Loading branch information
tinybeachthor committed Jul 5, 2023
1 parent ac68cb7 commit b9476ee
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion client/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn run(opts: Opts) -> Result<()> {
let path = opts.config;
let data: ConfigData = sub.clone().into();

let config = Config::new(path, data)?;
let config = Config::new(path, data.into())?;
config.save()?;

eprintln!("Updated nixcache config.");
Expand Down
30 changes: 25 additions & 5 deletions client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ pub struct Config {
pub path: PathBuf,
}
impl Config {
pub fn new(path: Option<PathBuf>, data: ConfigData) -> Result<Self> {
pub fn new(path: Option<PathBuf>, data: ConfigDataVersioned) -> Result<Self> {
let path = match path {
Some(path) => path,
None => get_config_path()?,
};

Ok(Self {
path,
data,
data: data.into(),
})
}

Expand All @@ -44,18 +44,19 @@ impl Config {
if path.exists() {
let contents = fs::read(&path)?;
let s = std::str::from_utf8(&contents)?;
let data: ConfigData = toml::from_str(s)?;
let data: ConfigDataVersioned = toml::from_str(s)?;
return Ok(Config {
path,
data,
data: data.into(),
});
}

Err(anyhow!("No config found at '{}'.", path.to_string_lossy()))
}
/// Saves the configuration back to the system, if possible.
pub fn save(&self) -> Result<()> {
let serialized = toml::to_string(&self.data)?;
let config: ConfigDataVersioned = self.data.clone().into();
let serialized = toml::to_string(&config)?;

// This isn't atomic, so some other process might chmod it
// to something else before we write. We don't handle this case.
Expand All @@ -78,6 +79,25 @@ impl Config {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "version")]
pub enum ConfigDataVersioned {
#[serde(rename = "v1")]
V1(ConfigData),
}
impl Into<ConfigData> for ConfigDataVersioned {
fn into(self) -> ConfigData {
match self {
ConfigDataVersioned::V1(config) => config,
}
}
}
impl From<ConfigData> for ConfigDataVersioned {
fn from(config: ConfigData) -> Self {
ConfigDataVersioned::V1(config)
}
}

/// Client configuration.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ConfigData {
Expand Down
4 changes: 4 additions & 0 deletions package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
let
demo-url = "127.0.0.1:8080";
demo-config-server = writeText "config.toml" ''
version = "v1"
listen = "${demo-url}"
signing_key = "demo.nixcache-0:vjg4zb3o8U3SapIoeG5dWZ9+G4OyqA96J2+nxuoMPCT3a7/zXWgXpuKr+rJWChlyTGeCV2aARebK+ffmh+u2fw=="
Expand All @@ -21,6 +23,8 @@ type = "local"
path = "/tmp/_demo_nixcache"
'';
demo-config-client = writeText "config.toml" ''
version = "v1"
[server]
endpoint = "http://${demo-url}"
'';
Expand Down
22 changes: 19 additions & 3 deletions server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ pub struct Config {
/// Signing keypair.
pub keypair: Keypair,
}
impl TryFrom<ConfigInfo> for Config {
impl TryFrom<ConfigInfoVersioned> for Config {
type Error = anyhow::Error;
fn try_from(config: ConfigInfo) -> Result<Self> {
fn try_from(versioned: ConfigInfoVersioned) -> Result<Self> {
let config: ConfigInfo = versioned.into();

let token_hs256_secret = config.token_hs256_secret
.map(|x| decode_token_hs256_secret_base64(&x)).transpose()?;

Expand All @@ -54,13 +56,27 @@ pub async fn load(path: Option<PathBuf>) -> Result<Config> {

if path.is_file() {
let data = read_to_string(path)?;
let config: ConfigInfo = toml::from_str(&data)?;
let config: ConfigInfoVersioned = toml::from_str(&data)?;
config.try_into()
} else {
Err(anyhow!("No config found."))
}
}

#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "version")]
pub enum ConfigInfoVersioned {
#[serde(rename = "v1")]
V1(ConfigInfo),
}
impl Into<ConfigInfo> for ConfigInfoVersioned {
fn into(self) -> ConfigInfo {
match self {
ConfigInfoVersioned::V1(config) => config,
}
}
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConfigInfo {
Expand Down

0 comments on commit b9476ee

Please sign in to comment.