-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cli for scaffolding a new challenge
Refs: #555
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "challenge-cli" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
clap = { version = "4.2.5", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# CLI for WrongSecrets | ||
|
||
## Introduction | ||
|
||
At the moment the CLI only serves one purpose: creating a new challenge. In the future more options can be added. | ||
|
||
## Usage | ||
|
||
```shell | ||
./challenge-cli | ||
``` | ||
|
||
will print: | ||
|
||
```shell | ||
A CLI for WrongSecrets | ||
|
||
Usage: challenge-cli <COMMAND> | ||
|
||
Commands: | ||
challenge Create a new challenge | ||
help Print this message or the help of the given subcommand(s) | ||
|
||
Options: | ||
-h, --help Print help | ||
``` | ||
|
||
## Building | ||
|
||
First install [Rust](https://www.rust-lang.org/tools/install). Then open a terminal and type: | ||
|
||
```shell | ||
cd cli | ||
cargo build | ||
target/debug/challenge-cli | ||
``` | ||
|
||
## Todo | ||
|
||
- Add option to pass in the project directory | ||
- Create the directory structure for a new challenge | ||
- Add GitHub actions to build binary for the different platforms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Later on we can read this from the Github repository to make it more flexible | ||
// cache the values locally and add a flag `--force` to force reading the values again | ||
// Other option is to include a text file attached in a zip file. This makes it a bit more | ||
// error prone as we need to have that file in the same directory. | ||
// Other option is to have these files as part of the source code of wrongsecrets as you need | ||
// to pass the project folder anyway. Otherwise generating a new challenge makes no sense ;-) | ||
|
||
use std::fmt; | ||
|
||
#[derive(clap::ValueEnum, Clone, Debug)] | ||
pub enum Technology { | ||
Git, | ||
Docker, | ||
ConfigMaps, | ||
Secrets, | ||
Vault, | ||
Logging, | ||
Terraform, | ||
CSI, | ||
CICD, | ||
PasswordManager, | ||
Cryptography, | ||
Binary, | ||
Frontend, | ||
IAM, | ||
Web3, | ||
Documentation, | ||
} | ||
|
||
#[derive(clap::ValueEnum, Clone, Debug)] | ||
pub enum Difficulty { | ||
Easy, | ||
Normal, | ||
Hard, | ||
Expert, | ||
Master, | ||
} | ||
|
||
impl fmt::Display for Difficulty { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{:?}", self) | ||
} | ||
} | ||
|
||
impl fmt::Display for Technology { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{:?}", self) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use clap::{arg, Command}; | ||
|
||
use crate::enums::{Difficulty, Technology}; | ||
|
||
mod enums; | ||
|
||
fn cli() -> Command { | ||
Command::new("cli") | ||
.about("A CLI for WrongSecrets") | ||
.subcommand_required(true) | ||
.arg_required_else_help(true) | ||
.allow_external_subcommands(true) | ||
.subcommand( | ||
Command::new("challenge") | ||
.about("Create a new challenge") | ||
.arg_required_else_help(true) | ||
.arg( | ||
arg!(--"difficulty" <DIFFICULTY>) | ||
.short('d') | ||
.num_args(0..=1) | ||
.value_parser(clap::builder::EnumValueParser::<Difficulty>::new()) | ||
.num_args(0..=1) | ||
.default_value("easy") | ||
) | ||
.arg( | ||
arg!(--"technology" <TECHNOLOGY>) | ||
.short('t') | ||
.value_parser(clap::builder::EnumValueParser::<Technology>::new()) | ||
.num_args(0..=1) | ||
.require_equals(true) | ||
.default_value("git") | ||
) | ||
) | ||
} | ||
|
||
fn main() { | ||
let matches = cli().get_matches(); | ||
|
||
match matches.subcommand() { | ||
Some(("challenge", sub_matches)) => { | ||
println!( | ||
"Create new challenge with difficulty: {}", | ||
sub_matches.get_one::<Difficulty>("difficulty").expect("") | ||
); | ||
} | ||
_ => unreachable!() | ||
} | ||
} |