Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.

[Merged by Bors] - Support generation of shell completions #54

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- Support generation of shell completions ([#54](https://github.com/stackabletech/stackablectl/pull/54))

### Changed

- Update Rust and Go libs ([#46](https://github.com/stackabletech/stackablectl/pull/46))
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository = "https://github.com/stackabletech/stackablectl"
[dependencies]
cached = "0.37"
clap = { version = "3.2", features = ["derive", "cargo"] }
clap_complete = "3.2"
env_logger = "0.9"
indexmap = { version = "1.9", features = ["serde"] }
lazy_static = "1.4"
Expand Down
70 changes: 60 additions & 10 deletions src/arguments.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,72 @@
use crate::{operator::CliCommandOperator, release::CliCommandRelease, stack::CliCommandStack};
use clap::{ArgEnum, Parser};
use clap::{ArgEnum, Command, Parser, ValueHint};
use clap_complete::{generate, Generator, Shell};
use log::LevelFilter;
use std::io;

#[derive(Parser)]
#[clap(author, version, about)]
pub struct CliArgs {
#[clap(subcommand)]
pub cmd: CliCommand,

/// Log level. One of Error, Warn, Info, Debug or Trace
#[clap(short, long, default_value = "Info")]
pub log_level: LevelFilter,
/// Log level.
#[clap(short, long, arg_enum, default_value = "info")]
pub log_level: LogLevel,

/// Namespace where to deploy the products and operators
#[clap(short, long, default_value = "default")]
#[clap(short, long, default_value = "default", value_hint = ValueHint::Other)]
pub namespace: String,

/// Overwrite the URL of the stable helm repo
///
/// If you don't have access to the Stackable Helm repos you can mirror the repo and provide the URL here
/// (e.g. <https://my.repo/repository/stackable-stable/>).
#[clap(
long,
default_value = "https://repo.stackable.tech/repository/helm-stable"
default_value = "https://repo.stackable.tech/repository/helm-stable",
value_hint = ValueHint::Url,
)]
pub helm_repo_stackable_stable: String,

/// Overwrite the URL of the test helm repo
///
/// If you don't have access to the Stackable Helm repos you can mirror the repo and provide the URL here
/// (e.g. <https://my.repo/repository/stackable-test/>).
#[clap(
long,
default_value = "https://repo.stackable.tech/repository/helm-test"
default_value = "https://repo.stackable.tech/repository/helm-test",
value_hint = ValueHint::Url,
)]
pub helm_repo_stackable_test: String,

/// Overwrite the URL of the dev helm repo
///
/// If you don't have access to the Stackable Helm repos you can mirror the repo and provide the URL here
/// (e.g. <https://my.repo/repository/stackable-dev/>).
#[clap(
long,
default_value = "https://repo.stackable.tech/repository/helm-dev"
default_value = "https://repo.stackable.tech/repository/helm-dev",
value_hint = ValueHint::Url,
)]
pub helm_repo_stackable_dev: String,

/// Adds a YAML file containing custom releases
///
/// If you don't have access to the Stackable GitHub repos or you want to maintain your own releases you can specify additional YAML files containing release information.
/// Have a look [here](https://raw.githubusercontent.com/stackabletech/stackablectl/main/releases.yaml) for the structure.
/// Can either be an URL or a path to a file e.g. `https://my.server/my-releases.yaml` or '/etc/my-releases.yaml' or `C:\Users\Sebastian\my-releases.yaml`.
/// Can be specified multiple times.
#[clap(long, multiple_occurrences(true))]
#[clap(long, multiple_occurrences(true), value_hint = ValueHint::FilePath)]
pub additional_release_files: Vec<String>,

/// Adds a YAML file containing custom stacks
///
/// If you don't have access to the Stackable GitHub repos or you want to maintain your own stacks you can specify additional YAML files containing stack information.
/// Have a look [here](https://raw.githubusercontent.com/stackabletech/stackablectl/main/stacks.yaml) for the structure.
/// Can either be an URL or a path to a file e.g. `https://my.server/my-stacks.yaml` or '/etc/my-stacks.yaml' or `C:\Users\Sebastian\my-stacks.yaml`.
/// Can be specified multiple times.
#[clap(long, multiple_occurrences(true))]
#[clap(long, multiple_occurrences(true), value_hint = ValueHint::FilePath)]
pub additional_stack_files: Vec<String>,
}

Expand All @@ -68,6 +83,9 @@ pub enum CliCommand {
/// This subcommand interacts with stacks, which are ready-to-use combinations of products.
#[clap(subcommand, alias("s"), alias("st"))]
Stack(CliCommandStack),

/// Output shell completion code for the specified shell.
Completion(CliCommandCompletion),
}

#[derive(Clone, Parser, ArgEnum)]
Expand All @@ -76,3 +94,35 @@ pub enum OutputType {
Json,
Yaml,
}

#[derive(Clone, Copy, Parser, Debug, ArgEnum)]
pub enum LogLevel {
Error,
Warn,
Info,
Debug,
Trace,
}

impl From<LogLevel> for LevelFilter {
fn from(val: LogLevel) -> Self {
match val {
LogLevel::Error => LevelFilter::Error,
LogLevel::Warn => LevelFilter::Warn,
LogLevel::Info => LevelFilter::Info,
LogLevel::Debug => LevelFilter::Debug,
LogLevel::Trace => LevelFilter::Trace,
}
}
}

