cargo fmt

This commit is contained in:
Carson McManus 2022-06-19 14:44:47 -04:00
parent c86e31648a
commit 8839a18543
2 changed files with 82 additions and 38 deletions

View file

@ -1,16 +1,31 @@
use std::str::FromStr; use clap::{clap_derive::ArgEnum, Parser};
use clap::{Parser, clap_derive::ArgEnum};
use clap_complete::Shell; use clap_complete::Shell;
use std::str::FromStr;
#[derive(Debug, Clone, Parser)] #[derive(Debug, Clone, Parser)]
#[clap(name="steamguard-cli", bin_name="steamguard", author, version, about = "Generate Steam 2FA codes and confirm Steam trades from the command line.", long_about = None)] #[clap(name="steamguard-cli", bin_name="steamguard", author, version, about = "Generate Steam 2FA codes and confirm Steam trades from the command line.", long_about = None)]
pub(crate) struct Args { pub(crate) struct Args {
#[clap(short, long, conflicts_with="all", help = "Steam username, case-sensitive.", long_help = "Select the account you want by steam username. Case-sensitive. By default, the first account in the manifest is selected.")] #[clap(
short,
long,
conflicts_with = "all",
help = "Steam username, case-sensitive.",
long_help = "Select the account you want by steam username. Case-sensitive. By default, the first account in the manifest is selected."
)]
pub username: Option<String>, pub username: Option<String>,
#[clap(short, long, conflicts_with="username", help = "Select all accounts in the manifest.")] #[clap(
short,
long,
conflicts_with = "username",
help = "Select all accounts in the manifest."
)]
pub all: bool, pub all: bool,
/// The path to the maFiles directory. /// The path to the maFiles directory.
#[clap(short, long, help = "Specify which folder your maFiles are in. This should be a path to a folder that contains manifest.json. Default: ~/.config/steamguard-cli/maFiles")] #[clap(
short,
long,
help = "Specify which folder your maFiles are in. This should be a path to a folder that contains manifest.json. Default: ~/.config/steamguard-cli/maFiles"
)]
pub mafiles_path: Option<String>, pub mafiles_path: Option<String>,
#[clap(short, long, help = "Specify your encryption passkey.")] #[clap(short, long, help = "Specify your encryption passkey.")]
pub passkey: Option<String>, pub passkey: Option<String>,
@ -44,13 +59,16 @@ pub(crate) enum Verbosity {
impl std::fmt::Display for Verbosity { impl std::fmt::Display for Verbosity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{}", match self { f.write_fmt(format_args!(
Verbosity::Error => "error", "{}",
Verbosity::Warn => "warn", match self {
Verbosity::Info => "info", Verbosity::Error => "error",
Verbosity::Debug => "debug", Verbosity::Warn => "warn",
Verbosity::Trace => "trace", Verbosity::Info => "info",
})) Verbosity::Debug => "debug",
Verbosity::Trace => "trace",
}
))
} }
} }
@ -69,16 +87,15 @@ impl FromStr for Verbosity {
} }
} }
#[derive(Debug, Clone, Parser)] #[derive(Debug, Clone, Parser)]
#[clap(about="Debug stuff, not useful for most users.")] #[clap(about = "Debug stuff, not useful for most users.")]
pub(crate) struct ArgsDebug { pub(crate) struct ArgsDebug {
#[clap(long, help = "Show an example confirmation menu using dummy data.")] #[clap(long, help = "Show an example confirmation menu using dummy data.")]
pub demo_conf_menu: bool pub demo_conf_menu: bool,
} }
#[derive(Debug, Clone, Parser)] #[derive(Debug, Clone, Parser)]
#[clap(about="Generate shell completions")] #[clap(about = "Generate shell completions")]
pub(crate) struct ArgsCompletions { pub(crate) struct ArgsCompletions {
#[clap(short, long, arg_enum, help = "The shell to generate completions for.")] #[clap(short, long, arg_enum, help = "The shell to generate completions for.")]
pub shell: Shell, pub shell: Shell,
@ -101,9 +118,17 @@ pub(crate) struct ArgsImport {
#[derive(Debug, Clone, Parser)] #[derive(Debug, Clone, Parser)]
#[clap(about = "Interactive interface for trade confirmations")] #[clap(about = "Interactive interface for trade confirmations")]
pub(crate) struct ArgsTrade { pub(crate) struct ArgsTrade {
#[clap(short, long, help = "Accept all open trade confirmations. Does not open interactive interface.")] #[clap(
short,
long,
help = "Accept all open trade confirmations. Does not open interactive interface."
)]
pub accept_all: bool, pub accept_all: bool,
#[clap(short, long, help = "If submitting a confirmation response fails, exit immediately.")] #[clap(
short,
long,
help = "If submitting a confirmation response fails, exit immediately."
)]
pub fail_fast: bool, pub fail_fast: bool,
} }

