Skip to content

Commit

Permalink
Move thewithn CLI definition to a library.
Browse files Browse the repository at this point in the history
  • Loading branch information
noteed committed May 13, 2024
1 parent 62a75b8 commit f8c911c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 47 deletions.
59 changes: 12 additions & 47 deletions src/bin/thewithn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
//! thebacknd. The different commands are supposed to be symlinks to this single binary, which acts
//! differently depending on the symlink name.
use clap::{Parser, Subcommand};
use clap::{Parser};
use std::env;
use std::fs;
use std::path::Path;

use thebacknd::agent::cli::{Cli, Commands};
use thebacknd::agent::cli;

/// Thewithn single binary main entry point.
fn main() {
let invoked_as = env::args().nth(0).unwrap_or_else(|| String::from("unknown"));
Expand Down Expand Up @@ -36,84 +39,46 @@ fn main() {

match invoked_as {
"current-system" => {
let sub_args = CurrentSystemCmd::parse_from(args);
let sub_args = cli::CurrentSystemCmd::parse_from(args);
handle_current_system(&sub_args);
}
"desired-system" => {
let sub_args = DesiredSystemCmd::parse_from(args);
let sub_args = cli::DesiredSystemCmd::parse_from(args);
handle_desired_system(&sub_args);
}
"destroy-system" => {
let sub_args = DestroySystemCmd::parse_from(args);
let sub_args = cli::DestroySystemCmd::parse_from(args);
handle_destroy_system(&sub_args);
}
"update-system" => {
let sub_args = UpdateSystemCmd::parse_from(args);
let sub_args = cli::UpdateSystemCmd::parse_from(args);
handle_update_system(&sub_args);
}
_ => println!("Unknown symlink name: {}", invoked_as),
}
}
}

#[derive(Parser)]
#[command(name = "thewithn")]
#[command(about = "A program that behaves differently based on the symlink name or subcommands")]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
CurrentSystem(CurrentSystemCmd),
DesiredSystem(DesiredSystemCmd),
DestroySystem(DestroySystemCmd),
UpdateSystem(UpdateSystemCmd),
}

#[derive(Parser)]
struct CurrentSystemCmd {
}

#[derive(Parser)]
struct DesiredSystemCmd {
/// Provide a custom message
#[arg(short, long, default_value = "Called as 'desired-system'.")]
message: String,
}

#[derive(Parser)]
struct DestroySystemCmd {
/// Print extra details
#[arg(short, long)]
details: bool,
}

#[derive(Parser)]
struct UpdateSystemCmd {
}

fn handle_current_system(_args: &CurrentSystemCmd) {
fn handle_current_system(_args: &cli::CurrentSystemCmd) {
let current_system_link = "/run/current-system";
match fs::read_link(current_system_link) {
Ok(path) => println!("{}", path.display()),
Err(e) => eprintln!("Failed to read link {}: {}", current_system_link, e),
}
}

fn handle_desired_system(args: &DesiredSystemCmd) {
fn handle_desired_system(args: &cli::DesiredSystemCmd) {
println!("{}", args.message);
}

fn handle_destroy_system(args: &DestroySystemCmd) {
fn handle_destroy_system(args: &cli::DestroySystemCmd) {
if args.details {
println!("Called as 'destroy-system' with extra details.");
} else {
println!("Called as 'destroy-system'.");
}
}

fn handle_update_system(_args: &UpdateSystemCmd) {
fn handle_update_system(_args: &cli::UpdateSystemCmd) {
println!("Called as 'update-system'.");
}
40 changes: 40 additions & 0 deletions src/lib/thebacknd/agent/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// Command-line definition for thebacknd agent binary (called thewithn).
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(name = "thewithn")]
#[command(about = "A program that behaves differently based on the symlink name or subcommands")]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
}

#[derive(Subcommand)]
pub enum Commands {
CurrentSystem(CurrentSystemCmd),
DesiredSystem(DesiredSystemCmd),
DestroySystem(DestroySystemCmd),
UpdateSystem(UpdateSystemCmd),
}

#[derive(Parser)]
pub struct CurrentSystemCmd {
}

#[derive(Parser)]
pub struct DesiredSystemCmd {
/// Provide a custom message
#[arg(short, long, default_value = "Called as 'desired-system'.")]
pub message: String,
}

#[derive(Parser)]
pub struct DestroySystemCmd {
/// Print extra details
#[arg(short, long)]
pub details: bool,
}

#[derive(Parser)]
pub struct UpdateSystemCmd {
}
1 change: 1 addition & 0 deletions src/lib/thebacknd/agent/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod cli;
1 change: 1 addition & 0 deletions src/lib/thebacknd/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod agent;
pub mod client;

0 comments on commit f8c911c

Please sign in to comment.