1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
pub mod cmds;
pub mod errors;
pub mod specs;
pub mod validator;

use std::{
    env,
    io::{self, BufRead},
};

use clap::{crate_authors, value_parser, ArgAction, ArgMatches, Command};
use const_format::concatcp;

pub use crate::cli::cmds::*;
use crate::{
    context::Ctx,
    error::Result,
    printer::{ColorChoice, Printer},
};

const VERSION: &str = env!("SEAPLANE_VER_WITH_HASH");
static AUTHORS: &str = crate_authors!();
static LONG_VERBOSE: &str = "Display more verbose output

More uses displays more verbose output
    -v:  Display debug info
    -vv: Display trace info";

static LONG_QUIET: &str = "Suppress output at a specific level and below

More uses suppresses higher levels of output
    -q:   Only display WARN messages and above
    -qq:  Only display ERROR messages
    -qqq: Suppress all output";
static LONG_API_KEY: &str =
    "The API key associated with a Seaplane account used to access Seaplane API endpoints

The value provided here will override any provided in any configuration files.
A CLI provided value also overrides any environment variables.
One can use a special value of '-' to signal the value should be read from STDIN.";

pub trait CliCommand {
    fn update_ctx(&self, _matches: &ArgMatches, _ctx: &mut Ctx) -> Result<()> { Ok(()) }
    fn run(&self, _ctx: &mut Ctx) -> Result<()> { Ok(()) }
    fn next_subcmd<'a>(
        &self,
        _matches: &'a ArgMatches,
    ) -> Option<(Box<dyn CliCommand>, &'a ArgMatches)> {
        None
    }
}

impl dyn CliCommand + 'static {
    /// Performs three steps:
    ///
    /// - calls `self.update_ctx()`
    /// - calls `self.run()`
    /// - Gets the next subcommand (if any) by calling `self.next_subcmd()` and calls
    /// `traverse_exec` on that subcommand.
    ///
    /// This walks down the entire *used* subcommand hierarchy ensuring the `update_ctx` was called
    /// prior to `run` and that any deeper subcommands were executed.
    pub fn traverse_exec(&self, matches: &ArgMatches, ctx: &mut Ctx) -> Result<()> {
        self.update_ctx(matches, ctx)?;
        self.run(ctx)?;
        if let Some((c, m)) = self.next_subcmd(matches) {
            return c.traverse_exec(m, ctx);
        }
        Ok(())
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Seaplane;

impl Seaplane {
    pub fn command() -> Command<'static> {
        #[cfg_attr(not(any(feature = "unstable", feature = "ui_tests")), allow(unused_mut))]
        let mut app = Command::new("seaplane")
            .author(AUTHORS)
            .version(VERSION)
            .disable_colored_help(true)
            .long_version(concatcp!(VERSION, "\n", env!("SEAPLANE_BUILD_FEATURES")))
            .propagate_version(true)
            .subcommand_required(true)
            .arg_required_else_help(true)
            .arg(arg!(--verbose -('v') global)
                .help("Display more verbose output")
                .action(ArgAction::Count)
                .long_help(LONG_VERBOSE))
            .arg(arg!(--quiet -('q') global)
                .help("Suppress output at a specific level and below")
                .action(ArgAction::Count)
                .long_help(LONG_QUIET))
            .arg(arg!(--color global ignore_case =["COLOR"=>"auto"])
                .value_parser(value_parser!(ColorChoice))
                .overrides_with_all(&["color", "no-color"])
                .help("Should the output include color?"))
            .arg(arg!(--("no-color") global)
                .overrides_with_all(&["color", "no-color"])
                .help("Do not color output (alias for --color=never)"))
            .arg(arg!(--("api-key") -('A') global =["STRING"] hide_env_values)
                .env("SEAPLANE_API_KEY")
                .help("The API key associated with a Seaplane account used to access Seaplane API endpoints")
                .long_help(LONG_API_KEY))
            .arg(arg!(--("stateless") -('S') global)
                .help("Ignore local state files, do not read from or write to them"))
            .subcommand(SeaplaneAccount::command())
            .subcommand(SeaplaneFlight::command())
            .subcommand(SeaplaneFormation::command())
            .subcommand(SeaplaneInit::command())
            .subcommand(SeaplaneLicense::command())
            .subcommand(SeaplaneMetadata::command())
            .subcommand(SeaplaneLocks::command())
            .subcommand(SeaplaneRestrict::command())
            .subcommand(SeaplaneShellCompletion::command());

        #[cfg(feature = "unstable")]
        {
            app = app
                .subcommand(SeaplaneConfig::command())
                .subcommand(SeaplaneImage::command());
        }

        #[cfg(feature = "ui_tests")]
        {
            app = app.term_width(0);
        }
        app
    }
}

