Skip to content

Commit

Permalink
feat: add list services command
Browse files Browse the repository at this point in the history
  • Loading branch information
tmlye committed Feb 17, 2024
1 parent df3da8a commit ad9982f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 72 deletions.
98 changes: 34 additions & 64 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use reqwest::blocking::Response;

use self::types::{ListOrganizationResponse, Organization, CreateEnvironmentResponse};
use self::types::{ListOrganizationResponse, Organization, CreateEnvironmentResponse, ListServicesResponse};

pub mod types;

Expand All @@ -24,15 +25,7 @@ impl APIClient {
token: &str,
) -> Result<ListOrganizationResponse, reqwest::Error> {
let url = format!("{}/orgs", self.base_url);

let response = self.client
.get(url)
.header("User-Agent", self.user_agent.as_str())
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.send()?
.error_for_status()?;

let response = self.get(&url, token)?;
response.json()
}

Expand All @@ -42,32 +35,18 @@ impl APIClient {
name: &str
) -> Result<ListOrganizationResponse, reqwest::Error> {
let url = format!("{}/organization", self.base_url);

let response = self.client
.get(url)
.header("User-Agent", self.user_agent.as_str())
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.send()?
.error_for_status()?;

let response = self.get(&url, token)?;
response.json()
}

pub fn get_applications(
pub fn get_services(
&self,
token: &str,
) -> Result<ListOrganizationResponse, reqwest::Error> {
let url = format!("{}/organization", self.base_url);

let response = self.client
.get(url)
.header("User-Agent", self.user_agent.as_str())
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.send()?
.error_for_status()?;

org_name: &str,
env_name: &str
) -> Result<ListServicesResponse, reqwest::Error> {
let url = format!("{}/orgs/{}/envs/{}/svcs", self.base_url, org_name, env_name);
let response = self.get(&url, token)?;
response.json()
}

Expand All @@ -78,21 +57,10 @@ impl APIClient {
billing_email: &str,
) -> Result<Organization, reqwest::Error> {
let url = format!("{}/orgs", self.base_url);

let mut body = HashMap::new();
body.insert("name", name);
body.insert("billing_email", billing_email);

let response = self
.client
.post(url)
.header("User-Agent", self.user_agent.as_str())
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.json(&body)
.send()?
.error_for_status()?;

let response = self.post(&url, token, &body)?;
response.json()
}

Expand All @@ -102,15 +70,7 @@ impl APIClient {
org_name: &str
) -> Result<Vec<String>, reqwest::Error> {
let url = format!("{}/orgs/{}/envs", self.base_url, org_name);

let response = self.client
.get(url)
.header("User-Agent", self.user_agent.as_str())
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.send()?
.error_for_status()?;

let response = self.get(&url, token)?;
response.json()
}

Expand All @@ -121,20 +81,9 @@ impl APIClient {
org_name: &str
) -> Result<CreateEnvironmentResponse, reqwest::Error> {
let url = format!("{}/orgs/{}/envs", self.base_url, org_name);

let mut body = HashMap::new();
body.insert("name", name);

let response = self
.client
.post(url)
.header("User-Agent", self.user_agent.as_str())
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.json(&body)
.send()?
.error_for_status()?;

let response = self.post(&url, token, &body)?;
response.json()
}

Expand All @@ -145,4 +94,25 @@ impl APIClient {

response.json()
}

fn get(&self, url: &str, token: &str) -> Result<Response, reqwest::Error> {
return self.client
.get(url)
.header("User-Agent", self.user_agent.as_str())
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.send()?
.error_for_status();
}

fn post(&self, url: &str, token: &str, body: &HashMap<&str, &str>) -> Result<Response, reqwest::Error> {
return self.client
.post(url)
.header("User-Agent", self.user_agent.as_str())
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.json(&body)
.send()?
.error_for_status();
}
}
10 changes: 8 additions & 2 deletions src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ pub struct CreateEnvironmentResponse {
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Application {
pub struct ListServicesResponse {
pub services: Vec<Service>
}

#[derive(Serialize, Deserialize, Debug, Tabled)]
pub struct Service {
name: String,
organization_id: String,
image: String,
container_port: u16,
}
43 changes: 40 additions & 3 deletions src/commands/apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::{Parser, Subcommand};
use dialoguer::{FuzzySelect, Input};
use std::path::Path;
use super::CommandBase;
use tabled::Table;

use crate::config::{
application::{Build, HttpService},
Expand All @@ -19,16 +20,17 @@ use crate::config::{
subcommand_required = true,
arg_required_else_help = true
)]
pub struct Apps {
pub struct Services {
#[command(subcommand)]
pub command: Option<Commands>,
}

impl Apps {
impl Services {
pub fn execute(&self, base: &mut CommandBase) -> Result<()> {
match &self.command {
Some(Commands::Deploy(depl)) => depl.execute(base),
Some(Commands::Initialize(init)) => init.execute(base),
Some(Commands::List(list)) => list.execute(base),
None => Ok(())
}
}
Expand All @@ -42,6 +44,8 @@ pub enum Commands {
Deploy(Deploy),
/// Generate Dockerfile and Molnett manifest
Initialize(Initialize),
/// List services
List(List)
}

#[derive(Parser)]
Expand Down Expand Up @@ -72,7 +76,6 @@ impl Deploy {
// 4. submit change
Ok(())
}

}

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -294,3 +297,37 @@ impl InitPlan {
InitPlanBuilder::new(base)
}
}

#[derive(Parser)]
#[derive(Debug)]
pub struct List {
#[arg(long, help = "Organization to list the services of")]
org: Option<String>,
#[arg(long, help = "Environment to list the services of")]
env: String,
}

impl List {
pub fn execute(&self, base: &CommandBase) -> Result<()> {
let org_name = if self.org.is_some() {
self.org.clone().unwrap()
} else {
base.user_config().get_default_org().unwrap().to_string()
};
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;

let response = base.api_client().get_services(
token,
&org_name,
&self.env
)?;

let table = Table::new(response.services).to_string();
println!("{}", table);

Ok(())
}
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ enum Commands {
Orgs(commands::orgs::Orgs),
/// Login to Molnett
Auth(commands::auth::Auth),
/// Deploy and manage apps
Apps(commands::apps::Apps),
/// Deploy and manage services
Services(commands::apps::Services),
/// Create and manage environments
Environments(commands::environments::Environments),
}
Expand All @@ -53,7 +53,7 @@ fn main() -> Result<()> {
let mut base = CommandBase::new(&mut config);

match cli.command {
Some(Commands::Apps(apps)) => apps.execute(&mut base),
Some(Commands::Services(svcs)) => svcs.execute(&mut base),
Some(Commands::Auth(auth)) => auth.execute(&mut base),
Some(Commands::Environments(environments)) => environments.execute(&mut base),
Some(Commands::Orgs(orgs)) => orgs.execute(&mut base),
Expand Down

0 comments on commit ad9982f

Please sign in to comment.