Add usage help and documentation

This commit is contained in:
Kiana Sheibani 2023-09-30 05:13:52 -04:00
parent 9feba1a136
commit c70fc7506a
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -21,31 +21,65 @@ struct Cli {
#[command(subcommand)] #[command(subcommand)]
subcommand: Subcommands, subcommand: Subcommands,
#[arg(short = 'A', long = "auth", value_name = "TOKEN", global = true)] #[arg(
short = 'A',
long = "auth",
value_name = "TOKEN",
global = true,
help = "Authentication token",
long_help = "The authentication token for accessing start.gg.
A token can be specified using this argument, in the environment variable
AUTH_TOKEN, or in a text file '<CONFIG_DIR>/auth.txt'."
)]
auth_token: Option<String>, auth_token: Option<String>,
#[arg(short, long = "config", value_name = "DIR", global = true)] #[arg(
short,
long = "config",
value_name = "DIR",
global = true,
help = "Config directory",
long_help = "This option overrides the default config directory.
If this directory does not exist, it will be created and a database file will
be initialized within it."
)]
config_dir: Option<PathBuf>, config_dir: Option<PathBuf>,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
enum Subcommands { enum Subcommands {
#[command(about = "Manipulate stored datasets")]
Dataset { Dataset {
#[command(subcommand)] #[command(subcommand)]
subcommand: DatasetSC, subcommand: DatasetSC,
}, },
#[command(
about = "Sync player ratings",
long_about = "Pull recent tournament data off of start.gg and use it to update each player's
stored ratings. This command will automatically keep track of the last time each
dataset was synced."
)]
Sync { Sync {
#[arg(group = "datasets")] #[arg(
names: Vec<String>, group = "datasets",
#[arg(short, long, group = "datasets")] help = "The datasets to sync",
long_help = "A list of datasets to sync.
If no datasets are given, then the dataset 'default' is synced. This dataset is
created if it does not already exist."
)]
datasets: Vec<String>,
#[arg(short, long, group = "datasets", help = "Sync all stored databases")]
all: bool, all: bool,
}, },
} }
#[derive(Subcommand)] #[derive(Subcommand)]
enum DatasetSC { enum DatasetSC {
#[command(about = "List datasets")]
List, List,
#[command(about = "Create a new dataset")]
New { name: Option<String> }, New { name: Option<String> },
#[command(about = "Delete a dataset")]
Delete { name: Option<String> }, Delete { name: Option<String> },
} }
@ -63,7 +97,7 @@ fn main() {
subcommand: DatasetSC::Delete { name }, subcommand: DatasetSC::Delete { name },
} => dataset_delete(name), } => dataset_delete(name),
Subcommands::Sync { names, all } => sync(names, all, cli.auth_token), Subcommands::Sync { datasets, all } => sync(datasets, all, cli.auth_token),
} }
} }
@ -110,24 +144,24 @@ fn dataset_delete(name: Option<String>) {
delete_dataset(&connection, &name).unwrap(); delete_dataset(&connection, &name).unwrap();
} }
fn sync(names: Vec<String>, all: bool, auth_token: Option<String>) { fn sync(datasets: Vec<String>, all: bool, auth_token: Option<String>) {
let config_dir = dirs::config_dir().unwrap(); let config_dir = dirs::config_dir().unwrap();
let auth = auth_token.or_else(|| get_auth_token(&config_dir)).unwrap(); let auth = auth_token.or_else(|| get_auth_token(&config_dir)).unwrap();
let connection = open_datasets(&config_dir).unwrap(); let connection = open_datasets(&config_dir).unwrap();
let names = if all { let datasets = if all {
list_datasets(&connection).unwrap() list_datasets(&connection).unwrap()
} else if names.len() == 0 { } else if datasets.len() == 0 {
new_dataset(&connection, "default").unwrap(); new_dataset(&connection, "default").unwrap();
vec![String::from("default")] vec![String::from("default")]
} else { } else {
names datasets
}; };
for name in names { for dataset in datasets {
let last_sync = get_last_sync(&connection, &name).unwrap().unwrap(); let last_sync = get_last_sync(&connection, &dataset).unwrap().unwrap();
let results = run_query::<TournamentSets, _>( let results = run_query::<TournamentSets, _>(
TournamentSetsVars { TournamentSetsVars {
@ -142,13 +176,13 @@ fn sync(names: Vec<String>, all: bool, auth_token: Option<String>) {
) )
.unwrap(); .unwrap();
update_from_tournament(&connection, &name, results).unwrap(); update_from_tournament(&connection, &dataset, results).unwrap();
let current_time = SystemTime::now() let current_time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.unwrap() .unwrap()
.as_secs(); .as_secs();
update_last_sync(&connection, &name, current_time).unwrap(); update_last_sync(&connection, &dataset, current_time).unwrap();
} }
} }