Derive debug for subcommands and accept &str

Instead of &Option<String>, as it will always be passed in
This commit is contained in:
Aadi Desai 2023-08-30 13:30:16 +01:00
parent 25ec3b53d7
commit e2c5de4a0a
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM
14 changed files with 126 additions and 93 deletions

View file

@ -10,9 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Address input parameter for fn `process` in `impl`s - Address input parameter for fn `process` in `impl`s
- Derive `Debug` for subcommands in `/src`
### Changed ### Changed
- fn `process` in `impl`s takes `&str` instead of `&Option<String>`
### Deprecated ### Deprecated
### Removed ### Removed
@ -21,7 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security ### Security
## 0.1.0 - 2023-05-08 ## [0.1.0] - 2023-05-08
### Added ### Added

View file

@ -8,4 +8,10 @@ license = "Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = { version = "4.2.7", features = ["derive"] } clap = { version = "4.4.1", features = ["derive"] }
const_format = "0.2.31"
reqwest = { version = "0.11.20", features = ["blocking", "json"] }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.105"
structstruck = "0.4.1"
thiserror = "1.0.47"

View file

@ -1,45 +1,66 @@
use clap::Subcommand; use clap::Subcommand;
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Account { pub enum Account {
/// Get information about your account /// Get information about your account
GetInfo, GetInfo {
/// Email of your omg.lol account
email: String,
},
/// Get all addresses associated with your account /// Get all addresses associated with your account
GetAddresses, GetAddresses {
/// Email of your omg.lol account
email: String,
},
/// Get the name associated with your account /// Get the name associated with your account
GetName, GetName {
/// Email of your omg.lol account
email: String,
},
/// Update the name associated with your account /// Update the name associated with your account
SetName { SetName {
/// Email of your omg.lol account
email: String,
/// Name to set for your account /// Name to set for your account
name: String, name: String,
}, },
/// Get all sessions associated with your account /// Get all sessions associated with your account
GetSessions, GetSessions {
/// Email of your omg.lol account
email: String,
},
/// Delete a session from your account /// Delete a session from your account
RemoveSession { RemoveSession {
/// Email of your omg.lol account
email: String,
/// ID of the session to remove /// ID of the session to remove
session_id: String, session_id: String,
}, },
/// Get settings associated with your account /// Get settings associated with your account
GetSettings, GetSettings {
/// Email of your omg.lol account
email: String,
},
/// Update settings associated with your account /// Update settings associated with your account
SetSettings { SetSettings {
/// Email of your omg.lol account
email: String,
/// Temporary JSON data input /// Temporary JSON data input
json_data: String, json_data: String,
}, },
} }
impl Account { impl Account {
pub fn process(&self, _email: &str) { pub fn process(&self) {
match self { match self {
Account::GetInfo => todo!(), Account::GetInfo { email: _ } => todo!(),
Account::GetAddresses => todo!(), Account::GetAddresses { email: _ } => todo!(),
Account::GetName => todo!(), Account::GetName { email: _ } => todo!(),
Account::SetName { name: _ } => todo!(), Account::SetName { email: _, name: _ } => todo!(),
Account::GetSessions => todo!(), Account::GetSessions { email: _ } => todo!(),
Account::RemoveSession { session_id: _ } => todo!(), Account::RemoveSession { email: _, session_id: _ } => todo!(),
Account::GetSettings => todo!(), Account::GetSettings { email: _ } => todo!(),
Account::SetSettings { json_data: _ } => todo!(), Account::SetSettings { email: _, json_data: _ } => todo!(),
} }
} }
} }

View file

