Skip to content

Commit

Permalink
Merge pull request #898 from cgwalters/use-std-read
Browse files Browse the repository at this point in the history
Simplify read paths
  • Loading branch information
Luca Bruno authored Nov 22, 2022
2 parents 267ba4e + d63a014 commit 9c7294a
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 34 deletions.
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

0 comments on commit 9c7294a

Please sign in to comment.