Skip to content

Commit 566578a

Browse files
authored
feat: added a set token command (#55)
* Added a command to set the token in the configuration file Closes #54
1 parent 4e3cc6a commit 566578a

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ Once you get your session token, we recommend to set it in `elv`'s
177177
configuration. You can do it by running:
178178

179179
```console
180-
elv config set aoc.token <your-session-token>
180+
elv token <your-session-token>
181+
# or
182+
elv t <your-session-token>
181183
```
182184

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

373375
```console
376+
elv token 01234567890123456789abcdefghi
377+
elv t 01234567890123456789abcdefghi
374378
elv config set aoc.token 01234567890123456789abcdefghi
375379
```
376380

src/application/cli.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ impl ElvCli {
5858
CliCommand::ListDirs => handle_list_dirs_command(),
5959
CliCommand::Config { cmd } => match cmd {
6060
ConfigSubcommand::List {} => handle_get_config(),
61-
ConfigSubcommand::Set { key, value } => handle_set_config(key, value),
61+
ConfigSubcommand::Set { key, value } => handle_set_config(&key, value),
6262
},
63+
CliCommand::Token { token } => handle_token_comamand(token),
6364
}
6465

6566
fn handle_submit_command(
@@ -133,7 +134,7 @@ impl ElvCli {
133134
}
134135
}
135136
}
136-
Err(e) => eprintln!("❗️ Error when getting the input:\n\t{}", e.to_string()),
137+
Err(e) => eprintln!(" Error when getting the input:\n\t{}", e.to_string()),
137138
}
138139
}
139140

@@ -215,13 +216,24 @@ impl ElvCli {
215216
}
216217
}
217218

218-
fn handle_set_config(key: String, value: String) {
219+
fn handle_set_config(key: &str, value: String) {
219220
match Driver::set_config_key(key, value) {
220-
Ok(_) => println!("✅ Key successfully updated"),
221+
Ok(_) => println!("✅ Key {key} successfully updated"),
221222
Err(e) => eprintln!("❌ Failure: {}", e.to_string()),
222223
}
223224
}
224225

226+
fn handle_token_comamand(token: Option<String>) {
227+
match token {
228+
Some(token) => handle_set_config("aoc.token", token),
229+
None => {
230+
let config = Configuration::new();
231+
println!("✅ Your saved token is: {}", config.aoc.token);
232+
println!("If you want to update your token, use elv t <YOUR_NEW_TOKEN>");
233+
}
234+
}
235+
}
236+
225237
fn determine_date(riddle_args: RiddleArgs) -> Result<(i32, i32), anyhow::Error> {
226238
let est_now = chrono::Utc::now() - chrono::Duration::hours(4);
227239
let best_guess_date =

src/application/cli/cli_command.rs

+16
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,20 @@ pub enum CliCommand {
186186
#[clap(subcommand)]
187187
cmd: ConfigSubcommand,
188188
},
189+
190+
/// 🪙 Set the Advent Of Code token
191+
///
192+
/// Token management
193+
/// You can save your Advent of Code token using this command.
194+
/// If you don't give any parameter to this command, it will print
195+
/// the currently saved token instead.
196+
/// Example:
197+
/// > elv token my_token
198+
/// > elv token
199+
/// my_token
200+
#[command(verbatim_doc_comment, visible_aliases = ["t", "sett", "set-token"])]
201+
Token {
202+
/// Token to be saved
203+
token: Option<String>
204+
}
189205
}

src/infrastructure/driver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ impl Driver {
220220
Ok(Configuration::get_file_configuration_map()?)
221221
}
222222

223-
pub fn set_config_key(key: String, value: String) -> Result<()> {
224-
Configuration::update_configuration_key(&key, value)?;
223+
pub fn set_config_key(key: &str, value: String) -> Result<()> {
224+
Configuration::update_configuration_key(key, value)?;
225225
Ok(())
226226
}
227227

0 commit comments

Comments
 (0)