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

Add Sundog to handle dynamic settings #49

Merged
merged 14 commits into from Jul 9, 2019
Merged
17 changes: 16 additions & 1 deletion workspaces/api/sundog/src/main.rs
Expand Up @@ -13,6 +13,7 @@ use std::env;
use std::process;
use std::str;

use apiserver::datastore;
use apiserver::datastore::deserialization;
zmrow marked this conversation as resolved.
Show resolved Hide resolved
use apiserver::model;

Expand All @@ -28,6 +29,7 @@ const API_COMMIT_URI: &str = "http://localhost:4242/settings/commit";
mod error {
use snafu::Snafu;

use apiserver::datastore;
use apiserver::datastore::deserialization;

// Get the HTTP status code out of a reqwest::Error
Expand Down Expand Up @@ -121,6 +123,12 @@ mod error {
#[snafu(display("Error serializing Settings to JSON: {}", source))]
Serialize { source: serde_json::error::Error },

#[snafu(display("Error serializing command output '{}': {}", output, source))]
SerializeScalar {
output: String,
source: datastore::ScalarError,
},

#[snafu(display("Error updating settings through '{}': {}", uri, source))]
UpdatingAPISettings { uri: String, source: reqwest::Error },

Expand Down Expand Up @@ -215,7 +223,14 @@ fn get_dynamic_settings(generators: HashMap<String, String>) -> Result<HashMap<S
.to_string();
trace!("Generator '{}' output: {}", &generator, &output);

settings.insert(setting, output);
// The command output must be serialized since we intend to call the
// datastore-level construct `from_map` on it. `from_map` treats
zmrow marked this conversation as resolved.
Show resolved Hide resolved
// strings as a serialized structure.
let serialized_output = datastore::serialize_scalar(&output)
.context(error::SerializeScalar { output: output })?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General Rust nit: if the field name is the same as the variable, you don't need to repeat it, e.g. "{ output }" is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, I knew that.

trace!("Serialized output: {}", &serialized_output);

settings.insert(setting, serialized_output);
}

Ok(settings)
Expand Down