Skip to content
This repository was archived by the owner on Dec 13, 2020. It is now read-only.

Commit 50b1edc

Browse files
fix CLI so that it adds user_id to config automatically once auth is established (I'd previously added it manually)
1 parent 32b1e48 commit 50b1edc

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/main.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@ fn add_access_token_to_config(client_config_path: PathBuf, oauth_access_token: &
175175
fs::write(client_config_path, toml).unwrap();
176176
}
177177

178+
fn add_user_id_to_config(
179+
client_config_path: PathBuf,
180+
gr_client: &api_client::GoodreadsApiClient,
181+
) -> Result<(), String> {
182+
let value = fs::read_to_string(client_config_path.clone()).unwrap();
183+
let mut config: GoodReadsConfig = toml::from_str(&value).unwrap();
184+
let user_id = gr_client.user_id();
185+
match user_id {
186+
Ok(id) => {
187+
config.user_id = Some(id);
188+
let toml = toml::to_string(&config).unwrap();
189+
fs::write(client_config_path, toml).unwrap();
190+
Ok(())
191+
}
192+
Err(err) => Err(err),
193+
}
194+
}
195+
178196
fn client_config_path() -> PathBuf {
179197
let home_directory: PathBuf = dirs::home_dir().expect("Could not determined home directory.");
180198
let mut config_file_path: PathBuf = PathBuf::new();
@@ -269,11 +287,11 @@ fn main() {
269287
let access_token_res: Result<String, _> = cfg.get_str("access_token");
270288
let access_token_secret_res: Result<String, _> = cfg.get_str("access_token_secret");
271289
let user_id_res: Result<i64, _> = cfg.get_int("user_id");
272-
let user_id = user_id_res.unwrap() as u32;
273290

274291
// TODO(Jonathon): Check for both access_token and access_token_secret
275292
match access_token_res {
276293
Ok(access_token) => {
294+
let user_id = user_id_res.unwrap() as u32;
277295
let access_token_secret = access_token_secret_res.unwrap();
278296
let app_config = GoodReadsConfig {
279297
developer_secret: dev_secret.clone(),
@@ -295,9 +313,21 @@ fn main() {
295313
Err(_err) => {
296314
match args {
297315
Cli::Authenticate {} => {
298-
let oauth_access_token = get_oauth_token(dev_key, dev_secret);
299-
add_access_token_to_config(client_config_path(), &oauth_access_token)
300-
// TODO(Jonathon): Need to also add user_id at this time
316+
let oauth_access_token = get_oauth_token(dev_key.clone(), dev_secret.clone());
317+
println!("Adding oauth access token to config...");
318+
add_access_token_to_config(client_config_path(), &oauth_access_token);
319+
let gr_client = api_client::GoodreadsApiClient::new(
320+
0, // TODO(Jonathon): This is an invalid value, though it will never be used. Still should clean this up.
321+
&dev_key,
322+
&dev_secret,
323+
&oauth_access_token.token,
324+
&oauth_access_token.token_secret,
325+
);
326+
println!("Adding your user id to config...");
327+
match add_user_id_to_config(client_config_path(), &gr_client) {
328+
Ok(_) => println!("✅"),
329+
Err(err) => println!("Error: {}", err),
330+
}
301331
}
302332
_ => println!("OAuth not set up. Please run: goodreads-sh auth"),
303333
}

0 commit comments

Comments
 (0)