Skip to content

Commit

Permalink
Merge pull request #39 from cmars/feat/version-subcommand
Browse files Browse the repository at this point in the history
feat: version subcommand
  • Loading branch information
cmars authored Apr 1, 2024
2 parents 824c35d + 8dc0fec commit c96a4dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ use cursive::{
CursiveRunnable, Vec2, View, With, XY,
};
use cursive_aligned_view::{Alignable, AlignedView};
use distrans_peer::{veilid_config, wait_for_network, Fetcher, Seeder};
use flume::{unbounded, Receiver, Sender};
use tokio_util::sync::CancellationToken;
use tracing::{debug, warn};
use veilid_core::{RoutingContext, Sequencing, VeilidStateAttachment, VeilidUpdate};

use distrans_peer::{other_err, veilid_config, wait_for_network, Fetcher, Seeder};

use crate::{cli::Commands, initialize_stderr_logging, initialize_ui_logging, Cli};

pub struct App {
Expand Down Expand Up @@ -95,6 +96,10 @@ impl App {
}

pub async fn run(&mut self) -> Result<()> {
if self.cli.version() {
println!("distrans {}", env!("CARGO_PKG_VERSION"));
return Ok(());
}
let (tx, rx) = unbounded();
if self.cli.no_ui() {
self.run_no_ui(tx, rx).await
Expand Down Expand Up @@ -316,6 +321,7 @@ impl App {
.await?;
seeder.seed(cancel, updates).await
}
_ => Err(other_err("invalid command")),
};
complete_cancel.cancel();
if let Err(e) = canceller.await {
Expand Down
22 changes: 15 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::io::IsTerminal;

use clap::{arg, Parser, Subcommand};

use color_eyre::eyre::Result;
use distrans_peer::{other_err, Error};
use color_eyre::eyre::{Error, Result};
use sha2::{Digest, Sha256};
use tracing::debug;

Expand All @@ -23,7 +21,7 @@ pub struct Cli {

impl Cli {
pub fn no_ui(&self) -> bool {
return self.no_ui || !std::io::stdout().is_terminal()
return self.no_ui || !std::io::stdout().is_terminal();
}

pub fn state_dir(&self) -> Result<String> {
Expand All @@ -36,6 +34,7 @@ impl Cli {
ref root,
} => self.state_dir_for(format!("get:{}:{}", dht_key, root)),
Commands::Post { ref file } => self.state_dir_for(format!("post:{}", file.to_owned())),
_ => Err(Error::msg("invalid command")),
}
}

Expand All @@ -44,17 +43,25 @@ impl Cli {
key_digest.update(&key.as_bytes());
let key_digest_bytes: [u8; 32] = key_digest.finalize().into();
let dir_name = hex::encode(key_digest_bytes);
let data_dir =
dirs::state_dir().or(dirs::data_local_dir()).ok_or(Error::Other("cannot resolve state dir".to_string()))?;
let data_dir = dirs::state_dir()
.or(dirs::data_local_dir())
.ok_or(Error::msg("cannot resolve state dir"))?;
let state_dir = data_dir
.join("distrans")
.join(dir_name)
.into_os_string()
.into_string()
.map_err(|os| other_err(format!("{:?}", os)))?;
.map_err(|os| Error::msg(format!("{:?}", os)))?;
debug!(state_dir);
Ok(state_dir)
}

pub fn version(&self) -> bool {
if let Commands::Version = self.commands {
return true;
}
return false;
}
}

#[derive(Subcommand, Debug)]
Expand All @@ -67,4 +74,5 @@ pub enum Commands {
Post {
file: String,
},
Version,
}

0 comments on commit c96a4dd

Please sign in to comment.