Skip to content

Commit e9bb5e9

Browse files
committed
setting up a simple CLI with clap
1 parent e53fd48 commit e9bb5e9

File tree

7 files changed

+151
-1
lines changed

7 files changed

+151
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
target/
17+
Cargo.lock

agent/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ name = "agent"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[[bin]]
7+
name = "al"
8+
path = "src/main.rs"
9+
610
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
711

812
[dependencies]
13+
clap = { version = "4.1.4", features = ["derive"] }

agent/src/cli.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use clap::{Parser, Subcommand};
2+
3+
use config::Config;
4+
use lint::Lint;
5+
use run::Run;
6+
7+
pub mod config;
8+
pub mod lint;
9+
pub mod run;
10+
11+
#[derive(Parser)]
12+
#[command(name = "al")]
13+
#[command(version = "0.0")]
14+
#[command(about = "Assembly Line build agent")]
15+
#[command(
16+
long_about = "Assembly line is a CI/CD system for building and deploying applications focused on using containers as steps on environments that support containers."
17+
)]
18+
#[command(author = None)]
19+
pub struct Cli {
20+
#[command(subcommand)]
21+
pub command: Commands,
22+
}
23+
24+
/// Define a finite list of valid sub commands
25+
#[derive(Subcommand)]
26+
pub enum Commands {
27+
/// Show/set config
28+
#[clap(name = "config")]
29+
Config(Config),
30+
/// Check a pipeline configuration file for correctness
31+
Lint(Lint),
32+
/// Execute the pipeline specified
33+
Run(Run),
34+
/// Run as a server, depends on config as to what type - local or true build agent
35+
Server,
36+
}

agent/src/cli/config.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use clap::{Parser, Subcommand};
2+
3+
/// Defines the options available to the `config` sub command,
4+
#[derive(Parser)]
5+
pub struct Config {
6+
#[command(subcommand)]
7+
command: ConfigCommand,
8+
}
9+
10+
/// Define a finite list of options that `config`'s `command` can provide
11+
#[derive(Subcommand)]
12+
pub enum ConfigCommand {
13+
/// Show the current config
14+
Get,
15+
/// Generate an example config based on a CRI-API supported runtime
16+
#[clap(name = "generate")]
17+
Generate(Generate),
18+
}
19+
20+
/// Defines options available to the `config generate` sub command
21+
#[derive(Parser)]
22+
pub struct Generate {
23+
#[command(subcommand)]
24+
command: GenerateConfigCommand,
25+
}
26+
27+
/// Select a CRI-API enabled endpoint to generate default configuration for
28+
#[derive(Subcommand)]
29+
pub enum GenerateConfigCommand {
30+
/// Generate config that communicates with a default install of Containerd
31+
Containerd,
32+
/// Generate config that communicates with a default install of CRI-O
33+
CriO,
34+
/// Generate config that communicates with a default install od Docker
35+
Docker,
36+
}
37+
38+
/// command handler for the config sub command
39+
pub fn config_commands(config: Config) {
40+
match config.command {
41+
ConfigCommand::Get => println!("GET CONFIG CALLED"),
42+
ConfigCommand::Generate(flavour) => generate_template_config(flavour),
43+
}
44+
}
45+
46+
/// Calls the generator for the selected CRI-API enabled runtime
47+
pub fn generate_template_config(flavour: Generate) {
48+
match flavour.command {
49+
GenerateConfigCommand::Containerd => generate_example_containerd_config(),
50+
GenerateConfigCommand::CriO => generate_example_crio_config(),
51+
GenerateConfigCommand::Docker => generate_example_docker_config(),
52+
}
53+
}
54+
55+
/// Generate Containerd specific config based on a default install
56+
pub fn generate_example_containerd_config() {
57+
println!("THIS IS WHERE WE OUTPUT CONTAINERD SPECIFIC CONFIG")
58+
}
59+
60+
/// Generate CRI-O specific config based on a default install
61+
pub fn generate_example_crio_config() {
62+
println!("YOU CHOSE CRI-O")
63+
}
64+
65+
/// Generate Docker specific config based on a default install
66+
pub fn generate_example_docker_config() {
67+
println!("DOCKER FOR THE WIN")
68+
}

agent/src/cli/lint.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use clap::Args;
2+
use std::path::PathBuf;
3+
4+
/// Arguments for the lint command to control behaviour
5+
#[derive(Args)]
6+
pub struct Lint {
7+
/// The path of the pipeline file to check for correctness
8+
pub path: PathBuf,
9+
}
10+
11+
/// Lint the pipeline specified by the arguments for correctness
12+
pub fn lint_command(args: Lint) {
13+
println!("YOU CHOSE TO LINT: {:?} (not yet implemented)", args.path)
14+
}

agent/src/cli/run.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use clap::Args;
2+
use std::path::PathBuf;
3+
4+
/// Arguments for the run command to control execution
5+
#[derive(Args)]
6+
pub struct Run {
7+
/// The path of the pipeline file to run locally
8+
pub path: PathBuf,
9+
}
10+
11+
/// Run the pipeline as specified by the args on the local machine
12+
pub fn run_command(args: Run) {
13+
println!("YOU CHOSE TO RUN: {:?} (not yet implemened)", args.path)
14+
}

agent/src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
use clap::Parser;
2+
3+
use cli::*;
4+
mod cli;
5+
16
fn main() {
2-
println!("Hello, world!");
7+
let cli = cli::Cli::parse();
8+
match cli.command {
9+
Commands::Server => println!("RUN AS A SERVER (not yet implemented)"),
10+
Commands::Run(run_args) => run::run_command(run_args),
11+
Commands::Lint(lint_args) => lint::lint_command(lint_args),
12+
Commands::Config(c) => config::config_commands(c),
13+
}
314
}

0 commit comments

Comments
 (0)