Skip to content

Commit 0c99384

Browse files
authored
Only add Helm repos when required (#113)
1 parent 9ff9d30 commit 0c99384

File tree

9 files changed

+116
-127
lines changed

9 files changed

+116
-127
lines changed

rust/stackablectl/src/cli/mod.rs

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22

33
use clap::{Parser, Subcommand, ValueEnum};
44
use directories::ProjectDirs;
5-
use snafu::Snafu;
5+
use snafu::{ResultExt, Snafu};
66
use tracing::{debug, instrument, Level};
77

88
use stackable_cockpit::{
@@ -17,17 +17,41 @@ use stackable_cockpit::{
1717

1818
use crate::{
1919
args::{CommonFileArgs, CommonRepoArgs},
20-
cmds::{
21-
cache::CacheArgs, completions::CompletionsArgs, demo::DemoArgs, operator::OperatorArgs,
22-
release::ReleaseArgs, stack::StackArgs, stacklets::StackletsArgs,
23-
},
20+
cmds::{cache, completions, demo, operator, release, stack, stacklets},
2421
constants::{
2522
ENV_KEY_DEMO_FILES, ENV_KEY_RELEASE_FILES, ENV_KEY_STACK_FILES, REMOTE_DEMO_FILE,
2623
REMOTE_RELEASE_FILE, REMOTE_STACK_FILE, USER_DIR_APPLICATION_NAME,
2724
USER_DIR_ORGANIZATION_NAME, USER_DIR_QUALIFIER,
2825
},
2926
};
3027

28+
#[derive(Debug, Snafu)]
29+
pub enum Error {
30+
#[snafu(display("operator command error"))]
31+
Operator { source: operator::CmdError },
32+
33+
#[snafu(display("release command error"))]
34+
Release { source: release::CmdError },
35+
36+
#[snafu(display("stack command error"))]
37+
Stack { source: stack::CmdError },
38+
39+
#[snafu(display("stacklets command error"))]
40+
Stacklets { source: stacklets::CmdError },
41+
42+
#[snafu(display("demo command error"))]
43+
Demo { source: demo::CmdError },
44+
45+
#[snafu(display("completions command error"))]
46+
Completions { source: completions::CmdError },
47+
48+
#[snafu(display("cache command error"))]
49+
Cache { source: cache::CmdError },
50+
51+
#[snafu(display("helm error"))]
52+
Helm { source: HelmError },
53+
}
54+
3155
#[derive(Debug, Parser)]
3256
#[command(author, version, about, propagate_version = true)]
3357
pub struct Cli {
@@ -135,21 +159,54 @@ impl Cli {
135159
Ok(CacheSettings::disk(project_dir.cache_dir()))
136160
}
137161
}
162+
163+
#[instrument]
164+
pub async fn run(&self) -> Result<String, Error> {
165+
// FIXME (Techassi): There might be a better way to handle this with
166+
// the match later in this function.
167+
168+
// Add Helm repos only when required
169+
match &self.subcommand {
170+
Commands::Completions(_) => (),
171+
Commands::Cache(_) => (),
172+
_ => self.add_helm_repos().context(HelmSnafu)?,
173+
}
174+
175+
let cache = self
176+
.cache_settings()
177+
.unwrap()
178+
.try_into_cache()
179+
.await
180+
.unwrap();
181+
182+
// TODO (Techassi): Do we still want to auto purge when running cache commands?
183+
cache.auto_purge().await.unwrap();
184+
185+
match &self.subcommand {
186+
Commands::Operator(args) => args.run(self).await.context(OperatorSnafu),
187+
Commands::Release(args) => args.run(self, cache).await.context(ReleaseSnafu),
188+
Commands::Stack(args) => args.run(self, cache).await.context(StackSnafu),
189+
Commands::Stacklets(args) => args.run(self).await.context(StackletsSnafu),
190+
Commands::Demo(args) => args.run(self, cache).await.context(DemoSnafu),
191+
Commands::Completions(args) => args.run().context(CompletionsSnafu),
192+
Commands::Cache(args) => args.run(cache).await.context(CacheSnafu),
193+
}
194+
}
138195
}
139196

140197
#[derive(Debug, Subcommand)]
141198
pub enum Commands {
142199
/// Interact with single operator instead of the full platform
143200
#[command(alias("op"))]
144-
Operator(OperatorArgs),
201+
Operator(operator::OperatorArgs),
145202

146203
/// Interact with all operators of the platform which are released together
147204
#[command(alias("re"))]
148-
Release(ReleaseArgs),
205+
Release(release::ReleaseArgs),
149206

150207
/// Interact with stacks, which are ready-to-use product combinations
151208
#[command(alias("st"))]
152-
Stack(StackArgs),
209+
Stack(stack::StackArgs),
153210

154211
/// Interact with deployed stacklets, which are bundles of resources and
155212
/// containers required to run the product.
@@ -162,17 +219,17 @@ Each stacklet consists of init containers, app containers, sidecar containers
162219
and additional Kubernetes resources like StatefulSets, ConfigMaps, Services and
163220
CRDs."
164221
)]
165-
Stacklets(StackletsArgs),
222+
Stacklets(stacklets::StackletsArgs),
166223

167224
/// Interact with demos, which are end-to-end usage demonstrations of the Stackable data platform
168-
Demo(DemoArgs),
225+
Demo(demo::DemoArgs),
169226

170227
/// Generate shell completions for this tool
171228
#[command(alias("comp"))]
172-
Completions(CompletionsArgs),
229+
Completions(completions::CompletionsArgs),
173230

174231
/// Interact with locally cached files
175-
Cache(CacheArgs),
232+
Cache(cache::CacheArgs),
176233
}
177234

