Skip to content

Instantly share code, notes, and snippets.

@Ironlenny
Last active December 20, 2018 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ironlenny/f72629388056bfed0bbf5458b0e551b8 to your computer and use it in GitHub Desktop.
Save Ironlenny/f72629388056bfed0bbf5458b0e551b8 to your computer and use it in GitHub Desktop.
bcachectl
[package]
name = "bcachectl"
version = "0.1.0"
authors = ["Foo <foo@example.com>"]
edition = "2018"
[dependencies]
structopt = "0.2"
failure = ""
exitfailure = ""
toml = "0.4"
serde_derive = "1.0"
use std::fs;
use std::path::PathBuf;
use structopt::StructOpt;
use std::collections::HashMap;
use failure::ResultExt;
use exitfailure::ExitFailure;
use toml;
use serde_derive::Deserialize;
#[derive(Debug, Deserialize)]
struct Config {
device: Device,
}
#[derive(Debug, Deserialize)]
struct Device {
name: String,
cache_mode: String,
sequental_cutoff: u32,
}
pub struct Commands {
commands: HashMap<PathBuf, String>,
default_conf: PathBuf,
pub args: Cli,
}
#[derive(Debug, StructOpt)]
#[structopt(name = "Bcache Control", about = "A program to control Bcache devices", rename_all = "kebab-case")]
pub struct Cli {
/// The name of the device to control. Ex. bcache0
pub device: Option<String>,
/// Set caching mode. Modes are writethrough, writeback, writearound, and none
#[structopt(short, long,)]
cache_mode: Option<String>,
#[structopt(short, long,)]
/// Set cutoff for sequential reads and writes
sequential_cutoff: Option<u32>,
}
impl Commands {
pub fn new () -> Self {
Commands { commands: HashMap::new(), default_conf: PathBuf::from("./bcache.conf"), args: Cli::from_args() }
}
pub fn parse_args(&mut self) {
let device = match &self.args.device {
Some(_) => self.args.device.as_ref().unwrap().to_string(),
None => panic!("No device given"),
};
if let Some(mode) = &self.args.cache_mode {
let path = format!("{}{}{}", "/sys/block/", device, "/cache_mode");
let path = PathBuf::from(path);
&mut self.commands.insert(path, mode.to_string());
}
if let Some(value) = &self.args.sequential_cutoff {
let path = format!("{}{}{}", "/sys/block/", device, "/sequential_cutoff");
let path = PathBuf::from(path);
&mut self.commands.insert(path, value.to_string());
}
}
pub fn parse_conf(&self) {
return
}
pub fn run_commands(&self) -> Result<(), ExitFailure> {
for (path, value) in &self.commands {
fs::write(&path, value).with_context(|_| format!("could not write to file {}", path.display()))?
}
Ok(())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment