Skip to content

Commit

Permalink
Start to use unified console output
Browse files Browse the repository at this point in the history
  • Loading branch information
Techassi committed Oct 9, 2023
1 parent 7d20ded commit 817f868
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 100 deletions.
8 changes: 7 additions & 1 deletion rust/stackablectl/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
REMOTE_RELEASE_FILE, REMOTE_STACK_FILE, USER_DIR_APPLICATION_NAME,
USER_DIR_ORGANIZATION_NAME, USER_DIR_QUALIFIER,
},
output::Output,
};

#[derive(Debug, Snafu)]
Expand Down Expand Up @@ -189,9 +190,14 @@ impl Cli {
Commands::Stacklet(args) => args.run(self).await.context(StackletSnafu),
Commands::Demo(args) => args.run(self, cache).await.context(DemoSnafu),
Commands::Completions(args) => args.run().context(CompletionsSnafu),
Commands::Cache(args) => args.run(cache).await.context(CacheSnafu),
Commands::Cache(args) => args.run(self, cache).await.context(CacheSnafu),
}
}

// Output utility functions
pub fn output(&self) -> Output {
Output::new().unwrap()
}
}

#[derive(Debug, Subcommand)]
Expand Down
15 changes: 10 additions & 5 deletions rust/stackablectl/src/cmds/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use snafu::{ResultExt, Snafu};
use stackable_cockpit::xfer::cache::{self, Cache, DeleteFilter};
use tracing::{info, instrument};

use crate::cli::CacheSettingsError;
use crate::cli::{CacheSettingsError, Cli};

#[derive(Debug, Args)]
pub struct CacheArgs {
Expand Down Expand Up @@ -42,16 +42,16 @@ pub enum CmdError {
}

impl CacheArgs {
pub async fn run(&self, cache: Cache) -> Result<String, CmdError> {
pub async fn run(&self, cli: &Cli, cache: Cache) -> Result<String, CmdError> {
match &self.subcommand {
CacheCommands::List => list_cmd(cache).await,
CacheCommands::List => list_cmd(cache, cli).await,
CacheCommands::Clean(args) => clean_cmd(args, cache).await,
}
}
}

#[instrument(skip_all)]
async fn list_cmd(cache: Cache) -> Result<String, CmdError> {
async fn list_cmd(cache: Cache, cli: &Cli) -> Result<String, CmdError> {
info!("Listing cached files");

let files = cache.list().await.context(CacheSnafu)?;
Expand All @@ -77,7 +77,12 @@ async fn list_cmd(cache: Cache) -> Result<String, CmdError> {
table.add_row(vec![file_path, format!("{modified} seconds ago")]);
}

Ok(table.to_string())
let mut output = cli.output();
output
.add_command_hint("stackablectl cache clean", "to clean all cached files")
.set_output(table.to_string());

Ok(output.render())
}

#[instrument(skip_all)]
Expand Down
27 changes: 19 additions & 8 deletions rust/stackablectl/src/cmds/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,32 +148,30 @@ pub enum CmdError {

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

let transfer_client = FileTransferClient::new_with(cache);

// Build demo list based on the (default) remote demo file, and additional files provided by the
// STACKABLE_DEMO_FILES env variable or the --demo-files CLI argument.
let files = common_args.get_demo_files().context(PathOrUrlParseSnafu)?;
let files = cli.get_demo_files().context(PathOrUrlParseSnafu)?;

let list = DemoList::build(&files, &transfer_client)
.await
.context(ListSnafu)?;

match &self.subcommand {
DemoCommands::List(args) => list_cmd(args, list).await,
DemoCommands::List(args) => list_cmd(args, cli, list).await,
DemoCommands::Describe(args) => describe_cmd(args, list).await,
DemoCommands::Install(args) => {
install_cmd(args, common_args, list, &transfer_client).await
}
DemoCommands::Install(args) => install_cmd(args, cli, list, &transfer_client).await,
}
}
}

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

match args.output_type {
Expand All @@ -195,7 +193,20 @@ async fn list_cmd(args: &DemoListArgs, list: DemoList) -> Result<String, CmdErro
table.add_row(row);
}

Ok(table.to_string())
let mut output = cli.output();

output
.add_command_hint(
"stackablectl demo describe [OPTIONS] <DEMO>",
"display further information for the specified demo",
)
.add_command_hint(
"stackablectl demo install [OPTIONS] <DEMO>",
"install a demo",
)
.set_output(table.to_string());

Ok(output.render())
}
OutputType::Json => serde_json::to_string(&list.inner()).context(JsonOutputFormatSnafu),
OutputType::Yaml => serde_yaml::to_string(&list.inner()).context(YamlOutputFormatSnafu),
Expand Down
128 changes: 85 additions & 43 deletions rust/stackablectl/src/cmds/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,19 @@ pub enum CmdError {
pub struct OperatorVersionList(HashMap<String, Vec<String>>);

impl OperatorArgs {
pub async fn run(&self, common_args: &Cli) -> Result<String, CmdError> {
pub async fn run(&self, cli: &Cli) -> Result<String, CmdError> {
match &self.subcommand {
OperatorCommands::List(args) => list_cmd(args, common_args).await,
OperatorCommands::Describe(args) => describe_cmd(args).await,
OperatorCommands::Install(args) => install_cmd(args, common_args).await,
OperatorCommands::Uninstall(args) => uninstall_cmd(args, common_args),
OperatorCommands::Installed(args) => installed_cmd(args, common_args),
OperatorCommands::List(args) => list_cmd(args, cli).await,
OperatorCommands::Describe(args) => describe_cmd(args, cli).await,
OperatorCommands::Install(args) => install_cmd(args, cli).await,
OperatorCommands::Uninstall(args) => uninstall_cmd(args, cli),
OperatorCommands::Installed(args) => installed_cmd(args, cli),
}
}
}

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

// Build map which maps Helm repo name to Helm repo URL
Expand Down Expand Up @@ -201,7 +201,20 @@ async fn list_cmd(args: &OperatorListArgs, common_args: &Cli) -> Result<String,
]);
}

