Skip to content

Commit

Permalink
Fix/first time using (#82)
Browse files Browse the repository at this point in the history
* fix(login): login at the first time

* refactor(login): if let
  • Loading branch information
wusitee authored Jan 9, 2025
1 parent f0be26e commit d343f35
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
10 changes: 8 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub fn save_config(config: &Config) {
}

pub fn get_config() -> Config {
let config: Config = confy::load("tls-xb", "config").expect("Failed to get config");
config
println!(
":: Getting config.toml from {}...",
confy::get_configuration_file_path("tls-xb", "config")
.unwrap()
.to_str()
.unwrap()
);
confy::load("tls-xb", "config").expect("Failed to get config")
}
34 changes: 17 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ use clap::{Parser, Subcommand};
use client::LoginError;
use colored::Colorize;
use config::Config;
use confy::get_configuration_file_path;
use futures::future::join_all;
use gpa::*;
use log::info;
use semester::*;
use std::sync::Arc;
use std::{fs, sync::Arc};
use subject::*;
use tabled::{
settings::{object::Rows, Remove, Style},
Expand All @@ -40,19 +41,26 @@ async fn main() {
env_logger::init();
let cli = Cli::parse();
let mut config;
let client = match &cli.command {
Some(command) => match command {
let client = Arc::new(if let Some(command) = &cli.command {
match command {
Commands::Login => {
config = config::login();
login(&mut config).await
}
},
None => {
config = config::get_config();
login(&mut config).await
}
};
let client = Arc::new(client);
} else {
let config_path = get_configuration_file_path("tls-xb", "config").unwrap();
match fs::metadata(config_path) {
Ok(_) => {
config = config::get_config();
}
Err(_) => {
// if the config file doesn't exit, do tls-xb login.
config = config::login();
}
}
login(&mut config).await
});

println!(":: Fetching semesters...");
let semesters = get_semesters(&client).await;
Expand Down Expand Up @@ -263,14 +271,6 @@ fn colorize(string: &str, score_level: &str) -> String {
}

async fn login(config: &mut Config) -> reqwest::Client {
println!(
":: Getting config.toml from {}...",
confy::get_configuration_file_path("tls-xb", "config")
.unwrap()
.to_str()
.unwrap()
);

println!(":: Logging in...");
let mut client;
let login_limit = 3;
Expand Down

0 comments on commit d343f35

Please sign in to comment.