Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ pub fn login() {
let api_url = profile_config.api_url.to_string();
let app_url = profile_config.app_url.to_string();

// Check if already authenticated
if let Some(api_key) = &profile_config.api_key {
if api_key != "PLACEHOLDER" {
let client = reqwest::blocking::Client::new();
if let Ok(resp) = client
.get(format!("{api_url}/workspaces"))
.header("Authorization", format!("Bearer {api_key}"))
.send()
{
if resp.status().is_success() {
println!("{}", "You are already signed in.".green());
print!("Do you want to log in again? [y/N] ");
use std::io::Write;
std::io::stdout().flush().unwrap();
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
if !input.trim().eq_ignore_ascii_case("y") {
return;
}
}
}
}
}

let code_verifier = generate_code_verifier();
let code_challenge = generate_code_challenge(&code_verifier);
let state = generate_random_string(32);
Expand Down Expand Up @@ -261,6 +285,10 @@ pub fn login() {
None => print_row("Workspace", &"None".dark_grey().to_string()),
}
}
Ok(r) if r.status() == reqwest::StatusCode::FORBIDDEN => {
eprintln!("{}", "You are not authorized to create a new API token.".red());
std::process::exit(1);
}
Ok(r) => {
eprintln!("token exchange failed: HTTP {}", r.status());
std::process::exit(1);
Expand Down
15 changes: 11 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ pub struct ConfigFile {
pub profiles: HashMap<String, ProfileConfig>,
}

fn write_config(config_path: &std::path::Path, content: &str) -> Result<(), String> {
if let Some(parent) = config_path.parent() {
fs::create_dir_all(parent).map_err(|e| format!("error creating config directory: {e}"))?;
}
fs::write(config_path, content).map_err(|e| format!("error writing config file: {e}"))
}

pub fn save_api_key(profile: &str, api_key: &str) -> Result<(), String> {
let user_dirs = UserDirs::new().ok_or("could not determine home directory")?;
let config_path = user_dirs.home_dir().join(".hotdata").join("config.yml");
Expand All @@ -122,7 +129,7 @@ pub fn save_api_key(profile: &str, api_key: &str) -> Result<(), String> {
let content = serde_yaml::to_string(&config_file)
.map_err(|e| format!("error serializing config: {e}"))?;

fs::write(&config_path, content).map_err(|e| format!("error writing config file: {e}"))
write_config(&config_path, &content)
}

pub fn remove_api_key(profile: &str) -> Result<(), String> {
Expand All @@ -145,7 +152,7 @@ pub fn remove_api_key(profile: &str) -> Result<(), String> {

let content = serde_yaml::to_string(&config_file)
.map_err(|e| format!("error serializing config: {e}"))?;
fs::write(&config_path, content).map_err(|e| format!("error writing config file: {e}"))
write_config(&config_path, &content)
}

pub fn save_workspaces(profile: &str, workspaces: Vec<WorkspaceEntry>) -> Result<(), String> {
Expand All @@ -171,7 +178,7 @@ pub fn save_workspaces(profile: &str, workspaces: Vec<WorkspaceEntry>) -> Result
let content = serde_yaml::to_string(&config_file)
.map_err(|e| format!("error serializing config: {e}"))?;

fs::write(&config_path, content).map_err(|e| format!("error writing config file: {e}"))
write_config(&config_path, &content)
}

pub fn save_default_workspace(profile: &str, workspace: WorkspaceEntry) -> Result<(), String> {
Expand All @@ -192,7 +199,7 @@ pub fn save_default_workspace(profile: &str, workspace: WorkspaceEntry) -> Resul

let content = serde_yaml::to_string(&config_file)
.map_err(|e| format!("error serializing config: {e}"))?;
fs::write(&config_path, content).map_err(|e| format!("error writing config file: {e}"))
write_config(&config_path, &content)
}

pub fn resolve_workspace_id(provided: Option<String>, profile_config: &ProfileConfig) -> Result<String, String> {
Expand Down
2 changes: 1 addition & 1 deletion src/datasets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ pub fn create_from_url(
}
};
let api = ApiClient::new(Some(workspace_id));
create_dataset(&api, label, table_name, json!({ "Url": { "url": url } }), None);
create_dataset(&api, label, table_name, json!({ "url": url }), None);
}

pub fn create_from_query(
Expand Down
27 changes: 0 additions & 27 deletions src/init.rs

This file was deleted.

Loading