178235
#[derive(Clone, Debug, Default, ValueEnum)]

rust/stackablectl/src/cmds/cache.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct CacheCleanArgs {
3333
}
3434

3535
#[derive(Debug, Snafu)]
36-
pub enum CacheCmdError {
36+
pub enum CmdError {
3737
#[snafu(display("cache settings error"))]
3838
CacheSettingsError { source: CacheSettingsError },
3939

@@ -42,7 +42,7 @@ pub enum CacheCmdError {
4242
}
4343

4444
impl CacheArgs {
45-
pub async fn run(&self, cache: Cache) -> Result<String, CacheCmdError> {
45+
pub async fn run(&self, cache: Cache) -> Result<String, CmdError> {
4646
match &self.subcommand {
4747
CacheCommands::List => list_cmd(cache).await,
4848
CacheCommands::Clean(args) => clean_cmd(args, cache).await,
@@ -51,7 +51,7 @@ impl CacheArgs {
5151
}
5252

5353
#[instrument(skip_all)]
54-
async fn list_cmd(cache: Cache) -> Result<String, CacheCmdError> {
54+
async fn list_cmd(cache: Cache) -> Result<String, CmdError> {
5555
info!("Listing cached files");
5656

5757
let files = cache.list().await.context(CacheSnafu)?;
@@ -81,7 +81,7 @@ async fn list_cmd(cache: Cache) -> Result<String, CacheCmdError> {
8181
}
8282

8383
#[instrument(skip_all)]
84-
async fn clean_cmd(args: &CacheCleanArgs, cache: Cache) -> Result<String, CacheCmdError> {
84+
async fn clean_cmd(args: &CacheCleanArgs, cache: Cache) -> Result<String, CmdError> {
8585
info!("Cleaning cached files");
8686

8787
let delete_filter = if args.only_remove_old_files {

rust/stackablectl/src/cmds/completions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ pub enum CompletionCommands {
2323
}
2424

2525
#[derive(Debug, Snafu)]
26-
pub enum CompletionsCmdError {
26+
pub enum CmdError {
2727
#[snafu(display("string error: {source}"))]
2828
StringError { source: std::string::FromUtf8Error },
2929
}
3030

3131
impl CompletionsArgs {
32-
pub fn run(&self) -> Result<String, CompletionsCmdError> {
32+
pub fn run(&self) -> Result<String, CmdError> {
3333
match &self.subcommand {
3434
CompletionCommands::Bash => generate_completions(Shell::Bash),
3535
CompletionCommands::Fish => generate_completions(Shell::Fish),
@@ -38,7 +38,7 @@ impl CompletionsArgs {
3838
}
3939
}
4040

41-
fn generate_completions(shell: Shell) -> Result<String, CompletionsCmdError> {
41+
fn generate_completions(shell: Shell) -> Result<String, CmdError> {
4242
let mut cmd = Cli::command();
4343
let mut buf = Vec::new();
4444

rust/stackablectl/src/cmds/demo.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ to specify operator versions."
111111
pub struct DemoUninstallArgs {}
112112

113113
#[derive(Debug, Snafu)]
114-
pub enum DemoCmdError {
114+
pub enum CmdError {
115115
#[snafu(display("unable to format yaml output"))]
116116
YamlOutputFormatError { source: serde_yaml::Error },
117117

@@ -148,7 +148,7 @@ pub enum DemoCmdError {
148148

149149
impl DemoArgs {
150150
#[instrument]
151-
pub async fn run(&self, common_args: &Cli, cache: Cache) -> Result<String, DemoCmdError> {
151+
pub async fn run(&self, common_args: &Cli, cache: Cache) -> Result<String, CmdError> {
152152
debug!("Handle demo args");
153153

154154
let transfer_client = FileTransferClient::new_with(cache);
@@ -173,7 +173,7 @@ impl DemoArgs {
173173

174174
/// Print out a list of demos, either as a table (plain), JSON or YAML
175175
#[instrument]
176-
async fn list_cmd(args: &DemoListArgs, list: DemoList) -> Result<String, DemoCmdError> {
176+
async fn list_cmd(args: &DemoListArgs, list: DemoList) -> Result<String, CmdError> {
177177
info!("Listing demos");
178178

179179
match args.output_type {
@@ -204,10 +204,10 @@ async fn list_cmd(args: &DemoListArgs, list: DemoList) -> Result<String, DemoCmd
204204

205205
/// Describe a specific demo by printing out a table (plain), JSON or YAML
206206
#[instrument]
207-
async fn describe_cmd(args: &DemoDescribeArgs, list: DemoList) -> Result<String, DemoCmdError> {
207+
async fn describe_cmd(args: &DemoDescribeArgs, list: DemoList) -> Result<String, CmdError> {
208208
info!("Describing demo {}", args.demo_name);
209209

210-
let demo = list.get(&args.demo_name).ok_or(DemoCmdError::NoSuchDemo {
210+
let demo = list.get(&args.demo_name).ok_or(CmdError::NoSuchDemo {
211211
name: args.demo_name.clone(),
212212
})?;
213213

@@ -242,10 +242,10 @@ async fn install_cmd(
242242
common_args: &Cli,
243243
list: DemoList,
244244
transfer_client: &FileTransferClient,
245-
) -> Result<String, DemoCmdError> {
245+
) -> Result<String, CmdError> {
246246
info!("Installing demo {}", args.demo_name);
247247

248-
let demo_spec = list.get(&args.demo_name).ok_or(DemoCmdError::NoSuchDemo {
248+
let demo_spec = list.get(&args.demo_name).ok_or(CmdError::NoSuchDemo {
249249
name: args.demo_name.clone(),
250250
})?;
251251

rust/stackablectl/src/cmds/operator.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub struct OperatorInstalledArgs {
122122
}
123123

124124
#[derive(Debug, Snafu)]
125-
pub enum OperatorCmdError {
125+
pub enum CmdError {
126126
#[snafu(display("invalid repo name"))]
127127
InvalidRepoNameError { source: InvalidRepoNameError },
128128

@@ -158,7 +158,7 @@ pub enum OperatorCmdError {
158158
pub struct OperatorVersionList(HashMap<String, Vec<String>>);
159159

160160
impl OperatorArgs {
161-
pub async fn run(&self, common_args: &Cli) -> Result<String, OperatorCmdError> {
161+
pub async fn run(&self, common_args: &Cli) -> Result<String, CmdError> {
162162
match &self.subcommand {
163163
OperatorCommands::List(args) => list_cmd(args, common_args).await,
164164
OperatorCommands::Describe(args) => describe_cmd(args).await,
@@ -170,7 +170,7 @@ impl OperatorArgs {
170170
}
171171

172172
#[instrument]
173-
async fn list_cmd(args: &OperatorListArgs, common_args: &Cli) -> Result<String, OperatorCmdError> {
173+
async fn list_cmd(args: &OperatorListArgs, common_args: &Cli) -> Result<String, CmdError> {
174174
debug!("Listing operators");
175175

176176
// Build map which maps Helm repo name to Helm repo URL
@@ -213,7 +213,7 @@ async fn list_cmd(args: &OperatorListArgs, common_args: &Cli) -> Result<String,
213213
}
214214

215215
#[instrument]
216-
async fn describe_cmd(args: &OperatorDescribeArgs) -> Result<String, OperatorCmdError> {
216+
async fn describe_cmd(args: &OperatorDescribeArgs) -> Result<String, CmdError> {
217217
debug!("Describing operator {}", args.operator_name);
218218

219219
// Build map which maps Helm repo name to Helm repo URL
@@ -257,10 +257,7 @@ async fn describe_cmd(args: &OperatorDescribeArgs) -> Result<String, OperatorCmd
257257
}
258258

259259
#[instrument]
260-
async fn install_cmd(
261-
args: &OperatorInstallArgs,
262-
common_args: &Cli,
263-
) -> Result<String, OperatorCmdError> {
260+
async fn install_cmd(args: &OperatorInstallArgs, common_args: &Cli) -> Result<String, CmdError> {
264261
info!("Installing operator(s)");
265262

266263
println!(
@@ -290,7 +287,7 @@ async fn install_cmd(
290287
match operator.install(&args.operator_namespace) {
291288
Ok(_) => println!("Installed {} operator", operator.name),
292289
Err(err) => {
293-
return Err(OperatorCmdError::HelmError { source: err });
290+
return Err(CmdError::HelmError { source: err });
294291
}
295292
};
296293
}
@@ -307,10 +304,7 @@ async fn install_cmd(
307304
}
308305

309306
#[instrument]
310-
fn uninstall_cmd(
311-
args: &OperatorUninstallArgs,
312-
common_args: &Cli,
313-
) -> Result<String, OperatorCmdError> {
307+
fn uninstall_cmd(args: &OperatorUninstallArgs, common_args: &Cli) -> Result<String, CmdError> {
314308
info!("Uninstalling operator(s)");
315309

316310
for operator in &args.operators {
@@ -331,10 +325,7 @@ fn uninstall_cmd(
331325
}
332326

333327
#[instrument]
334-
fn installed_cmd(
335-
args: &OperatorInstalledArgs,
336-
common_args: &Cli,
337-
) -> Result<String, OperatorCmdError> {
328+
fn installed_cmd(args: &OperatorInstalledArgs, common_args: &Cli) -> Result<String, CmdError> {
338329
debug!("Listing installed operators");
339330

340331
type ReleaseList = IndexMap<String, HelmRelease>;
@@ -388,7 +379,7 @@ fn installed_cmd(
388379

389380
/// Builds a map which maps Helm repo name to Helm repo URL.
390381
#[instrument]
391-
async fn build_helm_index_file_list<'a>() -> Result<HashMap<&'a str, HelmRepo>, OperatorCmdError> {
382+
async fn build_helm_index_file_list<'a>() -> Result<HashMap<&'a str, HelmRepo>, CmdError> {
392383
debug!("Building Helm index file list");
393384

394385
let mut helm_index_files = HashMap::new();
@@ -417,7 +408,7 @@ async fn build_helm_index_file_list<'a>() -> Result<HashMap<&'a str, HelmRepo>,
417408
#[instrument]
418409
fn build_versions_list(
419410
helm_index_files: &HashMap<&str, HelmRepo>,
420-
) -> Result<IndexMap<String, OperatorVersionList>, OperatorCmdError> {
411+
) -> Result<IndexMap<String, OperatorVersionList>, CmdError> {
421412
debug!("Building versions list");
422413

423414
let mut versions_list = IndexMap::new();
@@ -440,7 +431,7 @@ fn build_versions_list(
440431
fn build_versions_list_for_operator<T>(
441432
operator_name: T,
442433
helm_index_files: &HashMap<&str, HelmRepo>,
443-
) -> Result<OperatorVersionList, OperatorCmdError>
434+
) -> Result<OperatorVersionList, CmdError>
444435
where
445436
T: AsRef<str> + std::fmt::Debug,
446437
{
@@ -465,7 +456,7 @@ where
465456
fn list_operator_versions_from_repo<T>(
466457
operator_name: T,
467458
helm_repo: &HelmRepo,
468-
) -> Result<Vec<String>, OperatorCmdError>
459+
) -> Result<Vec<String>, CmdError>
469460
where
470461
T: AsRef<str> + std::fmt::Debug,
471462
{

0 commit comments

Comments
 (0)