Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: version subcommand #39

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
Loading