Ok(table.to_string())
let mut output = cli.output();

output
.add_command_hint(
"stackablectl operator describe [OPTIONS] <OPERATOR>",
"display further information for the specified operator",
)
.add_command_hint(
"stackablectl operator install [OPTIONS] <OPERATORS>...",
"install one or more operators",
)
.set_output(table.to_string());

Ok(output.render())
}
OutputType::Json => {
Ok(serde_json::to_string(&versions_list).context(JsonOutputFormatSnafu)?)
Expand All @@ -213,7 +226,7 @@ async fn list_cmd(args: &OperatorListArgs, common_args: &Cli) -> Result<String,
}

#[instrument]
async fn describe_cmd(args: &OperatorDescribeArgs) -> Result<String, CmdError> {
async fn describe_cmd(args: &OperatorDescribeArgs, cli: &Cli) -> Result<String, CmdError> {
debug!("Describing operator {}", args.operator_name);

// Build map which maps Helm repo name to Helm repo URL
Expand All @@ -240,7 +253,6 @@ async fn describe_cmd(args: &OperatorDescribeArgs) -> Result<String, CmdError> {
};

let mut table = Table::new();

table
.set_content_arrangement(ContentArrangement::Dynamic)
.load_preset(NOTHING)
Expand All @@ -249,27 +261,26 @@ async fn describe_cmd(args: &OperatorDescribeArgs) -> Result<String, CmdError> {
.add_row(vec!["TEST VERSIONS", test_versions_string.as_str()])
.add_row(vec!["DEV VERSIONS", dev_versions_string.as_str()]);

Ok(table.to_string())
let mut output = cli.output();

output
.add_command_hint(
format!("stackablectl operator install {}", args.operator_name),
"install the operator",
)
.set_output(table.to_string());

Ok(output.render())
}
OutputType::Json => serde_json::to_string(&versions_list).context(JsonOutputFormatSnafu),
OutputType::Yaml => serde_yaml::to_string(&versions_list).context(YamlOutputFormatSnafu),
}
}

#[instrument]
async fn install_cmd(args: &OperatorInstallArgs, common_args: &Cli) -> Result<String, CmdError> {
async fn install_cmd(args: &OperatorInstallArgs, cli: &Cli) -> Result<String, CmdError> {
info!("Installing operator(s)");

println!(
"Installing {} {}",
args.operators.len(),
if args.operators.len() == 1 {
"operator"
} else {
"operators"
}
);

args.local_cluster
.install_if_needed(None)
.await
Expand All @@ -290,19 +301,28 @@ async fn install_cmd(args: &OperatorInstallArgs, common_args: &Cli) -> Result<St
};
}

Ok(format!(
"Installed {} {}",
args.operators.len(),
if args.operators.len() == 1 {
"operator"
} else {
"operators"
}
))
let mut output = cli.output();

output
.add_command_hint(
"stackablectl operator installed [OPTIONS]",
"list installed operators",
)
.set_output(format!(
"Installed {} {}",
args.operators.len(),
if args.operators.len() == 1 {
"operator"
} else {
"operators"
}
));

Ok(output.render())
}

#[instrument]
fn uninstall_cmd(args: &OperatorUninstallArgs, common_args: &Cli) -> Result<String, CmdError> {
fn uninstall_cmd(args: &OperatorUninstallArgs, cli: &Cli) -> Result<String, CmdError> {
info!("Uninstalling operator(s)");

for operator in &args.operators {
Expand All @@ -311,19 +331,28 @@ fn uninstall_cmd(args: &OperatorUninstallArgs, common_args: &Cli) -> Result<Stri
.context(HelmSnafu)?;
}

Ok(format!(
"Uninstalled {} {}",
args.operators.len(),
if args.operators.len() == 1 {
"operator"
} else {
"operators"
}
))
let mut output = cli.output();

output
.add_command_hint(
"stackablectl operator installed [OPTIONS]",
"list remaining installed operators",
)
.set_output(format!(
"Uninstalled {} {}",
args.operators.len(),
if args.operators.len() == 1 {
"operator"
} else {
"operators"
}
));

Ok(output.render())
}

#[instrument]
fn installed_cmd(args: &OperatorInstalledArgs, common_args: &Cli) -> Result<String, CmdError> {
fn installed_cmd(args: &OperatorInstalledArgs, cli: &Cli) -> Result<String, CmdError> {
debug!("Listing installed operators");

type ReleaseList = IndexMap<String, HelmRelease>;
Expand Down Expand Up @@ -368,7 +397,20 @@ fn installed_cmd(args: &OperatorInstalledArgs, common_args: &Cli) -> Result<Stri
]);
}

Ok(table.to_string())
let mut output = cli.output();

output
.add_command_hint(
"stackablectl operator install [OPTIONS] <OPERATORS>...",
"install one or more additional operators",
)
.add_command_hint(
"stackablectl operator uninstall [OPTIONS] <OPERATORS>...",
"uninstall one or more operators",
)
.set_output(table.to_string());

Ok(output.render())
}
OutputType::Json => Ok(serde_json::to_string(&installed).context(JsonOutputFormatSnafu)?),
OutputType::Yaml => Ok(serde_yaml::to_string(&installed).context(YamlOutputFormatSnafu)?),
Expand Down
Loading

0 comments on commit 817f868

Please sign in to comment.