@ -1,19 +1,34 @@
use clap::Subcommand; use clap::Subcommand;
use serde::Deserialize;
#[derive(Subcommand)] use crate::{get, get_auth};
#[derive(Debug, Subcommand)]
pub enum Address { pub enum Address {
/// Get information about the availability of an address /// Get information about the availability of an address
IsAvailable, IsAvailable {
/// Address to get availability of
address: String,
},
/// Get the expiration date for an address /// Get the expiration date for an address
GetExpiry, GetExpiry {
/// Address to get availability of
address: String,
},
/// Get limited (public) information about an address (no auth required) /// Get limited (public) information about an address (no auth required)
GetPublicInfo, GetPublicInfo {
///Get comprehensive information about an address /// Address to get availability of
GetInfo, address: String,
},
/// Get comprehensive information about an address
GetInfo {
/// Address to get availability of
address: String,
},
} }
impl Address { impl Address {
pub fn process(&self, _address: &Option<String>) { pub fn process(&self, api_key: &str) -> Result<AddressResponse, reqwest::Error> {
match self { match self {
Address::IsAvailable => todo!(), Address::IsAvailable => todo!(),
Address::GetExpiry => todo!(), Address::GetExpiry => todo!(),

View file

@ -1,6 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Dns { pub enum Dns {
/// Get a list of all your DNS records /// Get a list of all your DNS records
GetRecords, GetRecords,
@ -22,7 +22,7 @@ pub enum Dns {
} }
impl Dns { impl Dns {
pub fn process(&self, _address: &Option<String>) { pub fn process(&self, _address: &str) {
match self { match self {
Dns::GetRecords => todo!(), Dns::GetRecords => todo!(),
Dns::AddRecord { json_data: _ } => todo!(), Dns::AddRecord { json_data: _ } => todo!(),

View file

@ -1,6 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Email { pub enum Email {
/// Get forwarding address(es) /// Get forwarding address(es)
GetForwards, GetForwards,
@ -12,7 +12,7 @@ pub enum Email {
} }
impl Email { impl Email {
pub fn process(&self, _address: &Option<String>) { pub fn process(&self, _address: &str) {
match self { match self {
Email::GetForwards => todo!(), Email::GetForwards => todo!(),
Email::SetForwards { json_data: _ } => todo!(), Email::SetForwards { json_data: _ } => todo!(),

View file

@ -3,7 +3,7 @@ use clap::Subcommand;
pub mod account; pub mod account;
pub use account::Account; pub use account::Account;
pub mod address; pub mod address;
pub use address::Address; pub use address::{Address, AddressResponse, GetExpiry, GetInfo, GetPublicInfo, IsAvailable};
pub mod dns; pub mod dns;
pub use dns::Dns; pub use dns::Dns;
pub mod email; pub mod email;
@ -28,56 +28,38 @@ pub use weblog::Weblog;
pub enum Commands { pub enum Commands {
/// Get information and make changes to your account /// Get information and make changes to your account
Account { Account {
/// Email of your omg.lol account, needed for Account commands only
#[clap(short, long, global = true)]
email: String,
#[clap(subcommand)] #[clap(subcommand)]
command: Account, command: Account,
}, },
/// Get information and make changes to your addresses /// Get information and make changes to your addresses
Address { Address {
/// omg.lol address to interact with, overrides config and env. var: (OMGLOL_USERNAME)
#[clap(short, long, global = true)]
address: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Address, command: Address,
}, },
/// Save your omg.lol API key to the config.json (Rather than using the env. var: OMGLOL_API_KEY) /// Save omg.lol API key to config.toml, will prompt (vs env. var: OMGLOL_API_KEY)
Auth { Auth {
/// API key to save to config.json /// Linked omg.lol name for API key
api_key: String, name: String,
}, },
/// Get the address directory, consisting of addresses that have opted-in to be listed /// Get the address directory, consisting of addresses that have opted-in to be listed
Directory, Directory,
/// Adjust the switchboard / DNS records for your omg.lol address /// Adjust the switchboard / DNS records for your omg.lol address
Dns { Dns {
/// omg.lol address to interact with, overrides config and env. var: (OMGLOL_USERNAME)
#[clap(short, long, global = true)]
address: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Dns, command: Dns,
}, },
/// Manage the email configuration for an omg.lol address /// Manage the email configuration for an omg.lol address
Email { Email {
/// omg.lol address to interact with, overrides config and env. var: (OMGLOL_USERNAME)
#[clap(short, long, global = true)]
address: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Email, command: Email,
}, },
/// Manage your /now page /// Manage your /now page
Now { Now {
/// omg.lol address to interact with, overrides config and env. var: (OMGLOL_USERNAME)
#[clap(short, long, global = true)]
address: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Now, command: Now,
}, },
/// Manage the pastebin for an omg.lol address /// Manage the pastebin for an omg.lol address
Pastebin { Pastebin {
/// omg.lol address to interact with, overrides config and env. var: (OMGLOL_USERNAME)
#[clap(short, long, global = true)]
address: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Pastebin, command: Pastebin,
}, },
@ -92,9 +74,6 @@ pub enum Commands {
}, },
/// Manage PURLs (Persistent URLs) for your omg.lol address /// Manage PURLs (Persistent URLs) for your omg.lol address
Purl { Purl {
/// omg.lol address to interact with, overrides config and env. var: (OMGLOL_USERNAME)
#[clap(short, long, global = true)]
address: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Purl, command: Purl,
}, },
@ -102,33 +81,26 @@ pub enum Commands {
Service, Service,
/// Manage the statuslog for an omg.lol address /// Manage the statuslog for an omg.lol address
Status { Status {
/// omg.lol address to interact with, overrides config and env. var: (OMGLOL_USERNAME)
#[clap(short, long, global = true)]
address: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Status, command: Status,
}, },
/// Set a default omg.lol address (and API key) from saved addresses
Switch {
/// new default omg.lol address, leave blank to list available addresses
address: Option<String>,
},
/// Manage omg.lol profile themes /// Manage omg.lol profile themes
Theme { Theme {
/// omg.lol address to interact with, overrides config and env. var: (OMGLOL_USERNAME)
#[clap(short, long, global = true)]
address: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Theme, command: Theme,
}, },
/// Manage profile page and web stuff for an omg.lol address /// Manage profile page and web stuff for an omg.lol address
Web { Web {
/// omg.lol address to interact with, overrides config and env. var: (OMGLOL_USERNAME)
#[clap(short, long, global = true)]
address: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Web, command: Web,
}, },
/// Manage the weblog for an omg.lol address /// Manage the weblog for an omg.lol address
Weblog { Weblog {
/// omg.lol address to interact with, overrides config and env. var: (OMGLOL_USERNAME)
#[clap(short, long, global = true)]
address: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Weblog, command: Weblog,
}, },
@ -136,23 +108,39 @@ pub enum Commands {
impl Commands { impl Commands {
// TBD: Is there a more idiomatic / succinct approach to this? // TBD: Is there a more idiomatic / succinct approach to this?
pub fn process(&self, _address: &Option<String>) { pub fn process(
&self,
_address: &str,
api_key: &str,
) -> Result<CommandResponse, reqwest::Error> {
match self { match self {
Commands::Account { email, command } => command.process(email), Commands::Account { command } => {
Commands::Address { address, command } => command.process(address), command.process();
Commands::Auth { api_key: _ } => todo!(), Ok(CommandResponse::Todo(()))
}
Commands::Address { command } => {
Ok(CommandResponse::Address(command.process(api_key)?))
}
Commands::Auth { name } => unreachable!("{name}"),
Commands::Directory => todo!(), Commands::Directory => todo!(),
Commands::Dns { address, command } => command.process(address), Commands::Dns { command } => todo!("{command:?}"),
Commands::Email { address, command } => command.process(address), Commands::Email { command } => todo!("{command:?}"),
Commands::Now { address, command } => command.process(address), Commands::Now { command } => todo!("{command:?}"),
Commands::Pastebin { address, command } => command.process(address), Commands::Pastebin { command } => todo!("{command:?}"),
Commands::Preferences { owner: _, item: _, value: _ } => todo!(), Commands::Preferences { owner, item, value } => todo!("{owner}, {item}, {value}"),
Commands::Purl { address, command } => command.process(address), Commands::Purl { command } => todo!("{command:?}"),
Commands::Service => todo!(), Commands::Service => todo!(),
Commands::Status { address, command } => command.process(address), Commands::Status { command } => todo!("{command:?}"),
Commands::Theme { address, command } => command.process(address), Commands::Switch { address: _ } => todo!(),
Commands::Web { address, command } => command.process(address), Commands::Theme { command } => todo!("{command:?}"),
Commands::Weblog { address, command } => command.process(address), Commands::Web { command } => todo!("{command:?}"),
Commands::Weblog { command } => todo!("{command:?}"),
} }
} }
} }
#[derive(Debug)]
pub enum CommandResponse {
Todo(()),
Address(AddressResponse),
}

View file

@ -1,6 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Now { pub enum Now {
/// Get the /now page for an address /// Get the /now page for an address
Get, Get,
@ -17,7 +17,7 @@ pub enum Now {
} }
impl Now { impl Now {
pub fn process(&self, _address: &Option<String>) { pub fn process(&self, _address: &str) {
match self { match self {
Now::Get => todo!(), Now::Get => todo!(),
Now::List => todo!(), Now::List => todo!(),

View file

@ -1,6 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Pastebin { pub enum Pastebin {
/// Get a specific paste for an omg.lol address /// Get a specific paste for an omg.lol address
Get { Get {
@ -26,7 +26,7 @@ pub enum Pastebin {
} }
impl Pastebin { impl Pastebin {
pub fn process(&self, _address: &Option<String>) { pub fn process(&self, _address: &str) {
match self { match self {
Pastebin::Get { name: _ } => todo!(), Pastebin::Get { name: _ } => todo!(),
Pastebin::GetAll => todo!(), Pastebin::GetAll => todo!(),

View file

@ -1,6 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Purl { pub enum Purl {
/// Create a new PURL for an omg.lol address /// Create a new PURL for an omg.lol address
Create { Create {
@ -24,7 +24,7 @@ pub enum Purl {
} }
impl Purl { impl Purl {
pub fn process(&self, _address: &Option<String>) { pub fn process(&self, _address: &str) {
match self { match self {
Purl::Create { name: _, url: _ } => todo!(), Purl::Create { name: _, url: _ } => todo!(),
Purl::Get { name: _ } => todo!(), Purl::Get { name: _ } => todo!(),

View file

@ -1,6 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Status { pub enum Status {
/// Get a single statuslog entry for an omg.lol address /// Get a single statuslog entry for an omg.lol address
Get { Get {
@ -47,7 +47,7 @@ pub enum Status {
} }
impl Status { impl Status {
pub fn process(&self, _address: &Option<String>) { pub fn process(&self, _address: &str) {
match self { match self {
Status::Get { id: _ } => todo!(), Status::Get { id: _ } => todo!(),
Status::GetAll => todo!(), Status::GetAll => todo!(),

View file

@ -1,6 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Theme { pub enum Theme {
/// List available omg.lol profile themes /// List available omg.lol profile themes
List, List,
@ -17,7 +17,7 @@ pub enum Theme {
} }
impl Theme { impl Theme {
pub fn process(&self, _address: &Option<String>) { pub fn process(&self, _address: &str) {
match self { match self {
Theme::List => todo!(), Theme::List => todo!(),
Theme::Info { id: _ } => todo!(), Theme::Info { id: _ } => todo!(),

View file

@ -1,6 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Web { pub enum Web {
/// Get web content and information for an omg.lol address /// Get web content and information for an omg.lol address
Get, Get,
@ -22,7 +22,7 @@ pub enum Web {
} }
impl Web { impl Web {
pub fn process(&self, _address: &Option<String>) { pub fn process(&self, _address: &str) {
match self { match self {
Web::Get => todo!(), Web::Get => todo!(),
Web::Set { content: _, publish: _ } => todo!(), Web::Set { content: _, publish: _ } => todo!(),

View file

@ -1,6 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
#[derive(Subcommand)] #[derive(Debug, Subcommand)]
pub enum Weblog { pub enum Weblog {
/// Get a specific weblog entry for an omg.lol address /// Get a specific weblog entry for an omg.lol address
Get { Get {
@ -38,7 +38,7 @@ pub enum Weblog {
} }
impl Weblog { impl Weblog {
pub fn process(&self, _address: &Option<String>) { pub fn process(&self, _address: &str) {
match self { match self {
Weblog::Get { id: _ } => todo!(), Weblog::Get { id: _ } => todo!(),
Weblog::Latest => todo!(), Weblog::Latest => todo!(),