seaplane_cli/cli/cmds/flight/
list.rs

1use clap::{value_parser, ArgMatches, Command};
2
3use crate::{
4    cli::{cmds::formation::SeaplaneFormationFetch, CliCommand},
5    error::Result,
6    printer::Output,
7    Ctx, OutputFormat,
8};
9
10#[derive(Copy, Clone, Debug)]
11pub struct SeaplaneFlightList;
12
13impl SeaplaneFlightList {
14    pub fn command() -> Command {
15        // TODO: add sorting
16        // TODO: add filtering
17        Command::new("list")
18            .visible_alias("ls")
19            .about("List all local Flight Plans")
20            .arg(arg!(--fetch|sync|synchronize - ('F')).help("Fetch and synchronize remote Formation Instances (which reference Flight Plans) prior (by default only local plans displayed)"))
21            .arg(
22                arg!(--format =["FORMAT"=>"table"])
23                    .help("Change the output format")
24                    .value_parser(value_parser!(OutputFormat)),
25            )
26    }
27}
28
29impl CliCommand for SeaplaneFlightList {
30    fn run(&self, ctx: &mut Ctx) -> Result<()> {
31        if ctx.args.stateless && !ctx.args.fetch {
32            cli_eprint!(@Red, "error: ");
33            cli_eprint!("'seaplane flight list ");
34            cli_eprint!(@Yellow, "--stateless");
35            cli_eprint!("' does nothing without also adding '");
36            cli_eprint!(@Green, "--fetch");
37            cli_eprintln!("'");
38            cli_eprintln!("(hint: 'seaplane flight list' only displays local plans, but '--stateless' ignores anything local)");
39            cli_eprint!("(hint: 'seaplane flight list ");
40            cli_eprint!(@Green, "--fetch");
41            cli_eprintln!("' will download and display remote references as well)");
42            std::process::exit(1);
43        }
44
45        if ctx.args.fetch {
46            let old_name = ctx.args.name_id.take();
47            SeaplaneFormationFetch.run(ctx)?;
48            ctx.args.name_id = old_name;
49        }
50
51        match ctx.args.out_format {
52            OutputFormat::Json => ctx.db.flights.print_json(ctx)?,
53            OutputFormat::Table => ctx.db.flights.print_table(ctx)?,
54        }
55
56        Ok(())
57    }
58
59    fn update_ctx(&self, matches: &ArgMatches, ctx: &mut Ctx) -> Result<()> {
60        ctx.args.out_format = matches.get_one("format").copied().unwrap_or_default();
61        ctx.args.fetch = matches.get_flag("fetch");
62        Ok(())
63    }
64}