Skip to content

Commit

Permalink
cmdline: move all postprocessing into command implementations
Browse files Browse the repository at this point in the history
Drop the separate pre/postprocessed structs for install and download,
and the separate subcommand enum.
  • Loading branch information
bgilbert committed Aug 19, 2021
1 parent 9c7f2df commit ae1f4a7
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 501 deletions.
23 changes: 0 additions & 23 deletions src/bin/rdcore/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// For consistency, have all parse_*() functions return Result.
#![allow(clippy::unnecessary_wraps)]

use anyhow::{bail, Result};
use structopt::clap::AppSettings;
use structopt::StructOpt;

Expand Down Expand Up @@ -96,25 +95,3 @@ pub struct StreamHashConfig {
#[structopt(value_name = "hash-file")]
pub hash_file: String,
}

/// Parse command-line arguments.
pub fn parse_args() -> Result<Cmd> {
let config = Cmd::from_args();
if let Cmd::Kargs(ref config) = config {
check_kargs(config)?;
}
Ok(config)
}

fn check_kargs(config: &KargsConfig) -> Result<()> {
// we could enforce these via clap's ArgGroup, but I don't like how the --help text looks
if !(config.boot_device.is_some()
|| config.boot_mount.is_some()
|| config.current
|| config.override_options.is_some())
{
// --override-options is undocumented on purpose
bail!("one of --boot-device, --boot-mount, or --current required");
}
Ok(())
}
12 changes: 11 additions & 1 deletion src/bin/rdcore/kargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use std::fs::read_to_string;

use libcoreinst::install::*;
Expand All @@ -23,6 +23,16 @@ use crate::cmdline::*;
use crate::rootmap::get_boot_mount_from_cmdline_args;

pub fn kargs(config: &KargsConfig) -> Result<()> {
// we could enforce these via clap's ArgGroup, but I don't like how the --help text looks
if !(config.boot_device.is_some()
|| config.boot_mount.is_some()
|| config.current
|| config.override_options.is_some())
{
// --override-options is undocumented on purpose
bail!("one of --boot-device, --boot-mount, or --current required");
}

if let Some(ref orig_options) = config.override_options {
modify_and_print(config, orig_options.trim()).context("modifying options")?;
} else if config.current {
Expand Down
7 changes: 3 additions & 4 deletions src/bin/rdcore/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ mod kargs;
mod rootmap;
mod stream_hash;

use anyhow::{Context, Result};
use anyhow::Result;
use structopt::StructOpt;

use crate::cmdline::*;

fn main() -> Result<()> {
let config = cmdline::parse_args().context("parsing arguments")?;

match config {
match Cmd::from_args() {
Cmd::Kargs(c) => kargs::kargs(&c),
Cmd::Rootmap(c) => rootmap::rootmap(&c),
Cmd::StreamHash(c) => stream_hash::stream_hash(&c),
Expand Down
Loading

0 comments on commit ae1f4a7

Please sign in to comment.