mod.rs
1 use crate::config::Config; 2 use crate::util::CliPublicKeyValueParser; 3 4 mod database; 5 mod fetch; 6 mod get; 7 mod key; 8 mod put; 9 mod read; 10 mod serve; 11 12 #[derive(Debug, clap::Subcommand)] 13 pub enum Command { 14 Version, 15 16 /// Key management commands 17 #[clap(subcommand)] 18 Key(key::KeyCommand), 19 20 /// Fetch something and print it, but don't put it into the local storage 21 Fetch { 22 #[clap( 23 long = "online-timeout", 24 default_value = "10s", 25 value_parser = humantime::parse_duration 26 )] 27 online_timeout: std::time::Duration, 28 29 /// Remote nodes to connect to for fetching 30 #[clap(long, short = 'C')] 31 #[clap(value_parser = CliPublicKeyValueParser)] 32 connect_to: Vec<iroh::PublicKey>, 33 34 /// Optional list of ips to connect to 35 #[clap(long, short = 'i')] 36 connect_to_ips: Option<Vec<std::net::SocketAddr>>, 37 38 #[clap(subcommand)] 39 subcommand: fetch::FetchCommand, 40 }, 41 42 /// Fetch something, print it, and put it into the local storage 43 Get { 44 #[clap( 45 long = "online-timeout", 46 default_value = "10s", 47 value_parser = humantime::parse_duration 48 )] 49 online_timeout: std::time::Duration, 50 51 /// Remote nodes to connect to for fetching 52 #[clap(long, short = 'C')] 53 #[clap(value_parser = CliPublicKeyValueParser)] 54 connect_to: Vec<iroh::PublicKey>, 55 56 /// Optional list of ips to connect to 57 #[clap(long, short = 'i')] 58 connect_to_ips: Option<Vec<std::net::SocketAddr>>, 59 60 #[clap(subcommand)] 61 subcommand: get::GetCommand, 62 }, 63 64 /// Read something from the chain of a profile 65 Read { 66 #[clap(subcommand)] 67 subcommand: read::ReadCommand, 68 }, 69 70 /// Put something onto the chain of a profile 71 Put { 72 #[clap(subcommand)] 73 subcommand: put::PutCommand, 74 }, 75 76 /// Serve the local storage 77 Serve { 78 #[clap( 79 long = "online-timeout", 80 default_value = "10s", 81 value_parser = humantime::parse_duration 82 )] 83 online_timeout: std::time::Duration, 84 85 /// Remote nodes to connect to for fetching 86 #[clap(long, short = 'C')] 87 #[clap(value_parser = CliPublicKeyValueParser)] 88 connect_to: Vec<iroh::PublicKey>, 89 90 /// Optional list of ips to connect to 91 #[clap(long, short = 'i')] 92 connect_to_ips: Option<Vec<std::net::SocketAddr>>, 93 94 #[clap(subcommand)] 95 subcommand: serve::ServeCommand, 96 }, 97 98 /// Database maintenance 99 Database { 100 #[clap(subcommand)] 101 subcommand: database::DatabaseCommand, 102 }, 103 } 104 105 impl Command { 106 pub async fn run( 107 self, 108 process_state: &crate::systemd::ProcessState, 109 config: &Config, 110 ) -> Result<(), CommandError> { 111 match self { 112 Command::Version => { 113 println!("{}", env!("CARGO_PKG_VERSION")); 114 Ok(()) 115 } 116 117 Command::Key(key) => key.run(config).await.map_err(CommandError::from), 118 Command::Fetch { 119 online_timeout, 120 subcommand, 121 connect_to, 122 connect_to_ips, 123 } => { 124 let (ctoken, instance_handle, database, connection_handlers) = 125 crate::boot::boot(config, online_timeout, connect_to, connect_to_ips).await?; 126 127 subcommand 128 .run( 129 config, 130 ctoken, 131 instance_handle, 132 database, 133 connection_handlers, 134 ) 135 .await 136 .map_err(CommandError::from) 137 } 138 Command::Get { 139 online_timeout, 140 subcommand, 141 connect_to, 142 connect_to_ips, 143 } => { 144 let (ctoken, instance_handle, database, connection_handlers) = 145 crate::boot::boot(config, online_timeout, connect_to, connect_to_ips).await?; 146 147 subcommand 148 .run( 149 config, 150 ctoken, 151 instance_handle, 152 database, 153 connection_handlers, 154 ) 155 .await 156 .map_err(CommandError::from) 157 } 158 Command::Read { subcommand } => { 159 subcommand.run(config).await.map_err(CommandError::from) 160 } 161 Command::Put { subcommand } => subcommand.run(config).await.map_err(CommandError::from), 162 Command::Serve { 163 online_timeout, 164 connect_to, 165 connect_to_ips, 166 subcommand, 167 } => { 168 process_state.set_starting(); 169 170 let (ctoken, instance_handle, database, connection_handlers) = 171 crate::boot::boot(config, online_timeout, connect_to, connect_to_ips).await?; 172 173 subcommand 174 .run( 175 process_state, 176 config, 177 ctoken, 178 instance_handle, 179 database, 180 connection_handlers, 181 ) 182 .await 183 .map_err(CommandError::from) 184 } 185 Command::Database { subcommand } => { 186 subcommand.run(config).await.map_err(CommandError::from) 187 } 188 } 189 } 190 } 191 192 #[derive(Debug, thiserror::Error)] 193 pub enum CommandError { 194 #[error(transparent)] 195 Boot(#[from] crate::boot::BootError), 196 197 #[error(transparent)] 198 Key(#[from] self::key::KeyCommandError), 199 200 #[error(transparent)] 201 Read(#[from] self::read::ReadCommandError), 202 203 #[error(transparent)] 204 Fetch(#[from] self::fetch::FetchCommandError), 205 206 #[error(transparent)] 207 Get(#[from] self::get::GetCommandError), 208 209 #[error(transparent)] 210 Put(#[from] self::put::PutCommandError), 211 212 #[error(transparent)] 213 Database(#[from] self::database::DatabaseCommandError), 214 215 #[error(transparent)] 216 Serve(#[from] serve::ServeCommandError), 217 }