-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmod.rs
More file actions
125 lines (109 loc) · 3.37 KB
/
Copy pathmod.rs
File metadata and controls
125 lines (109 loc) · 3.37 KB
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
//! Contains various types for composing the CLI interface for operators and other applications
//! running in a Kubernetes cluster.
use clap::{Args, Parser};
use stackable_telemetry::tracing::TelemetryOptions;
use crate::{namespace::WatchNamespace, utils::cluster_info::KubernetesClusterInfoOptions};
mod environment;
mod maintenance;
pub use environment::*;
pub use maintenance::*;
// NOTE (@Techassi): Why the hell is this here? Let's get rid of it.
pub const AUTHOR: &str = "Stackable GmbH - info@stackable.tech";
/// A common set of commands used by operators.
///
/// This enum is generic over the arguments available to the [`Command::Run`] subcommand. By default,
/// [`RunArguments`] is used, but a custom type can be used.
///
/// ```rust
/// use clap::Parser;
/// use stackable_operator::cli::Command;
///
/// #[derive(Parser)]
/// struct Run {
/// #[arg(long)]
/// name: String,
/// }
///
/// let _ = Command::<Run>::parse_from(["foobar-operator", "run", "--name", "foo"]);
/// ```
///
/// If you need operator-specific commands then you can flatten [`Command`] into your own command
/// enum.
///
/// ```rust
/// use clap::Parser;
/// use stackable_operator::cli::Command;
///
/// #[derive(Parser)]
/// enum CustomCommand {
/// /// Print hello world message
/// Hello,
///
/// #[clap(flatten)]
/// Framework(Command),
/// }
/// ```
#[derive(Debug, PartialEq, Eq, Parser)]
pub enum Command<Run: Args = RunArguments> {
/// Print CRD objects.
Crd,
/// Run the operator.
Run(Run),
}
/// Default CLI arguments that most operators take when running.
///
/// ### Embed into an extended argument set
///
/// ```rust
/// use clap::Parser;
/// use stackable_operator::cli::RunArguments;
///
/// #[derive(clap::Parser, Debug, PartialEq, Eq)]
/// struct Run {
/// #[clap(long)]
/// name: String,
///
/// #[clap(flatten)]
/// common: RunArguments,
/// }
/// ```
#[derive(Debug, PartialEq, Eq, Parser)]
#[command(long_about = "")]
pub struct RunArguments {
// TODO (@Techassi): This should be moved into the environment options
/// Provides a specific namespace to watch (instead of watching all namespaces)
#[arg(long, env, default_value = "")]
pub watch_namespace: WatchNamespace,
// IMPORTANT: All (flattened) sub structs should be placed at the end to ensure the help
// headings are correct.
#[command(flatten)]
pub common: CommonOptions,
#[command(flatten)]
pub maintenance: MaintenanceOptions,
#[command(flatten)]
pub operator_environment: OperatorEnvironmentOptions,
}
/// A set of CLI arguments that all (or at least most) Stackable applications use.
///
/// [`RunArguments`] is intended for operators, but it has fields that are not needed for utilities
/// such as `user-info-fetcher` or `opa-bundle-builder`. So this struct offers a limited set, that
/// should be shared across all Stackable tools running on Kubernetes.
#[derive(Debug, PartialEq, Eq, Args)]
pub struct CommonOptions {
#[command(flatten)]
pub telemetry: TelemetryOptions,
#[command(flatten)]
pub cluster_info: KubernetesClusterInfoOptions,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn verify_cli() {
use clap::CommandFactory;
RunArguments::command()
.print_long_help()
.expect("help message should be printed to stdout");
RunArguments::command().debug_assert();
}
}