View file

@ -1,5 +1,5 @@
extern crate rpassword; extern crate rpassword;
use clap::{Parser, IntoApp}; use clap::{IntoApp, Parser};
use log::*; use log::*;
use std::{ use std::{
io::{stdout, Write}, io::{stdout, Write},
@ -53,11 +53,11 @@ fn run() -> anyhow::Result<()> {
match args.sub { match args.sub {
Some(cli::Subcommands::Debug(args)) => { Some(cli::Subcommands::Debug(args)) => {
return do_subcmd_debug(args); return do_subcmd_debug(args);
}, }
Some(cli::Subcommands::Completion(args)) => { Some(cli::Subcommands::Completion(args)) => {
return do_subcmd_completion(args); return do_subcmd_completion(args);
}, }
_ => {}, _ => {}
}; };
let mafiles_dir = if let Some(mafiles_path) = &args.mafiles_path { let mafiles_dir = if let Some(mafiles_path) = &args.mafiles_path {
@ -122,17 +122,17 @@ fn run() -> anyhow::Result<()> {
match args.sub { match args.sub {
Some(cli::Subcommands::Setup(args)) => { Some(cli::Subcommands::Setup(args)) => {
return do_subcmd_setup(args, &mut manifest); return do_subcmd_setup(args, &mut manifest);
}, }
Some(cli::Subcommands::Import(args)) => { Some(cli::Subcommands::Import(args)) => {
return do_subcmd_import(args, &mut manifest); return do_subcmd_import(args, &mut manifest);
}, }
Some(cli::Subcommands::Encrypt(args)) => { Some(cli::Subcommands::Encrypt(args)) => {
return do_subcmd_encrypt(args, &mut manifest); return do_subcmd_encrypt(args, &mut manifest);
}, }
Some(cli::Subcommands::Decrypt(args)) => { Some(cli::Subcommands::Decrypt(args)) => {
return do_subcmd_decrypt(args, &mut manifest); return do_subcmd_decrypt(args, &mut manifest);
}, }
_ => {}, _ => {}
} }
let selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>>; let selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>>;
@ -170,14 +170,14 @@ fn run() -> anyhow::Result<()> {
match args.sub { match args.sub {
Some(cli::Subcommands::Trade(args)) => { Some(cli::Subcommands::Trade(args)) => {
return do_subcmd_trade(args, &mut manifest, selected_accounts); return do_subcmd_trade(args, &mut manifest, selected_accounts);
}, }
Some(cli::Subcommands::Remove(args)) => { Some(cli::Subcommands::Remove(args)) => {
return do_subcmd_remove(args, &mut manifest, selected_accounts); return do_subcmd_remove(args, &mut manifest, selected_accounts);
}, }
Some(s) => { Some(s) => {
error!("Unknown subcommand: {:?}", s); error!("Unknown subcommand: {:?}", s);
return Err(errors::UserError::UnknownSubcommand.into()); return Err(errors::UserError::UnknownSubcommand.into());
}, }
_ => { _ => {
debug!("No subcommand given, assuming user wants a 2fa code"); debug!("No subcommand given, assuming user wants a 2fa code");
return do_subcmd_code(selected_accounts); return do_subcmd_code(selected_accounts);
@ -319,7 +319,10 @@ fn do_subcmd_completion(args: cli::ArgsCompletions) -> Result<(), anyhow::Error>
return Ok(()); return Ok(());
} }
fn do_subcmd_setup(args: cli::ArgsSetup, manifest: &mut accountmanager::Manifest) -> anyhow::Result<()> { fn do_subcmd_setup(
args: cli::ArgsSetup,
manifest: &mut accountmanager::Manifest,
) -> anyhow::Result<()> {
println!("Log in to the account that you want to link to steamguard-cli"); println!("Log in to the account that you want to link to steamguard-cli");
print!("Username: "); print!("Username: ");
let username = if args.username.is_some() { let username = if args.username.is_some() {
@ -336,8 +339,7 @@ fn do_subcmd_setup(args: cli::ArgsSetup, manifest: &mut accountmanager::Manifest
username username
); );
} }
let session = let session = do_login_raw(username).expect("Failed to log in. Account has not been linked.");
do_login_raw(username).expect("Failed to log in. Account has not been linked.");
let mut linker = AccountLinker::new(session); let mut linker = AccountLinker::new(session);
let account: SteamGuardAccount; let account: SteamGuardAccount;
@ -434,7 +436,10 @@ fn do_subcmd_setup(args: cli::ArgsSetup, manifest: &mut accountmanager::Manifest
return Ok(()); return Ok(());
} }
fn do_subcmd_import(args: cli::ArgsImport, manifest: &mut accountmanager::Manifest) -> anyhow::Result<()> { fn do_subcmd_import(
args: cli::ArgsImport,
manifest: &mut accountmanager::Manifest,
) -> anyhow::Result<()> {
for file_path in args.files { for file_path in args.files {
match manifest.import_account(&file_path) { match manifest.import_account(&file_path) {
Ok(_) => { Ok(_) => {
@ -450,7 +455,11 @@ fn do_subcmd_import(args: cli::ArgsImport, manifest: &mut accountmanager::Manife
return Ok(()); return Ok(());
} }
fn do_subcmd_trade(args: cli::ArgsTrade, manifest: &mut accountmanager::Manifest, mut selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>>) -> anyhow::Result<()> { fn do_subcmd_trade(
args: cli::ArgsTrade,
manifest: &mut accountmanager::Manifest,
mut selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
) -> anyhow::Result<()> {
for a in selected_accounts.iter_mut() { for a in selected_accounts.iter_mut() {
let mut account = a.lock().unwrap(); let mut account = a.lock().unwrap();
@ -529,7 +538,11 @@ fn do_subcmd_trade(args: cli::ArgsTrade, manifest: &mut accountmanager::Manifest
return Ok(()); return Ok(());
} }
fn do_subcmd_remove(_args: cli::ArgsRemove, manifest: &mut accountmanager::Manifest, selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>>) -> anyhow::Result<()> { fn do_subcmd_remove(
_args: cli::ArgsRemove,
manifest: &mut accountmanager::Manifest,
selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
) -> anyhow::Result<()> {
println!( println!(
"This will remove the mobile authenticator from {} accounts: {}", "This will remove the mobile authenticator from {} accounts: {}",
selected_accounts.len(), selected_accounts.len(),
@ -589,7 +602,10 @@ fn do_subcmd_remove(_args: cli::ArgsRemove, manifest: &mut accountmanager::Manif
return Ok(()); return Ok(());
} }
fn do_subcmd_encrypt(_args: cli::ArgsEncrypt, manifest: &mut accountmanager::Manifest) -> anyhow::Result<()> { fn do_subcmd_encrypt(
_args: cli::ArgsEncrypt,
manifest: &mut accountmanager::Manifest,
) -> anyhow::Result<()> {
if !manifest.has_passkey() { if !manifest.has_passkey() {
let mut passkey; let mut passkey;
loop { loop {
@ -611,7 +627,10 @@ fn do_subcmd_encrypt(_args: cli::ArgsEncrypt, manifest: &mut accountmanager::Man
return Ok(()); return Ok(());
} }
fn do_subcmd_decrypt(_args: cli::ArgsDecrypt, manifest: &mut accountmanager::Manifest) -> anyhow::Result<()> { fn do_subcmd_decrypt(
_args: cli::ArgsDecrypt,
manifest: &mut accountmanager::Manifest,
) -> anyhow::Result<()> {
manifest.load_accounts()?; manifest.load_accounts()?;
for entry in &mut manifest.entries { for entry in &mut manifest.entries {
entry.encryption = None; entry.encryption = None;