Skip to content

Commit

Permalink
datapod: add config command (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Wagner <n.wagner@dnb.de>
  • Loading branch information
nwagner84 authored Jul 9, 2024
1 parent 4a2d24e commit 2254b31
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/datapod/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::{Parser, Subcommand};

use crate::commands::config::Config;
use crate::commands::init::Init;
use crate::commands::version::Version;

Expand All @@ -13,5 +14,6 @@ pub(crate) struct Args {
#[derive(Debug, Subcommand)]
pub(crate) enum Command {
Init(Init),
Config(Config),
Version(Version),
}
96 changes: 96 additions & 0 deletions crates/datapod/src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use clap::Parser;

use crate::config::Runtime;
use crate::datapod::Datapod;
use crate::error::{bail, DatapodError, DatapodResult};

/// Get and set dataset options.
#[derive(Debug, Parser)]
pub(crate) struct Config {
/// Get the value for the given key.
#[arg(long, conflicts_with_all = ["value", "unset", "set"])]
get: bool,

/// Remove the key from the config.
#[arg(long, conflicts_with_all = ["value", "get", "set"])]
unset: bool,

/// Set the value for the given key.
#[arg(long, requires = "value", conflicts_with_all = ["get", "unset"])]
set: bool,

key: String,

#[arg(conflicts_with_all = ["get", "unset"])]
value: Option<String>,
}

#[inline]
fn print_option<T>(key: &str, value: Option<T>)
where
T: ToString,
{
println!(
"{key} = {}",
match value {
Some(value) => value.to_string(),
None => "None".to_string(),
}
);
}

pub(crate) fn execute(args: Config) -> DatapodResult<()> {
let datapod = Datapod::discover()?;
let mut config = datapod.config()?;
let key = args.key.as_str();

if args.value.is_some() {
let value = args.value.unwrap();
match key {
"runtime.num_jobs" => {
if let Ok(value) = value.parse::<usize>() {
if let Some(ref mut runtime) = config.runtime {
runtime.num_jobs = Some(value);
} else {
config.runtime = Some(Runtime {
num_jobs: Some(value),
});
}

config.save()?;
} else {
bail!("invalid value `{value}`");
}
}
_ => {
bail!("unknown or unsupported config option `{key}`");
}
}
} else if args.get || (!args.unset && !args.set) {
match key {
"runtime.num_jobs" => {
print_option(
key,
config.runtime.and_then(|rt| rt.num_jobs),
);
}
_ => {
bail!("unknown or unsupported config option `{key}`");
}
}
} else if args.unset {
match key {
"runtime.num_jobs" => {
config.runtime = None;
config.save()?;
}
_ => {
bail!("unknown or unsupported config option `{key}`");
}
}
} else {
unreachable!()
}

Ok(())
}
1 change: 1 addition & 0 deletions crates/datapod/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod config;
pub(crate) mod init;
pub(crate) mod version;
11 changes: 11 additions & 0 deletions crates/datapod/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub(crate) struct Config {
/// Datapod metadata.
pub(crate) metadata: Metadata,

/// Runtime options.
pub(crate) runtime: Option<Runtime>,

/// This structure should always be constructed using a public
/// constructor or using the update syntax:
///
Expand Down Expand Up @@ -60,6 +63,14 @@ impl Default for Metadata {
}
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub(crate) struct Runtime {
/// Number of threads to use. If this options isn't set or a value
/// of "0" is chosen, the maximum number of available threads
/// is used.
pub(crate) num_jobs: Option<usize>,
}

impl Config {
/// Creates a new default config and sets the file location.
pub(crate) fn create<P>(path: P) -> DatapodResult<Self>
Expand Down
8 changes: 8 additions & 0 deletions crates/datapod/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
pub(crate) type DatapodResult<T> = Result<T, DatapodError>;

macro_rules! bail {
($($arg:tt)*) => {{
return Err(DatapodError::Other(format!($($arg)*)));
}};
}

pub(crate) use bail;

#[derive(Debug, thiserror::Error)]
pub(crate) enum DatapodError {
#[error(transparent)]
Expand Down
2 changes: 2 additions & 0 deletions crates/datapod/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ mod cli;
mod commands;
mod config;
mod datapod;
#[macro_use]
mod error;

fn run(args: Args) -> DatapodResult<()> {
match args.cmd {
Command::Init(args) => commands::init::execute(args),
Command::Config(args) => commands::config::execute(args),
Command::Version(args) => commands::version::execute(args),
}
}
Expand Down

0 comments on commit 2254b31

Please sign in to comment.