Skip to content

Commit

Permalink
Tembo init should not populate unconfigured profiles (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuajerin committed Mar 28, 2024
1 parent 873b413 commit 6a29e1e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 35 deletions.
64 changes: 39 additions & 25 deletions tembo-cli/src/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@ use serde::Serialize;

use crate::tui;

// TODO: Move this to a template file
pub const CONTEXT_DEFAULT_TEXT: &str = "version = \"1.0\"
[[environment]]
name = 'local'
target = 'docker'
set = true
[[environment]]
name = 'prod'
target = 'tembo-cloud'
org_id = 'ORG_ID'
profile = 'prod'
# [[environment]]
# name = 'prod'
# target = 'tembo-cloud'
# org_id can be found in your tembo cloud url. Example: org_2bVDi36rsJNot2gwzP37enwxzMk
# org_id = 'Org ID here'
# profile = 'prod'
";

// TODO: Move this to a template file
pub const CREDENTIALS_DEFAULT_TEXT: &str = "version = \"1.0\"
[[profile]]
name = 'prod'
tembo_access_token = 'ACCESS_TOKEN'
tembo_host = 'https://api.tembo.io'
tembo_data_host = 'https://api.data-1.use1.tembo.io'
# Remove commented out profile to setup your environment
# [[profile]]
# name = 'prod'
# Generate an Access Token either through 'tembo login' or visit 'https://cloud.tembo.io/generate-jwt'
# tembo_access_token = 'Access token here'
# tembo_host = 'https://api.tembo.io'
# tembo_data_host = 'https://api.data-1.use1.tembo.io'
";

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -127,25 +129,37 @@ pub fn get_current_context() -> Result<Environment, anyhow::Error> {
let maybe_context = list_context()?;

if let Some(context) = maybe_context {
let maybe_profiles = list_credential_profiles()?;

if let Some(profiles) = maybe_profiles {
for mut env in context.environment {
if let Some(_is_set) = env.set {
if let Some(profile) = &env.profile {
let credential =
profiles.iter().rev().find(|c| &c.name == profile).unwrap();

env.selected_profile = Some(credential.to_owned());
}

for env in context.environment {
if let Some(true) = env.set {
if env.name == "local" {
return Ok(env);
} else {
let maybe_profiles = list_credential_profiles()?;

if let Some(profiles) = maybe_profiles {
if let Some(profile_name) = &env.profile {
let credential = profiles
.iter()
.rev()
.find(|c| &c.name == profile_name)
.ok_or_else(|| anyhow!("Profile not found in credentials"))?;

let mut env_with_profile = env.clone();
env_with_profile.selected_profile = Some(credential.clone());

return Ok(env_with_profile);
} else {
bail!("Environment is not set up properly. Check out your context");
}
} else {
bail!("Credentials file not found or invalid");
}
}
}
}
}

bail!("Tembo context not set");
bail!("Environment is not set up properly. Check out your context");
}

pub fn list_credential_profiles() -> Result<Option<Vec<Profile>>, anyhow::Error> {
Expand Down
19 changes: 11 additions & 8 deletions tembo-cli/src/cmd/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ struct TokenRequest {

pub fn execute() -> Result<(), anyhow::Error> {
let env = get_current_context()?;
let profile = env
.selected_profile
.as_ref()
.ok_or_else(|| anyhow!("Cannot log in to the local context, please select a tembo-cloud context before logging in"))?;
let login_url = url(profile)?;
let rt = tokio::runtime::Runtime::new().expect("Failed to create a runtime");

rt.block_on(handle_tokio(login_url))?;
if env.target == "tembo-cloud" {
let profile = env.selected_profile.as_ref().ok_or_else(|| {
anyhow!("Tembo-Cloud Environment is not setup properly. Run 'tembo init'")
})?;
let login_url = url(profile)?;
let rt = tokio::runtime::Runtime::new().expect("Failed to create a runtime");

rt.block_on(handle_tokio(login_url))?;
} else {
print!("Cannot log in to the local context, please select a tembo-cloud context before logging in");
}

Ok(())
}
Expand Down
6 changes: 5 additions & 1 deletion tembo-cli/src/cmd/validate.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::cli::context::{
list_context, list_credential_profiles, tembo_context_file_path, tembo_credentials_file_path,
Target,
};
use crate::cli::file_utils::FileUtils;
use crate::cli::tembo_config::InstanceSettings;
use crate::tui::{self, error, info, white_confirmation};
use anyhow::Error;
use clap::Args;
use std::{collections::HashMap, fs, path::Path, str::FromStr};
use tembo::cli::context::get_current_context;

/// Validates the tembo.toml file, context file, etc.
#[derive(Args)]
Expand Down Expand Up @@ -35,7 +37,9 @@ pub fn execute(verbose: bool) -> Result<(), anyhow::Error> {
);
has_error = true
} else {
list_credential_profiles()?;
if get_current_context()?.target == Target::TemboCloud.to_string() {
list_credential_profiles()?;
}
}
if verbose {
info("Credentials file exists");
Expand Down
36 changes: 35 additions & 1 deletion tembo-cli/tests/integration_tests_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ async fn minimal_cloud() -> Result<(), Box<dyn Error>> {
let test_dir = PathBuf::from(root_dir).join("examples").join("minimal");

env::set_current_dir(&test_dir)?;
let context_example_text = "version = \"1.0\"
[[environment]]
name = 'local'
target = 'docker'
[[environment]]
name = 'prod'
target = 'tembo-cloud'
org_id = 'ORG_ID'
profile = 'prod'
set = true";

let crendentials_example_text = "version = \"1.0\"
[[profile]]
name = 'prod'
tembo_access_token = 'ACCESS_TOKEN'
tembo_host = 'https://api.tembo.io'
tembo_data_host = 'https://api.data-1.use1.tembo.io'
";

replace_file(tembo_context_file_path(), context_example_text)?;
replace_file(tembo_credentials_file_path(), crendentials_example_text)?;

// tembo init
let mut cmd = Command::cargo_bin(CARGO_BIN)?;
Expand All @@ -32,7 +56,7 @@ async fn minimal_cloud() -> Result<(), Box<dyn Error>> {

setup_env(&instance_name)?;

// tembo context set --name local
// tembo context set --name prod
let mut cmd = Command::cargo_bin(CARGO_BIN)?;
cmd.arg("context");
cmd.arg("set");
Expand All @@ -46,6 +70,7 @@ async fn minimal_cloud() -> Result<(), Box<dyn Error>> {
cmd.assert().success();

let env = get_current_context()?;
println!("{:?}", env);
let profile = env.clone().selected_profile.unwrap();
let config = Configuration {
base_path: profile.get_tembo_host(),
Expand Down Expand Up @@ -137,6 +162,15 @@ fn replace_vars_in_file(
let new_data = data.replace(word_from, word_to);
let mut dst = File::create(&file_path)?;
dst.write(new_data.as_bytes())?;
drop(dst);

Ok(())
}

fn replace_file(file_path: String, word_to: &str) -> Result<(), Box<dyn Error>> {
let mut dst = File::create(&file_path)?;
dst.write_all(word_to.as_bytes())?;
drop(dst);
Ok(())
}

Expand Down

0 comments on commit 6a29e1e

Please sign in to comment.