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

qr code generation optional argument #277

Merged
merged 12 commits into from
Dec 12, 2024
52 changes: 43 additions & 9 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ struct CommonLeaderArgs {
/// Length of code (in bytes/words)
#[arg(short = 'c', long, value_name = "NUMWORDS", default_value = "2")]
code_length: usize,
/// Suppress QR code generation from send link
#[arg(long)]
no_qr: bool,
}

// receive
Expand Down Expand Up @@ -289,7 +292,12 @@ async fn main() -> eyre::Result<()> {
match app.command {
WormholeCommand::Send {
common,
common_leader: CommonLeaderArgs { code, code_length },
common_leader:
CommonLeaderArgs {
code,
code_length,
no_qr,
},
common_send: CommonSenderArgs { file_name, files },
..
} => {
Expand All @@ -302,6 +310,7 @@ async fn main() -> eyre::Result<()> {
common,
code,
Some(code_length),
no_qr,
true,
transfer::APP_CONFIG,
Some(&sender_print_code),
Expand All @@ -328,7 +337,12 @@ async fn main() -> eyre::Result<()> {
tries,
timeout,
common,
common_leader: CommonLeaderArgs { code, code_length },
common_leader:
CommonLeaderArgs {
code,
code_length,
no_qr,
},
common_send: CommonSenderArgs { file_name, files },
..
} => {
Expand All @@ -339,6 +353,7 @@ async fn main() -> eyre::Result<()> {
common,
code,
Some(code_length),
no_qr,
true,
transfer::APP_CONFIG,
Some(&sender_print_code),
Expand Down Expand Up @@ -379,6 +394,7 @@ async fn main() -> eyre::Result<()> {
code,
None,
false,
false,
transfer::APP_CONFIG,
None,
));
Expand All @@ -401,7 +417,12 @@ async fn main() -> eyre::Result<()> {
WormholeCommand::Forward(ForwardCommand::Serve {
targets,
common,
common_leader: CommonLeaderArgs { code, code_length },
common_leader:
CommonLeaderArgs {
code,
code_length,
no_qr,
},
..
}) => {
// TODO make fancy
Expand Down Expand Up @@ -451,6 +472,7 @@ async fn main() -> eyre::Result<()> {
common.clone(),
code.clone(),
Some(code_length),
no_qr,
true,
app_config,
Some(&server_print_code),
Expand Down Expand Up @@ -481,8 +503,10 @@ async fn main() -> eyre::Result<()> {
tracing::warn!("This is an unstable feature. Make sure that your peer is running the exact same version of the program as you. Also, please report all bugs and crashes.");
let mut app_config = forwarding::APP_CONFIG;
app_config.app_version.transit_abilities = parse_transit_args(&common);
let (wormhole, _code, relay_hints) =
parse_and_connect(&mut term, common, code, None, false, app_config, None).await?;
let (wormhole, _code, relay_hints) = parse_and_connect(
&mut term, common, code, None, false, false, app_config, None,
)
.await?;

let offer = forwarding::connect(
wormhole,
Expand Down Expand Up @@ -548,7 +572,8 @@ fn parse_transit_args(args: &CommonArgs) -> transit::Abilities {
}
}

type PrintCodeFn = dyn Fn(&mut Term, &magic_wormhole::Code, &Option<url::Url>) -> eyre::Result<()>;
type PrintCodeFn =
dyn Fn(&mut Term, &magic_wormhole::Code, &Option<url::Url>, bool) -> eyre::Result<()>;

/**
* Parse the necessary command line arguments to establish an initial server connection.
Expand All @@ -563,6 +588,7 @@ async fn parse_and_connect(
common_args: CommonArgs,
mut code: Option<String>,
code_length: Option<usize>,
no_qr: bool,
is_send: bool,
mut app_config: magic_wormhole::AppConfig<impl serde::Serialize + Send + Sync + 'static>,
print_code: Option<&PrintCodeFn>,
Expand Down Expand Up @@ -637,6 +663,7 @@ async fn parse_and_connect(
term,
&code,
&uri_rendezvous,
no_qr,
)?;
}
MailboxConnection::connect(app_config, code, true).await?
Expand Down Expand Up @@ -667,6 +694,7 @@ async fn parse_and_connect(
term,
mailbox_connection.code(),
&uri_rendezvous,
no_qr,
)?;
}
mailbox_connection
Expand Down Expand Up @@ -764,6 +792,7 @@ fn sender_print_code(
term: &mut Term,
code: &magic_wormhole::Code,
rendezvous_server: &Option<url::Url>,
no_qr: bool,
) -> eyre::Result<()> {
let uri = magic_wormhole::uri::WormholeTransferUri {
code: code.clone(),
Expand All @@ -783,9 +812,13 @@ fn sender_print_code(
}

writeln!(term, "This is equivalent to the following link: \u{001B}]8;;{}\u{001B}\\{}\u{001B}]8;;\u{001B}\\", &uri, &uri)?;
let qr =
qr2term::generate_qr_string(&uri).context("Failed to generate QR code for send link")?;
writeln!(term, "{}", qr)?;
if no_qr {
tracing::debug!("QR option not enabled. Skipping QR code generation.");
} else {
let qr_code = qr2term::generate_qr_string(&uri)
.context("Failed to generate QR code for send link")?;
writeln!(term, "{}", qr_code)?;
}

writeln!(
term,
Expand All @@ -805,6 +838,7 @@ fn server_print_code(
term: &mut Term,
code: &magic_wormhole::Code,
_: &Option<url::Url>,
_qr: bool,
) -> eyre::Result<()> {
if cfg!(feature = "clipboard") {
writeln!(
Expand Down
Loading