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

Simplify read paths #898

Merged
merged 1 commit into from
Nov 22, 2022
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
6 changes: 1 addition & 5 deletions src/config/fragments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,10 @@ pub(crate) struct UpdatePeriodicWindow {
mod tests {
use super::*;
use maplit::btreeset;
use std::io::Read;

#[test]
fn basic_dist_config_sample() {
let fp = std::fs::File::open("tests/fixtures/00-config-sample.toml").unwrap();
let mut bufrd = std::io::BufReader::new(fp);
let mut content = vec![];
bufrd.read_to_end(&mut content).unwrap();
let content = std::fs::read("tests/fixtures/00-config-sample.toml").unwrap();
let cfg: ConfigFragment = toml::from_slice(&content).unwrap();

let expected = ConfigFragment {
Expand Down
11 changes: 2 additions & 9 deletions src/config/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,14 @@ impl ConfigInput {
common_path: &str,
extensions: Vec<String>,
) -> Result<Self> {
use std::io::Read;

let scanner = liboverdrop::FragmentScanner::new(dirs, common_path, true, extensions);

let mut fragments = Vec::new();
for (_, fpath) in scanner.scan() {
trace!("reading config fragment '{}'", fpath.display());

let fp = std::fs::File::open(&fpath)
.with_context(|| format!("failed to open file '{}'", fpath.display()))?;
let mut bufrd = std::io::BufReader::new(fp);
let mut content = vec![];
bufrd
.read_to_end(&mut content)
.with_context(|| format!("failed to read content of '{}'", fpath.display()))?;
let content = std::fs::read(&fpath)
.with_context(|| format!("failed to read file '{}'", fpath.display()))?;
let frag: fragments::ConfigFragment =
toml::from_slice(&content).context("failed to parse TOML")?;

Expand Down
12 changes: 1 addition & 11 deletions src/identity/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
//! https://github.com/coreos/afterburn/blob/v4.1.0/src/util/cmdline.rs
use anyhow::{Context, Result};
use std::io::Read;
use std::{fs, io};

/// Platform key.
static CMDLINE_PLATFORM_FLAG: &str = "ignition.platform.id";
Expand All @@ -18,16 +16,8 @@ pub(crate) fn read_id<T>(cmdline_path: T) -> Result<String>
where
T: AsRef<str>,
{
// open the cmdline file
let fpath = cmdline_path.as_ref();
let file =
fs::File::open(fpath).with_context(|| format!("failed to open cmdline file {}", fpath))?;

// read content
let mut bufrd = io::BufReader::new(file);
let mut contents = String::new();
bufrd
.read_to_string(&mut contents)
let contents = std::fs::read_to_string(fpath)
.with_context(|| format!("failed to read cmdline file {}", fpath))?;

// lookup flag by key name
Expand Down
6 changes: 2 additions & 4 deletions src/rpm_ostree/cli_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,8 @@ mod tests {
use super::*;

fn mock_status(path: &str) -> Result<StatusJson> {
let fp = std::fs::File::open(path).unwrap();
let bufrd = std::io::BufReader::new(fp);
let status: StatusJson = serde_json::from_reader(bufrd)?;
Ok(status)
let r = std::fs::File::open(path).map(std::io::BufReader::new)?;
Ok(serde_json::from_reader(r)?)
}

#[test]
Expand Down
6 changes: 1 addition & 5 deletions src/strategy/periodic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ impl StrategyPeriodic {
mod tests {
use super::*;
use crate::config::{fragments, inputs};
use std::io::Read;
use tokio::runtime as rt;

#[test]
Expand Down Expand Up @@ -301,10 +300,7 @@ mod tests {
}

fn parse_config_input(config_path: &str) -> inputs::ConfigInput {
let fp = std::fs::File::open(config_path).unwrap();
let mut bufrd = std::io::BufReader::new(fp);
let mut content = vec![];
bufrd.read_to_end(&mut content).unwrap();
let content = std::fs::read(config_path).unwrap();
let frag: fragments::ConfigFragment = toml::from_slice(&content).unwrap();
inputs::ConfigInput::merge_fragments(vec![frag])
}
Expand Down