|
| 1 | +use clap::Args; |
| 2 | + |
| 3 | +use crate::api::admin::{accounts, projects}; |
| 4 | +use crate::core::config::{self, ConfigFile, Connection, Defaults}; |
| 5 | +use crate::core::error::Result; |
| 6 | +use crate::core::rest_client::RestClient; |
| 7 | + |
| 8 | +#[derive(Debug, Args)] |
| 9 | +pub struct InitArgs { |
| 10 | + /// Disable interactive selection menus (use plain prompts only) |
| 11 | + #[arg(long)] |
| 12 | + pub non_interactive: bool, |
| 13 | +} |
| 14 | + |
| 15 | +pub async fn exec(args: &InitArgs) -> Result<()> { |
| 16 | + eprintln!("Setting up dbtp credentials.\n"); |
| 17 | + |
| 18 | + let host = config::prompt("dbt Cloud host", "https://cloud.getdbt.com"); |
| 19 | + let token = config::prompt("API token", ""); |
| 20 | + |
| 21 | + let host = if !host.starts_with("https://") && !host.starts_with("http://") { |
| 22 | + format!("https://{host}") |
| 23 | + } else { |
| 24 | + host |
| 25 | + }; |
| 26 | + |
| 27 | + let account_id_str = config::prompt("Account ID", ""); |
| 28 | + let account_id = account_id_str |
| 29 | + .parse::<u64>() |
| 30 | + .map_err(|_| crate::core::error::DbtpError::config("Account ID must be a number"))?; |
| 31 | + |
| 32 | + // Validate the connection |
| 33 | + eprint!("\nValidating connection..."); |
| 34 | + match accounts::get_by_id(&host, &token, account_id).await { |
| 35 | + Ok(_) => eprintln!(" ok"), |
| 36 | + Err(e) => { |
| 37 | + eprintln!(" failed\nWarning: could not verify credentials: {e}"); |
| 38 | + eprintln!("Saving anyway. Run `dbtp accounts show` to verify later.\n"); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + let project_id = if !args.non_interactive && config::is_interactive() && !token.is_empty() { |
| 43 | + pick_project(&host, &token, account_id).await |
| 44 | + } else { |
| 45 | + None |
| 46 | + }; |
| 47 | + |
| 48 | + let file = ConfigFile { |
| 49 | + connection: Connection { |
| 50 | + host: Some(host), |
| 51 | + token: Some(token), |
| 52 | + account_id: Some(account_id), |
| 53 | + }, |
| 54 | + defaults: Defaults { |
| 55 | + project_id, |
| 56 | + output: "table".to_string(), |
| 57 | + }, |
| 58 | + }; |
| 59 | + |
| 60 | + config::save_config(&file)?; |
| 61 | + |
| 62 | + let path = config::config_path()?; |
| 63 | + eprintln!("\nConfiguration saved to {}", path.display()); |
| 64 | + eprintln!("Run `dbtp config set project-id <id-or-name>` to set a default project."); |
| 65 | + |
| 66 | + Ok(()) |
| 67 | +} |
| 68 | + |
| 69 | +async fn pick_project(host: &str, token: &str, account_id: u64) -> Option<String> { |
| 70 | + let client = match RestClient::new(host, token, Some(account_id)) { |
| 71 | + Ok(c) => c, |
| 72 | + Err(e) => { |
| 73 | + eprintln!("\nCould not build API client: {e}"); |
| 74 | + return None; |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + eprintln!("\nFetching projects..."); |
| 79 | + let list = match projects::list(&client, &[], None).await { |
| 80 | + Ok(l) => l, |
| 81 | + Err(e) => { |
| 82 | + eprintln!("Could not fetch projects: {e}"); |
| 83 | + return None; |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + if list.is_empty() { |
| 88 | + eprintln!(" No projects found."); |
| 89 | + return None; |
| 90 | + } |
| 91 | + |
| 92 | + let items: Vec<(String, String)> = list |
| 93 | + .iter() |
| 94 | + .filter_map(|p| { |
| 95 | + let name = p["name"].as_str()?.to_string(); |
| 96 | + let id = p["id"].as_u64()?.to_string(); |
| 97 | + Some((name, id)) |
| 98 | + }) |
| 99 | + .collect(); |
| 100 | + |
| 101 | + config::prompt_select("Select default project", &items) |
| 102 | +} |
0 commit comments