Skip to content

Commit

Permalink
refactor: better structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Kek5chen committed Jun 7, 2024
1 parent cde0bb6 commit 1d0ba48
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 273 deletions.
88 changes: 88 additions & 0 deletions vickyctl/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use clap::{Args, Parser, Subcommand};
use uuid::Uuid;

// TODO: Add abouts to arguments
#[derive(Parser, Debug, Clone)]
pub struct AppContext {
#[clap(env)]
pub vicky_url: String,

#[clap(env)]
pub vicky_token: String,

#[clap(long)]
pub humanize: bool,
}

#[derive(Parser, Debug, Clone)]
pub struct TaskData {
#[clap(short, long)]
pub name: String,
#[clap(long)]
pub lock_name: Vec<String>,
#[clap(long)]
pub lock_type: Vec<String>,
#[clap(long)]
pub flake_url: String,
#[clap(long)]
pub flake_arg: Vec<String>,
#[clap(long)]
pub features: Vec<String>,
}

#[derive(Subcommand, Debug)]
pub enum TaskCommands {
Create(TaskData),
// Logs, // TODO: could add this later
Claim { features: Vec<String> },
Finish { id: Uuid, status: String },
}

#[derive(Args, Debug)]
#[command(version, about = "Manage tasks on the vicky delegation server", long_about = None)]
pub struct TaskArgs {
#[command(subcommand)]
pub commands: TaskCommands,

#[command(flatten)]
pub ctx: AppContext,
}

#[derive(Args, Debug)]
#[command(version, about = "Show all tasks vicky is managing", long_about = None)]
pub struct TasksArgs {
#[command(flatten)]
pub ctx: AppContext,
}

#[derive(Args, Debug)]
#[command(version, about = "Show all poisoned locks vicky is managing", long_about = None)]
pub struct LocksArgs {
#[command(flatten)]
pub ctx: AppContext,
#[clap(long)]
pub active: bool,
#[clap(long)]
pub poisoned: bool,
}

#[derive(Args, Debug)]
#[command(version, about = "Show all poisoned locks vicky is managing", long_about = None)]
pub struct ResolveArgs {
#[command(flatten)]
pub ctx: AppContext,
#[clap(long)]
pub all: bool,
#[clap(long,short)]
pub task_id: Option<String>,
}

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub enum Cli {
Task(TaskArgs),
Tasks(TasksArgs),
Locks(LocksArgs),
Resolve(ResolveArgs),
}

6 changes: 4 additions & 2 deletions vickyctl/src/http_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::AppContext;
use crate::error::Error;
use reqwest::blocking::Client;
use reqwest::header::{HeaderMap, AUTHORIZATION};
use crate::error::Error;
use reqwest::StatusCode;
use yansi::Paint;
use crate::cli::AppContext;

pub fn prepare_client(ctx: &AppContext) -> Result<Client, Error> {
let mut default_headers = HeaderMap::new();
Expand Down
2 changes: 1 addition & 1 deletion vickyctl/src/humanize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::process::{Command, Stdio};

use log::debug;
use which::which;
use crate::cli::AppContext;

use crate::AppContext;
use crate::error::Error;

pub fn ensure_jless(needed_for: &str) -> Result<(), Error> {
Expand Down
52 changes: 52 additions & 0 deletions vickyctl/src/locks/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::cli::AppContext;
use crate::error::Error;
use crate::http_client::prepare_client;
use crate::locks::types::{LockType, PoisonedLock};

pub fn get_locks_endpoint(lock_type: LockType, detailed: bool) -> &'static str {
match lock_type {
LockType::Poisoned => match detailed {
false => "api/v1/locks/poisoned",
true => "api/v1/locks/poisoned_detailed",
},
LockType::Active => "api/v1/locks/active",
}
}

pub fn fetch_locks_raw(
ctx: &AppContext,
lock_type: LockType,
detailed: bool,
) -> Result<String, Error> {
let client = prepare_client(ctx)?;
let request = client
.get(format!(
"{}/{}",
ctx.vicky_url,
get_locks_endpoint(lock_type, detailed)
))
.build()?;
let response = client.execute(request)?.error_for_status()?;

let locks = response.text()?;
Ok(locks)
}

pub fn fetch_detailed_poisoned_locks(ctx: &AppContext) -> Result<Vec<PoisonedLock>, Error> {
let raw_locks = fetch_locks_raw(ctx, LockType::Poisoned, true)?;
let locks: Vec<PoisonedLock> = serde_json::from_str(&raw_locks)?;
Ok(locks)
}

pub fn unlock_lock(ctx: &AppContext, lock_to_clear: &PoisonedLock) -> Result<(), Error> {
let client = prepare_client(ctx)?;
let request = client
.patch(format!(
"{}/api/v1/locks/unlock/{}",
ctx.vicky_url,
lock_to_clear.id()
))
.build()?;
client.execute(request)?.error_for_status()?;
Ok(())
}
6 changes: 6 additions & 0 deletions vickyctl/src/locks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod http;
mod tui;
mod types;

pub use tui::resolve_lock;
pub use tui::show_locks;
Loading

0 comments on commit 1d0ba48

Please sign in to comment.