Skip to content

Commit

Permalink
feat: added a set token command (#55)
Browse files Browse the repository at this point in the history
* Added a command to set the token in the configuration file

Closes #54
  • Loading branch information
kpagacz authored Oct 22, 2023
1 parent 4e3cc6a commit 566578a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ Once you get your session token, we recommend to set it in `elv`'s
configuration. You can do it by running:

```console
elv config set aoc.token <your-session-token>
elv token <your-session-token>
# or
elv t <your-session-token>
```

See the [section about storing the session key](#how-can-i-store-the-session-token)
Expand Down Expand Up @@ -371,6 +373,8 @@ ways, the most convenient being:
1. Set it in the configuration using the CLI:

```console
elv token 01234567890123456789abcdefghi
elv t 01234567890123456789abcdefghi
elv config set aoc.token 01234567890123456789abcdefghi
```

Expand Down
20 changes: 16 additions & 4 deletions src/application/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ impl ElvCli {
CliCommand::ListDirs => handle_list_dirs_command(),
CliCommand::Config { cmd } => match cmd {
ConfigSubcommand::List {} => handle_get_config(),
ConfigSubcommand::Set { key, value } => handle_set_config(key, value),
ConfigSubcommand::Set { key, value } => handle_set_config(&key, value),
},
CliCommand::Token { token } => handle_token_comamand(token),
}

fn handle_submit_command(
Expand Down Expand Up @@ -133,7 +134,7 @@ impl ElvCli {
}
}
}
Err(e) => eprintln!("❗️ Error when getting the input:\n\t{}", e.to_string()),
Err(e) => eprintln!(" Error when getting the input:\n\t{}", e.to_string()),
}
}

Expand Down Expand Up @@ -215,13 +216,24 @@ impl ElvCli {
}
}

fn handle_set_config(key: String, value: String) {
fn handle_set_config(key: &str, value: String) {
match Driver::set_config_key(key, value) {
Ok(_) => println!("✅ Key successfully updated"),
Ok(_) => println!("✅ Key {key} successfully updated"),
Err(e) => eprintln!("❌ Failure: {}", e.to_string()),
}
}

fn handle_token_comamand(token: Option<String>) {
match token {
Some(token) => handle_set_config("aoc.token", token),
None => {
let config = Configuration::new();
println!("✅ Your saved token is: {}", config.aoc.token);
println!("If you want to update your token, use elv t <YOUR_NEW_TOKEN>");
}
}
}

fn determine_date(riddle_args: RiddleArgs) -> Result<(i32, i32), anyhow::Error> {
let est_now = chrono::Utc::now() - chrono::Duration::hours(4);
let best_guess_date =
Expand Down
16 changes: 16 additions & 0 deletions src/application/cli/cli_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,20 @@ pub enum CliCommand {
#[clap(subcommand)]
cmd: ConfigSubcommand,
},

/// 🪙 Set the Advent Of Code token
///
/// Token management
/// You can save your Advent of Code token using this command.
/// If you don't give any parameter to this command, it will print
/// the currently saved token instead.
/// Example:
/// > elv token my_token
/// > elv token
/// my_token
#[command(verbatim_doc_comment, visible_aliases = ["t", "sett", "set-token"])]
Token {
/// Token to be saved
token: Option<String>
}
}
4 changes: 2 additions & 2 deletions src/infrastructure/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ impl Driver {
Ok(Configuration::get_file_configuration_map()?)
}

pub fn set_config_key(key: String, value: String) -> Result<()> {
Configuration::update_configuration_key(&key, value)?;
pub fn set_config_key(key: &str, value: String) -> Result<()> {
Configuration::update_configuration_key(key, value)?;
Ok(())
}

Expand Down

0 comments on commit 566578a

Please sign in to comment.