Move flag requirements for credentials to subcommands that need it.

This commit is contained in:
Bill Thiede 2020-02-09 22:05:48 -08:00
parent 88234c156c
commit 48da92e278
4 changed files with 85 additions and 25 deletions

65
Cargo.lock generated
View File

@ -53,7 +53,7 @@ version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" checksum = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
dependencies = [ dependencies = [
"byteorder", "byteorder 1.3.4",
] ]
[[package]] [[package]]
@ -76,7 +76,7 @@ checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b"
dependencies = [ dependencies = [
"block-padding", "block-padding",
"byte-tools", "byte-tools",
"byteorder", "byteorder 1.3.4",
"generic-array", "generic-array",
] ]
@ -111,6 +111,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
[[package]]
name = "byteorder"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.3.4" version = "1.3.4"
@ -123,7 +129,7 @@ version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" checksum = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c"
dependencies = [ dependencies = [
"byteorder", "byteorder 1.3.4",
"either", "either",
"iovec", "iovec",
] ]
@ -494,7 +500,7 @@ version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5b34c246847f938a410a03c5458c7fee2274436675e76d8b903c08efc29c462" checksum = "a5b34c246847f938a410a03c5458c7fee2274436675e76d8b903c08efc29c462"
dependencies = [ dependencies = [
"byteorder", "byteorder 1.3.4",
"bytes 0.4.12", "bytes 0.4.12",
"fnv", "fnv",
"futures", "futures",
@ -645,7 +651,7 @@ dependencies = [
"itoa", "itoa",
"log 0.4.8", "log 0.4.8",
"net2", "net2",
"rustc_version", "rustc_version 0.2.3",
"time", "time",
"tokio 0.1.22", "tokio 0.1.22",
"tokio-buf", "tokio-buf",
@ -881,7 +887,7 @@ version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75189eb85871ea5c2e2c15abbdd541185f63b408415e5051f5cac122d8c774b9" checksum = "75189eb85871ea5c2e2c15abbdd541185f63b408415e5051f5cac122d8c774b9"
dependencies = [ dependencies = [
"rustc_version", "rustc_version 0.2.3",
] ]
[[package]] [[package]]
@ -1010,6 +1016,12 @@ dependencies = [
"winapi 0.3.8", "winapi 0.3.8",
] ]
[[package]]
name = "nom"
version = "1.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce"
[[package]] [[package]]
name = "nom" name = "nom"
version = "4.2.3" version = "4.2.3"
@ -1102,7 +1114,7 @@ checksum = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252"
dependencies = [ dependencies = [
"lock_api", "lock_api",
"parking_lot_core", "parking_lot_core",
"rustc_version", "rustc_version 0.2.3",
] ]
[[package]] [[package]]
@ -1115,7 +1127,7 @@ dependencies = [
"cloudabi", "cloudabi",
"libc", "libc",
"redox_syscall", "redox_syscall",
"rustc_version", "rustc_version 0.2.3",
"smallvec 0.6.13", "smallvec 0.6.13",
"winapi 0.3.8", "winapi 0.3.8",
] ]
@ -1286,6 +1298,18 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "procinfo"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f42e8578852a3306838981aedad8c5642ba794929aa12af0c9eb6c072b77af6c"
dependencies = [
"byteorder 0.5.3",
"libc",
"nom 1.2.4",
"rustc_version 0.1.7",
]
[[package]] [[package]]
name = "prometheus" name = "prometheus"
version = "0.7.0" version = "0.7.0"
@ -1295,6 +1319,8 @@ dependencies = [
"cfg-if", "cfg-if",
"fnv", "fnv",
"lazy_static 1.4.0", "lazy_static 1.4.0",
"libc",
"procinfo",
"protobuf", "protobuf",
"quick-error", "quick-error",
"spin", "spin",
@ -1574,13 +1600,22 @@ dependencies = [
"winapi 0.3.8", "winapi 0.3.8",
] ]
[[package]]
name = "rustc_version"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5f5376ea5e30ce23c03eb77cbe4962b988deead10910c372b226388b594c084"
dependencies = [
"semver 0.1.20",
]
[[package]] [[package]]
name = "rustc_version" name = "rustc_version"
version = "0.2.3" version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [ dependencies = [
"semver", "semver 0.9.0",
] ]
[[package]] [[package]]
@ -1684,6 +1719,12 @@ dependencies = [
"core-foundation-sys", "core-foundation-sys",
] ]
[[package]]
name = "semver"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4f410fedcf71af0345d7607d246e7ad15faaadd49d240ee3b24e5dc21a820ac"
[[package]] [[package]]
name = "semver" name = "semver"
version = "0.9.0" version = "0.9.0"
@ -1932,7 +1973,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dafb35214e317d6c0a72b16d1aa667bbc0fea57e302798e7bc520e0f39988006" checksum = "dafb35214e317d6c0a72b16d1aa667bbc0fea57e302798e7bc520e0f39988006"
dependencies = [ dependencies = [
"base64 0.10.1", "base64 0.10.1",
"byteorder", "byteorder 1.3.4",
"chrono", "chrono",
"rand 0.6.5", "rand 0.6.5",
"serde", "serde",
@ -2257,7 +2298,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a0c2bd5aeb7dcd2bb32e472c8872759308495e5eccc942e929a513cd8d36110" checksum = "8a0c2bd5aeb7dcd2bb32e472c8872759308495e5eccc942e929a513cd8d36110"
dependencies = [ dependencies = [
"base64 0.11.0", "base64 0.11.0",
"byteorder", "byteorder 1.3.4",
"bytes 0.4.12", "bytes 0.4.12",
"http 0.1.21", "http 0.1.21",
"httparse", "httparse",
@ -2592,7 +2633,7 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bb43f70885151e629e2a19ce9e50bd730fd436cfd4b666894c9ce4de9141164" checksum = "3bb43f70885151e629e2a19ce9e50bd730fd436cfd4b666894c9ce4de9141164"
dependencies = [ dependencies = [
"nom", "nom 4.2.3",
] ]
[[package]] [[package]]

View File

@ -20,4 +20,7 @@ stderrlog = "0.4.3"
structopt = "0.3.9" structopt = "0.3.9"
yup-oauth2 = "^3.1" yup-oauth2 = "^3.1"
warp = "0.1" warp = "0.1"
prometheus = "0.7.0"
[dependencies.prometheus]
features = ["process"]
version = "0.7.0"

View File

@ -23,12 +23,18 @@ mod web;
enum Command { enum Command {
/// List albums for the user of the given credentials. Optionally title filter. /// List albums for the user of the given credentials. Optionally title filter.
ListAlbums { ListAlbums {
#[structopt(flatten)]
auth: Auth,
title_filter: Option<Regex>, title_filter: Option<Regex>,
}, },
SearchMediaItems { SearchMediaItems {
#[structopt(flatten)]
auth: Auth,
album_id: String, album_id: String,
}, },
Sync { Sync {
#[structopt(flatten)]
auth: Auth,
/// Optional album title to filter. Default will mirror all albums. /// Optional album title to filter. Default will mirror all albums.
#[structopt(short, long)] #[structopt(short, long)]
title_filter: Option<Regex>, title_filter: Option<Regex>,
@ -44,6 +50,16 @@ enum Command {
}, },
} }
#[derive(Debug, StructOpt)]
struct Auth {
/// Path to json file containing Google client ID and secrets for out of band auth flow.
#[structopt(long)]
credentials: PathBuf,
/// Path to json file where photosync will store auth tokens refreshed from Google.
#[structopt(long)]
token_cache: PathBuf,
}
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
#[structopt( #[structopt(
name = "photosync", name = "photosync",
@ -54,13 +70,6 @@ struct Opt {
#[structopt(short, parse(from_occurrences))] #[structopt(short, parse(from_occurrences))]
verbose: usize, verbose: usize,
/// Path to json file containing Google client ID and secrets for out of band auth flow.
#[structopt(long)]
credentials: PathBuf,
/// Path to json file where photosync will store auth tokens refreshed from Google.
#[structopt(long)]
token_cache: PathBuf,
#[structopt(subcommand)] #[structopt(subcommand)]
cmd: Command, cmd: Command,
} }
@ -300,20 +309,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.init() .init()
.unwrap(); .unwrap();
debug!("opt: {:?}", opt); debug!("opt: {:?}", opt);
let client = new_client(&opt.credentials, &opt.token_cache)?;
match opt.cmd { match opt.cmd {
Command::ListAlbums { title_filter } => { Command::ListAlbums { auth, title_filter } => {
let client = new_client(&auth.credentials, &auth.token_cache)?;
print_albums(list_albums(&client, title_filter)?); print_albums(list_albums(&client, title_filter)?);
Ok(()) Ok(())
} }
Command::SearchMediaItems { album_id } => { Command::SearchMediaItems { auth, album_id } => {
let client = new_client(&auth.credentials, &auth.token_cache)?;
print_media_items(search_media_items(&client, &album_id)?); print_media_items(search_media_items(&client, &album_id)?);
Ok(()) Ok(())
} }
Command::Sync { Command::Sync {
auth,
title_filter, title_filter,
output, output,
} => sync_albums(&client, title_filter, output), } => {
let client = new_client(&auth.credentials, &auth.token_cache)?;
sync_albums(&client, title_filter, output)?;
Ok(())
}
Command::Serve { addr, root } => serve(addr, root), Command::Serve { addr, root } => serve(addr, root),
} }
} }

View File

@ -2,6 +2,7 @@ use std::error::Error;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::path::PathBuf; use std::path::PathBuf;
use log::info;
use prometheus::Encoder; use prometheus::Encoder;
use warp; use warp;
use warp::http::header::{HeaderMap, HeaderValue}; use warp::http::header::{HeaderMap, HeaderValue};
@ -37,7 +38,7 @@ pub fn run(addr: SocketAddr, root: PathBuf) -> Result<(), Box<dyn Error>> {
//let api = api.or(index); //let api = api.or(index);
let api = index; let api = index;
let api = api.with(warp::log("moviewatcher")); let api = api.with(warp::log("photosync"));
// We don't want metrics & heath checking filling up the logs, so we add this handler after // We don't want metrics & heath checking filling up the logs, so we add this handler after
// wrapping with the log filter. // wrapping with the log filter.
let routes = metrics().or(api); let routes = metrics().or(api);