Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement first version of CLI using clap and YAML config file … #14

Merged
merged 2 commits into from
Apr 8, 2024
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = [ "src/api","src/vmm"]
members = [ "src/api","src/vmm", "src/cli"]
resolver = "2"
15 changes: 15 additions & 0 deletions src/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "cli"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.3", features = ["derive"] }
toml = "0.8.12"
tokio = { version = "1.36.0", features = ["full"] }
serde = { version = "1.0.197", features = ["derive"] }
serde_yaml = "0.9.34"
schemars = "0.8.16"
serde_json = "1.0.115"
4 changes: 4 additions & 0 deletions src/cli/config/config.template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: rust
env_path: .env
code_path: src
log_level: debug
71 changes: 71 additions & 0 deletions src/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use clap::Parser;
mod types;
mod utils;
use std::io::{self};
use types::{Config, Language, LogLevel};
use utils::load_config;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[command(subcommand)]
command: Commands,
}

#[derive(Parser, Debug)]
enum Commands {
Configure {
#[arg(short, long)]
config_path: String,
},
Status {},
Apply {},
Kill {},
}

#[tokio::main]

async fn main() -> io::Result<()> {
let args = Args::parse();

match args.command {
Commands::Configure { config_path } => {
let config: Config = load_config(&config_path).unwrap();

println!("Configuration from YAML file:");
println!(
"Language: {}",
match config.language {
Language::Rust => "Rust",
Language::Python => "Python",
Language::Node => "Node",
}
);
println!("Env Path: {}", config.env_path);
println!("Code Path: {}", config.code_path);
println!(
"Log Level: {}",
match config.log_level {
LogLevel::Debug => "Debug",
LogLevel::Info => "Info",
LogLevel::Warn => "Warn",
LogLevel::Error => "Error",
}
);
}

Commands::Status {} => {
println!("Getting status");
}

Commands::Apply {} => {
println!("Applying configuration");
}

Commands::Kill {} => {
println!("Killing configuration");
}
}

Ok(())
}
27 changes: 27 additions & 0 deletions src/cli/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use clap::ValueEnum;
use serde::Deserialize;

#[derive(Clone, Debug, ValueEnum, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Language {
Rust,
Python,
Node,
}

#[derive(Clone, Debug, ValueEnum, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
Debug,
Info,
Warn,
Error,
}

#[derive(Deserialize, Debug)]
pub struct Config {
pub language: Language,
pub env_path: String,
pub code_path: String,
pub log_level: LogLevel,
}
11 changes: 11 additions & 0 deletions src/cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::types::Config;
use std::fs::File;
use std::io::{self, Read};

pub fn load_config(config_path: &str) -> io::Result<Config> {
let mut file = File::open(config_path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let config: Config = serde_yaml::from_str(&contents).unwrap();
Ok(config)
}