From d8f4ae54d374d12be6b7211ef597a0fecf71a055 Mon Sep 17 00:00:00 2001 From: Mark Hammond Date: Wed, 23 Feb 2022 16:22:37 +1100 Subject: [PATCH] Make tabs example with on Windows and upgrade to StructOpt --- Cargo.lock | 2 +- examples/tabs-sync/Cargo.toml | 11 +++++++- examples/tabs-sync/src/tabs-sync.rs | 44 +++++++++++++++++------------ 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3e21376408..4f953a663e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -941,11 +941,11 @@ name = "example-tabs-sync" version = "0.1.0" dependencies = [ "anyhow", - "clap", "cli-support", "clipboard", "log", "serde_json", + "structopt", "tabs", "viaduct-reqwest", ] diff --git a/examples/tabs-sync/Cargo.toml b/examples/tabs-sync/Cargo.toml index 43619dd76f..cd4a36da25 100644 --- a/examples/tabs-sync/Cargo.toml +++ b/examples/tabs-sync/Cargo.toml @@ -6,6 +6,15 @@ license = "MPL-2.0" edition = "2021" publish = false +[features] +default = ["with-clipboard"] + +# The `clipboard` module we use appears to want to link against X11, which +# is a problem in some cases (notably, Windows/WSL) +# Running this example with, eg, `--no-default-features` will avoid using that module, +# but disable certain functionality. +with-clipboard = [] + [[example]] name = "tabs-sync" path = "src/tabs-sync.rs" @@ -16,6 +25,6 @@ serde_json = "1" log = "0.4" anyhow = "1.0" clipboard = "0.5" -clap = "2.33" +structopt = "0.3" cli-support = { path = "../cli-support" } viaduct-reqwest = { path = "../../components/support/viaduct-reqwest" } diff --git a/examples/tabs-sync/src/tabs-sync.rs b/examples/tabs-sync/src/tabs-sync.rs index 0cd8ef2125..158092254c 100644 --- a/examples/tabs-sync/src/tabs-sync.rs +++ b/examples/tabs-sync/src/tabs-sync.rs @@ -6,33 +6,32 @@ use cli_support::fxa_creds::{get_cli_fxa, get_default_fxa_config}; use cli_support::prompt::prompt_char; -use clipboard::{ClipboardContext, ClipboardProvider}; use std::sync::Arc; +use structopt::StructOpt; use tabs::{RemoteTab, TabsStore}; use anyhow::Result; +#[derive(Clone, Debug, StructOpt)] +#[structopt(name = "tabs_sync", about = "CLI for Sync tabs store")] +pub struct Opts { + #[structopt( + name = "credential_file", + value_name = "CREDENTIAL_JSON", + long = "credentials", + short = "c", + default_value = "./credentials.json" + )] + /// Path to credentials.json. + pub creds_file: String, +} + fn main() -> Result<()> { viaduct_reqwest::use_reqwest_backend(); cli_support::init_logging(); - let matches = clap::App::new("tabs_sync") - .about("CLI for Sync tabs store") - .arg( - clap::Arg::with_name("credential_file") - .short("c") - .long("credentials") - .value_name("CREDENTIAL_JSON") - .takes_value(true) - .help( - "Path to store our cached fxa credentials (defaults to \"./credentials.json\"", - ), - ) - .get_matches(); - let cred_file = matches - .value_of("credential_file") - .unwrap_or("./credentials.json"); + let opts = Opts::from_args(); - let mut cli_fxa = get_cli_fxa(get_default_fxa_config(), cred_file)?; + let mut cli_fxa = get_cli_fxa(get_default_fxa_config(), &opts.creds_file)?; let device_id = cli_fxa.account.get_current_device_id()?; let store = Arc::new(TabsStore::new()); @@ -107,7 +106,9 @@ fn main() -> Result<()> { Ok(()) } +#[cfg(feature = "with-clipboard")] fn read_local_state() -> Vec { + use clipboard::{ClipboardContext, ClipboardProvider}; println!("Please run the following command in the Firefox Browser Toolbox and copy it."); println!( " JSON.stringify(await Weave.Service.engineManager.get(\"tabs\")._store.getAllTabs())" @@ -148,3 +149,10 @@ fn read_local_state() -> Vec { } local_state } + +#[cfg(not(feature = "with-clipboard"))] +fn read_local_state() -> Vec { + println!("This module is build without the `clipboard` feature, so we can't"); + println!("read the local state."); + vec![] +}