~matthiasbeyer/imag

708c7b26b12364434316a5435b01a2c3869bfc6f — Matthias Beyer 1 year, 4 months ago 0cac7ec
Fix: Make "list" default command of imag-tag

The "list" command is advertised as default command for imag-tag. This
commit fixes the implementation so that the binary actually behaves this
way.

For this, the list() function gets a new parameter which tells it
whether there is a subcommand object to be expected in the Runtime.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
1 files changed, 21 insertions(+), 8 deletions(-)

M bin/core/imag-tag/src/lib.rs
M bin/core/imag-tag/src/lib.rs => bin/core/imag-tag/src/lib.rs +21 -8
@@ 89,7 89,7 @@ impl ImagApplication for ImagTag {
        let process = |iter: &mut dyn Iterator<Item = Result<StoreId>>| -> Result<()> {
            match rt.cli().subcommand() {
                ("list", _) => iter
                    .map_ok(|id| list(id, &rt))
                    .map_ok(|id| list(id, &rt, true))
                    .collect::<Result<Vec<_>>>()
                    .map(|_| ()),



@@ 185,6 185,13 @@ impl ImagApplication for ImagTag {
                        .map(|_| ())
                },

                (_, None) => {
                    debug!("No subcommand, using 'list'");
                    iter.map_ok(|id| list(id, &rt, false))
                        .collect::<Result<Vec<_>>>()
                        .map(|_| ())
                },

                (other, _) => {
                    debug!("Unknown command");
                    if rt.handle_unknown_subcommand("imag-tag", other, rt.cli())?.success() {


@@ 253,13 260,19 @@ fn alter(rt: &Runtime, path: StoreId, add: Option<Vec<Tag>>, rem: Option<Vec<Tag
    rt.report_touched(&path)
}

fn list(path: StoreId, rt: &Runtime) -> Result<()> {
fn list(path: StoreId, rt: &Runtime, has_subcommand: bool) -> Result<()> {
    let entry        = rt.store().get(path.clone())?.ok_or_else(|| err_msg("No entry found"))?;
    let scmd         = rt.cli().subcommand_matches("list").unwrap(); // safe, we checked in main()
    let json_out     = scmd.is_present("json");
    let line_out     = scmd.is_present("linewise");
    let sepp_out     = scmd.is_present("sep");
    let mut comm_out = scmd.is_present("commasep");
    let (scmd, json_out, line_out, sepp_out, mut comm_out) = if has_subcommand {
        let scmd     = rt.cli().subcommand_matches("list").unwrap();
        let json_out = scmd.is_present("json");
        let line_out = scmd.is_present("linewise");
        let sepp_out = scmd.is_present("sep");
        let comm_out = scmd.is_present("commasep");

        (Some(scmd), json_out, line_out, sepp_out, comm_out)
    } else {
        (None, false, false, false, false)
    };

    if !vec![json_out, line_out, comm_out, sepp_out].iter().any(|v| *v) {
        // None of the flags passed, go to default


@@ 279,7 292,7 @@ fn list(path: StoreId, rt: &Runtime) -> Result<()> {
    }

    if sepp_out {
        let sepp = scmd.value_of("sep").unwrap(); // we checked before
        let sepp = scmd.map(|s| s.value_of("sep").unwrap()).unwrap_or("");
        writeln!(rt.stdout(), "{}", tags.join(sepp))?;
    }