-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
108 lines (100 loc) · 2.84 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
mod client;
mod config;
mod contact;
mod dirs;
mod handler;
mod keypair;
#[derive(clap::Parser)]
#[command(version, about, long_about = None)]
struct Command {
#[command(subcommand)]
resource: Resource,
}
#[derive(clap::Subcommand)]
enum Resource {
/// Manage contacts
Contact {
#[command(subcommand)]
command: ContactCommand,
},
/// Manage keypair
Keypair {
#[command(subcommand)]
command: KeypairCommand,
},
/// Manage metadata
Metadata,
/// Manage text-notes
TextNote {
#[command(subcommand)]
command: TextNoteCommand,
},
}
#[derive(clap::Subcommand)]
enum ContactCommand {
/// List contact
List,
}
#[derive(clap::Subcommand)]
enum KeypairCommand {
/// Create a keypair from a private key
Create {
#[arg(long, env)]
private_key: String,
},
/// Get the keypair
Get {
#[arg(value_enum, default_value_t = PrivateKeyOrPublicKey::default())]
private_key_or_public_key: PrivateKeyOrPublicKey,
},
}
#[derive(Clone, Default, clap::ValueEnum)]
enum PrivateKeyOrPublicKey {
PrivateKey,
#[default]
PublicKey,
}
impl PrivateKeyOrPublicKey {
fn is_private_key(&self) -> bool {
match self {
PrivateKeyOrPublicKey::PrivateKey => true,
PrivateKeyOrPublicKey::PublicKey => false,
}
}
}
#[derive(clap::Subcommand)]
enum TextNoteCommand {
/// Create a new note
Create { content: String },
/// Delete the note
Delete { event_id: String },
/// Dislike the note
Dislike { event_id: String },
/// Like the note
Like { event_id: String },
/// List notes
List,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let command = <Command as clap::Parser>::parse();
match command.resource {
Resource::Contact { command } => match command {
ContactCommand::List => handler::contact::list().await,
},
Resource::Keypair { command } => match command {
KeypairCommand::Create { private_key } => handler::keypair::create(private_key).await,
KeypairCommand::Get {
private_key_or_public_key,
} => handler::keypair::get(private_key_or_public_key.is_private_key()).await,
},
Resource::Metadata => handler::metadata::get().await,
Resource::TextNote { command } => match command {
TextNoteCommand::Create { content } => handler::text_note::create(content).await,
TextNoteCommand::Delete { event_id } => handler::text_note::delete(event_id).await,
TextNoteCommand::Dislike { event_id } => handler::text_note::dislike(event_id).await,
TextNoteCommand::Like { event_id } => handler::text_note::like(event_id).await,
TextNoteCommand::List => handler::text_note::list().await,
},
}
}