#[derive(Parser)]
pub struct CliCommandCompletion {
// Shell to generate the completions for
#[clap(arg_enum, value_parser)]
pub shell: Shell,
}

pub fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}
2 changes: 1 addition & 1 deletion src/helm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn handle_common_cli_args(args: &CliArgs) {
args.helm_repo_stackable_dev.clone(),
);

*(LOG_LEVEL.lock().unwrap()) = args.log_level;
*(LOG_LEVEL.lock().unwrap()) = args.log_level.into();

let namespace = &args.namespace;
if namespace != "default" {
Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::arguments::CliCommand;
use arguments::CliArgs;
use clap::Parser;
use clap::{IntoApp, Parser};

mod arguments;
mod helm;
Expand Down Expand Up @@ -37,7 +37,7 @@ fn main() {
env_logger::builder()
.format_timestamp(None)
.format_target(false)
.filter_level(args.log_level)
.filter_level(args.log_level.into())
.init();
helm::handle_common_cli_args(&args);
release::handle_common_cli_args(&args);
Expand All @@ -47,5 +47,9 @@ fn main() {
CliCommand::Operator(command) => command.handle(),
CliCommand::Release(command) => command.handle(),
CliCommand::Stack(command) => command.handle(),
CliCommand::Completion(command) => {
let mut cmd = CliArgs::command();
arguments::print_completions(command.shell, &mut cmd);
}
}
}
10 changes: 6 additions & 4 deletions src/operator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{arguments::OutputType, helm, helm::HELM_REPOS, kind, AVAILABLE_OPERATORS};
use clap::Parser;
use clap::{Parser, ValueHint};
use indexmap::IndexMap;
use log::{info, warn};
use serde::Serialize;
Expand All @@ -16,6 +16,7 @@ pub enum CliCommandOperator {
/// Show details of a specific operator
#[clap(alias("desc"))]
Describe {
#[clap(value_hint = ValueHint::Other)]
operator: String,
#[clap(short, long, arg_enum, default_value = "text")]
output: OutputType,
Expand All @@ -26,7 +27,7 @@ pub enum CliCommandOperator {
/// Space separated list of operators to install.
/// Must have the form `name[=version]` e.g. `superset`, `superset=0.3.0`, `superset=0.3.0-nightly` or `superset=0.3.0-pr123`.
/// You can get the available versions with `stackablectl operator list` or `stackablectl operator describe superset`
#[clap(multiple_occurrences(true), required = true)]
#[clap(multiple_occurrences(true), required = true, value_hint = ValueHint::Other)]
operators: Vec<Operator>,

/// If specified a local kubernetes cluster consisting of 4 nodes for testing purposes will be created.
Expand All @@ -39,15 +40,16 @@ pub enum CliCommandOperator {
#[clap(
long,
default_value = "stackable-data-platform",
requires = "kind-cluster"
requires = "kind-cluster",
value_hint = ValueHint::Other,
)]
kind_cluster_name: String,
},
/// Uninstall a operator
#[clap(alias("un"))]
Uninstall {
/// Space separated list of operators to uninstall.
#[clap(multiple_occurrences(true), required = true)]
#[clap(multiple_occurrences(true), required = true, value_hint = ValueHint::Other)]
operators: Vec<String>,
},
/// List installed operators
Expand Down
14 changes: 8 additions & 6 deletions src/release.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{arguments::OutputType, helpers, kind, operator, operator::Operator, CliArgs};
use cached::proc_macro::cached;
use clap::{ArgGroup, Parser};
use clap::{ArgGroup, Parser, ValueHint};
use indexmap::IndexMap;
use lazy_static::lazy_static;
use log::{error, info, warn};
Expand All @@ -24,6 +24,7 @@ pub enum CliCommandRelease {
/// Show details of a specific release
#[clap(alias("desc"))]
Describe {
#[clap(value_hint = ValueHint::Other)]
release: String,
#[clap(short, long, arg_enum, default_value = "text")]
output: OutputType,
Expand All @@ -37,17 +38,17 @@ pub enum CliCommandRelease {
))]
Install {
/// Name of the release to install
#[clap(required = true)]
#[clap(required = true, value_hint = ValueHint::Other)]
release: String,

/// Whitelist of product operators to install.
/// Mutually exclusive with `--exclude-products`
#[clap(short, long)]
#[clap(short, long, value_hint = ValueHint::Other)]
include_products: Vec<String>,

/// Blacklist of product operators to install.
/// Mutually exclusive with `--include-products`
#[clap(short, long)]
#[clap(short, long, value_hint = ValueHint::Other)]
exclude_products: Vec<String>,

/// If specified a local kubernetes cluster consisting of 4 nodes for testing purposes will be created.
Expand All @@ -60,15 +61,16 @@ pub enum CliCommandRelease {
#[clap(
long,
default_value = "stackable-data-platform",
requires = "kind-cluster"
requires = "kind-cluster",
value_hint = ValueHint::Other,
)]
kind_cluster_name: String,
},
/// Uninstall a release
#[clap(alias("un"))]
Uninstall {
/// Name of the release to uninstall
#[clap(required = true)]
#[clap(required = true, value_hint = ValueHint::Other)]
release: String,
},
}
Expand Down