Skip to content

Commit

Permalink
Implement port annotation (#49)
Browse files Browse the repository at this point in the history
* feat: improved port creation and refactor

* feat: improved port commands

* chore: removed utils

* feat: improved init context
  • Loading branch information
paulobressan authored Oct 4, 2024
1 parent 356a0d4 commit fc7a5df
Show file tree
Hide file tree
Showing 21 changed files with 283 additions and 626 deletions.
131 changes: 65 additions & 66 deletions Cargo.lock

Large diffs are not rendered by default.

37 changes: 0 additions & 37 deletions src/api/account.rs

This file was deleted.

40 changes: 0 additions & 40 deletions src/api/format.rs

This file was deleted.

160 changes: 0 additions & 160 deletions src/api/mod.rs

This file was deleted.

76 changes: 21 additions & 55 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::collections::HashMap;
use miette::{Context as MietteContext, IntoDiagnostic};
use serde::{Deserialize, Serialize};

use crate::rpc;

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct Config {
pub contexts: HashMap<String, Context>,
Expand All @@ -12,24 +14,21 @@ pub struct Config {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Context {
pub project: Project,
pub cloud: Cloud,
pub operator: Operator,
pub auth: Auth,
}

impl Context {
pub fn ephemeral(id: &str, namespace: &str, api_key: &str) -> Self {
let project = crate::context::Project::new(id, namespace, None);
pub async fn ephemeral(id: &str, api_key: &str) -> miette::Result<Self> {
let project = rpc::projects::find_by_id(
rpc::auth::Credential::Secret((id.into(), api_key.into())),
id,
)
.await?;

let project = crate::context::Project::new(id, &project.namespace, Some(project.name));
let auth = crate::context::Auth::api_key(api_key);
let cloud = crate::context::Cloud::default();
let operator = crate::context::Operator::default();

Self {
project,
auth,
cloud,
operator,
}
Ok(Self { project, auth })
}
}

Expand Down Expand Up @@ -67,36 +66,6 @@ impl Auth {
}
}

const DEFAULT_CLOUD: &str = "cloud0.txpipe.io";

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Cloud {
pub name: String,
}

impl Default for Cloud {
fn default() -> Self {
Self {
name: DEFAULT_CLOUD.to_string(),
}
}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Operator {
pub name: String,
pub entrypoint: String,
}

impl Default for Operator {
fn default() -> Self {
Self {
name: "TxPipe".to_owned(),
entrypoint: "us1.demeter.run".to_owned(),
}
}
}

pub fn load_config(dirs: &crate::dirs::Dirs) -> miette::Result<Config> {
let location = dirs.root_dir().join("config.toml");

Expand Down Expand Up @@ -174,7 +143,7 @@ pub fn load_context_by_name(
) -> miette::Result<Option<Context>> {
let mut config = load_config(dirs)?;
let out = config.contexts.remove(name);
return Ok(out);
Ok(out)
}

pub fn load_default_context(dirs: &crate::dirs::Dirs) -> miette::Result<Option<Context>> {
Expand All @@ -188,22 +157,19 @@ pub fn load_default_context(dirs: &crate::dirs::Dirs) -> miette::Result<Option<C

Ok(None)
}
pub fn infer_context(
id: Option<&str>,

pub async fn infer_context(
name: Option<&str>,
namespace: Option<&str>,
project_id: Option<&str>,
api_key: Option<&str>,
dirs: &crate::dirs::Dirs,
) -> miette::Result<Option<Context>> {
match (id, name, namespace, api_key) {
(Some(id), None, Some(ns), Some(ak)) => Ok(Some(Context::ephemeral(id, ns, ak))),
(None, None, None, None) => load_default_context(dirs),
(None, Some(context), None, None) => load_context_by_name(context, dirs),
(None, None, None, Some(_)) => Err(miette::miette!("missing namespace or id value")),
(Some(_), None, Some(_), None) => Err(miette::miette!("missing api key value")),
(..) => Err(miette::miette!(
"conflicting values, specify either a context or namespace"
)),
match (name, project_id, api_key) {
(None, Some(id), Some(ak)) => Ok(Some(Context::ephemeral(id, ak).await?)),
(None, None, Some(_)) => Err(miette::miette!("missing project id value")),
(None, Some(_), None) => Err(miette::miette!("missing api key value")),
(Some(context), _, _) => load_context_by_name(context, dirs),
_ => load_default_context(dirs),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn ensure_root_dir(explicit: Option<&Path>) -> miette::Result<PathBuf> {
Ok(defined)
}

#[derive(Debug)]
pub struct Dirs {
root_dir: PathBuf,
}
Expand Down
9 changes: 1 addition & 8 deletions src/init/login.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use miette::{bail, Context as _, IntoDiagnostic as _};
use miette::bail;
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, time::Duration};

use crate::api;

async fn find_login_url() -> (String, String) {
let mut params = HashMap::new();
params.insert("client_id", "gpJ63MG5g1V1PKufM9WHGjjeAe7yCT8L");
Expand Down Expand Up @@ -96,11 +94,6 @@ pub async fn run() -> miette::Result<String> {
let (status, access_token) = poll_token(&device_code).await;

if status.is_success() {
api::account::initialize_user(&access_token)
.await
.into_diagnostic()
.context("initializing user")?;

println!("login successful!");
return Ok(access_token);
}
Expand Down
Loading

0 comments on commit fc7a5df

Please sign in to comment.