seaplane_cli/cli/cmds/formation/
list.rs1use 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
10static LONG_ABOUT: &str = "List all local Formation Plans
11
12This command will display the status and number of configurations for each of your Formation
13Plans. The Formations displayed come from the local database of known Formations. You may wish
14to update the local database with Remote Formation Instances as well by either first running:
15
16$ seaplane formation fetch-remote
17
18OR including `--fetch` such as:
19
20$ seaplane formation list --fetch
21
22After which your local database of Formation and Flight Plans will contain all remote Formation
23Instances and their configurations as well.";
24
25#[derive(Copy, Clone, Debug)]
26pub struct SeaplaneFormationList;
27
28impl SeaplaneFormationList {
29 pub fn command() -> Command {
30 Command::new("list")
31 .visible_alias("ls")
32 .long_about(LONG_ABOUT)
33 .about("List all local Formation Plans")
34 .arg(arg!(--fetch|sync|synchronize - ('F')).help("Fetch remote Formation Instances and create/synchronize with local Plan Definitions prior to listing (by default only local Plans are displayed)"))
35 .arg(
36 arg!(--format =["FORMAT"=>"table"])
37 .value_parser(value_parser!(OutputFormat))
38 .help("Change the output format"),
39 )
40 }
41}
42
43impl CliCommand for SeaplaneFormationList {
44 fn run(&self, ctx: &mut Ctx) -> Result<()> {
45 if ctx.args.stateless && !ctx.args.fetch {
46 cli_eprint!(@Red, "error: ");
47 cli_eprint!("'");
48 cli_eprint!(@Yellow, "seaplane formation list");
49 cli_eprint!("' when used with '");
50 cli_eprint!(@Yellow, "--stateless");
51 cli_eprint!("' is useless without '");
52 cli_eprint!(@Green, "--fetch");
53 cli_eprintln!("'");
54 cli_eprintln!("(hint: 'seaplane formation list' only looks at local Plan definitions)");
55 cli_eprint!("(hint: 'seaplane formation list");
56 cli_eprint!(@Green, "--fetch");
57 cli_eprintln!("' also synchronizes local Plan definitions with remote Instances)");
58 std::process::exit(1);
59 }
60
61 if ctx.args.fetch {
62 let old_name = ctx.args.name_id.take();
63 SeaplaneFormationFetch.run(ctx)?;
64 ctx.args.name_id = old_name;
65 }
66
67 match ctx.args.out_format {
68 OutputFormat::Json => ctx.db.formations.print_json(ctx)?,
69 OutputFormat::Table => ctx.db.formations.print_table(ctx)?,
70 }
71
72 Ok(())
73 }
74
75 fn update_ctx(&self, matches: &ArgMatches, ctx: &mut Ctx) -> Result<()> {
76 ctx.args.out_format = matches.get_one("format").copied().unwrap_or_default();
77 ctx.args.fetch = matches.get_flag("fetch");
78 Ok(())
79 }
80}