Skip to content

Commit

Permalink
feat: add cli for scaffolding a new challenge
Browse files Browse the repository at this point in the history
Refs: #555
  • Loading branch information
Nanne Baars committed May 2, 2023
1 parent 3e2ad6f commit 8902391
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cli/Cargo.toml
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"] }
42 changes: 42 additions & 0 deletions cli/README.md
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
50 changes: 50 additions & 0 deletions cli/src/enums.rs
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)
}
}

48 changes: 48 additions & 0 deletions cli/src/main.rs
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!()
}
}

0 comments on commit 8902391

Please sign in to comment.