impl CliCommand for Seaplane {
    fn run(&self, ctx: &mut Ctx) -> Result<()> {
        // Initialize the printer now that we have all the color choices
        Printer::init(ctx.args.color);
        Ok(())
    }

    fn update_ctx(&self, matches: &ArgMatches, ctx: &mut Ctx) -> Result<()> {
        // There is a "bug" where due to how clap handles nested-subcommands with global flags and
        // overrides (yeah...niche) if two mutually exclusive flags that override each-other are
        // used at different nesting levels, the overrides do not happen.
        //
        // For us this means doing `seaplane --no-color SUBCOMMAND --color=auto` effectively there
        // will be no color output, because clap will evaluate `--no-color` to `true` (i.e. used)
        // even though they override each-other.
        //
        // So we err on the side of not providing color since that is the safer option
        ctx.args.color = match (matches.get_one("color").copied(), matches.contains_id("no-color"))
        {
            (_, true) => ColorChoice::Never,
            (Some(choice), _) => {
                if choice != ColorChoice::Auto {
                    choice
                } else {
                    ctx.args.color
                }
            }
            _ => unreachable!("neither --color nor --no-color were used somehow"),
        };

        ctx.args.stateless = matches.contains_id("stateless");

        // API tests sometimes write their own DB to test, so we don't want to overwrite that
        #[cfg(not(feature = "api_tests"))]
        {
            ctx.db = crate::context::Db::load_if(
                ctx.flights_file(),
                ctx.formations_file(),
                !ctx.args.stateless,
            )?;
        }

        if let Some(key) = &matches.get_one::<String>("api-key") {
            if key == &"-" {
                let stdin = io::stdin();
                let mut lines = stdin.lock().lines();
                if let Some(line) = lines.next() {
                    ctx.args.api_key = Some(line?);
                }
            } else {
                ctx.args.api_key = Some(key.to_string());
            }
        }

        Ok(())
    }

    fn next_subcmd<'a>(
        &self,
        matches: &'a ArgMatches,
    ) -> Option<(Box<dyn CliCommand>, &'a ArgMatches)> {
        match matches.subcommand() {
            Some(("account", m)) => Some((Box::new(SeaplaneAccount), m)),
            Some(("flight", m)) => Some((Box::new(SeaplaneFlight), m)),
            Some(("formation", m)) => Some((Box::new(SeaplaneFormation), m)),
            Some(("init", m)) => Some((Box::new(SeaplaneInit), m)),
            Some(("metadata", m)) => Some((Box::new(SeaplaneMetadata), m)),
            Some(("locks", m)) => Some((Box::new(SeaplaneLocks), m)),
            Some(("restrict", m)) => Some((Box::new(SeaplaneRestrict), m)),
            Some(("shell-completion", m)) => Some((Box::new(SeaplaneShellCompletion), m)),
            Some(("license", m)) => Some((Box::new(SeaplaneLicense), m)),
            #[cfg(feature = "unstable")]
            Some(("image", m)) => Some((Box::new(SeaplaneImage), m)),
            #[cfg(feature = "unstable")]
            Some(("config", m)) => Some((Box::new(SeaplaneConfig), m)),
            _ => None, // TODO: handle external plugins
        }
    }
}