-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from charley04310/main
feat: implement first version of CLI using clap and YAML config file …
- Loading branch information
Showing
6 changed files
with
129 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[workspace] | ||
members = [ "src/api","src/vmm"] | ||
members = [ "src/api","src/vmm", "src/cli"] | ||
resolver = "2" |
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,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" |
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,4 @@ | ||
language: rust | ||
env_path: .env | ||
code_path: src | ||
log_level: debug |
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,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(()) | ||
} |
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,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, | ||
} |
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,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) | ||
} |