Skip to content
Draft
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
25 changes: 11 additions & 14 deletions src/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use oauth2::{
};
use tiny_http::{Response, Server};

use crate::config::user::Token;

use super::CommandBase;
use crate::config::user::{Token, UserConfig};

#[derive(Parser, Debug)]
#[command(
Expand All @@ -30,10 +28,10 @@ pub struct Auth {
}

impl Auth {
pub fn execute(self, base: CommandBase) -> Result<()> {
pub fn execute(self) -> Result<()> {
match self.command {
Some(Commands::Login(login)) => login.execute(base),
Some(Commands::Docker(docker)) => docker.execute(base),
Some(Commands::Login(login)) => login.execute(),
Some(Commands::Docker(docker)) => docker.execute(),
None => Ok(()),
}
}
Expand All @@ -52,12 +50,12 @@ pub enum Commands {
pub struct Login {}

impl Login {
pub fn execute(self, mut base: CommandBase) -> Result<()> {
pub fn execute(self) -> Result<()> {
let server = Server::http("localhost:0").unwrap();
let local_port = server.server_addr().to_ip().unwrap().port();
let redirect_uri = format!("http://localhost:{}/oauth2/callback", local_port);

let url = base.user_config().get_url();
let url = UserConfig::get_url();
let client = BasicClient::new(
ClientId::new("124a489e-93f7-4dd6-abae-1ed4c692bdc7".to_string()),
None,
Expand Down Expand Up @@ -104,7 +102,7 @@ impl Login {
token.expiry = Some(Utc::now() + chrono::Duration::hours(1));
}

base.user_config_mut().write_token(token)?;
UserConfig::set_token(token)?;

request.respond(Response::from_string("Success! You can close this tab now"))?;

Expand All @@ -116,12 +114,11 @@ impl Login {
pub struct Docker {}

impl Docker {
pub fn execute(self, base: CommandBase) -> Result<()> {
let token = base.user_config.get_token().ok_or_else(|| {
anyhow!("Could not get Molnett token. Please run molnctl auth login.")
})?;
pub fn execute(self) -> Result<()> {
let token = UserConfig::get_token()
.ok_or_else(|| anyhow!("Could not get Molnett token. Please run molnctl auth login."))?;

if base.user_config.is_token_expired() {
if UserConfig::is_token_expired() {
println!("Token expired. Please run molnctl auth login.");
return Ok(());
}
Expand Down
23 changes: 7 additions & 16 deletions src/commands/environments.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::CommandBase;
use anyhow::{anyhow, Result};
use anyhow::Result;
use clap::{Parser, Subcommand};
use dialoguer::FuzzySelect;
use tabled::Table;
Expand Down Expand Up @@ -54,13 +54,10 @@ pub struct Create {
impl Create {
pub fn execute(self, base: CommandBase) -> Result<()> {
let org_name = base.get_org()?;
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;
let token = base.get_token()?;

let response = base.api_client().create_environment(
token,
&token,
&self.name,
&org_name,
self.copy_from.as_deref(),
Expand All @@ -79,12 +76,9 @@ pub struct List {}
impl List {
pub fn execute(self, base: CommandBase) -> Result<()> {
let org_name = base.get_org()?;
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;
let token = base.get_token()?;

let response = base.api_client().get_environments(token, &org_name)?;
let response = base.api_client().get_environments(&token, &org_name)?;

let table = Table::new(response.environments).to_string();
println!("{}", table);
Expand All @@ -104,18 +98,15 @@ pub struct Delete {
impl Delete {
pub fn execute(self, base: CommandBase) -> Result<()> {
let org_name = base.get_org()?;
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;
let token = base.get_token()?;

if !self.confirm_deletion(&org_name)? {
println!("Delete cancelled");
return Ok(());
}

base.api_client()
.delete_environment(token, &org_name, &self.name)?;
.delete_environment(&token, &org_name, &self.name)?;

println!("Environment {} deleted", self.name);
Ok(())
Expand Down
36 changes: 14 additions & 22 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,34 @@ pub mod environments;
pub mod orgs;
pub mod secrets;
pub mod services;
pub mod projects;

pub struct CommandBase<'a> {
user_config: &'a mut UserConfig,
pub struct CommandBase {
org_arg: Option<String>,
}

impl CommandBase<'_> {
pub fn new(user_config: &mut UserConfig, org_arg: Option<String>) -> CommandBase {
impl CommandBase {
pub fn new(org_arg: Option<String>) -> CommandBase {
CommandBase {
user_config,
org_arg,
}
}

pub fn api_client(&self) -> APIClient {
let url = self.user_config.get_url();
APIClient::new(url)
APIClient::new(&UserConfig::get_url())
}

pub fn user_config(&self) -> &UserConfig {
self.user_config
}

pub fn user_config_mut(&mut self) -> &mut UserConfig {
self.user_config
pub fn get_token(&self) -> Result<String> {
UserConfig::get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))
}

pub fn get_org(&self) -> Result<String> {
let org_name = if self.org_arg.is_some() {
self.org_arg.clone().unwrap()
} else {
match self.user_config.get_default_org() {
Some(cfg) => cfg.to_string(),
None => return Err(anyhow!("Either set a default org in the config or provide one via --org"))
}
};
Ok(org_name)
if let Some(org) = &self.org_arg {
return Ok(org.clone());
}

UserConfig::get_default_org()
.ok_or_else(|| anyhow!("Either set a default org in the config or provide one via --org"))
}
}
24 changes: 9 additions & 15 deletions src/commands/orgs.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 tabled::Table;

use crate::config::user::UserConfig;
use super::CommandBase;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -44,12 +45,8 @@ pub struct List {}

impl List {
pub fn execute(self, base: CommandBase) -> Result<()> {
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;

let response = base.api_client().get_organizations(token)?;
let token = base.get_token()?;
let response = base.api_client().get_organizations(&token)?;

let table = Table::new(response.organizations).to_string();
println!("{}", table);
Expand All @@ -69,18 +66,14 @@ pub struct Create {

impl Create {
pub fn execute(self, base: CommandBase) -> Result<()> {
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;

let token = base.get_token()?;
let plan = CreatePlan::builder()
.name(self.name.as_deref())
.billing_email(self.billing_email.as_deref())
.build()?;

let response = base.api_client().create_organization(
token,
&token,
plan.name.as_str(),
plan.billing_email.as_str(),
)?;
Expand Down Expand Up @@ -169,10 +162,11 @@ pub struct Switch {
}

impl Switch {
pub fn execute(self, mut base: CommandBase) -> Result<()> {
pub fn execute(self, base: CommandBase) -> Result<()> {
let token = base.get_token()?;
let orgs = base
.api_client()
.get_organizations(base.user_config().get_token().unwrap())?;
.get_organizations(&token)?;
let org_names = orgs
.organizations
.iter()
Expand All @@ -198,7 +192,7 @@ impl Switch {
org_names[selection].to_string()
};

match base.user_config_mut().write_default_org(org_name) {
match UserConfig::set_default_org(org_name) {
Ok(_) => Ok(()),
Err(err) => {
println!("Error while writing config: {}", err);
Expand Down
38 changes: 38 additions & 0 deletions src/commands/projects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::config::user::UserConfig;

// This just serves as an example for now
// of how to create a command that is hidden from the help menu
// based on the user's permissions.

pub fn is_hidden() -> bool {
!UserConfig::get_permissions().contains(&"tenant:manage".to_string())
}

#[derive(Debug, Parser)]
#[command(
author,
version,
about,
long_about,
subcommand_required = true,
arg_required_else_help = true,
hide = is_hidden(),
)]
pub struct Projects {
#[command(subcommand)]
pub command: Option<Commands>,
}

impl Projects {
pub fn execute(self) -> Result<()> {
match self.command {
None => Ok(()),
}
}
}

#[derive(Debug, Subcommand)]
pub enum Commands {
}
23 changes: 7 additions & 16 deletions src/commands/secrets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::CommandBase;
use anyhow::{anyhow, Result};
use anyhow::Result;
use clap::{Parser, Subcommand};
use dialoguer::{FuzzySelect, Input};
use std::io::{self, BufRead};
Expand Down Expand Up @@ -54,10 +54,7 @@ pub struct Create {
impl Create {
pub fn execute(self, base: CommandBase) -> Result<()> {
let org_name = base.get_org()?;
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;
let token = base.get_token()?;

let value: String = if let Some(true) = self.stdin {
self.read_stdin()?
Expand All @@ -69,7 +66,7 @@ impl Create {
};

base.api_client()
.create_secret(token, &org_name, &self.env, &self.name, &value)?;
.create_secret(&token, &org_name, &self.env, &self.name, &value)?;

println!("Secret {} created", &self.name);
Ok(())
Expand Down Expand Up @@ -106,12 +103,9 @@ pub struct List {
impl List {
pub fn execute(self, base: CommandBase) -> Result<()> {
let org_name = base.get_org()?;
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;
let token = base.get_token()?;

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

let table = Table::new(response.secrets).to_string();
println!("{}", table);
Expand All @@ -133,10 +127,7 @@ pub struct Delete {
impl Delete {
pub fn execute(self, base: CommandBase) -> Result<()> {
let org_name = base.get_org()?;
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;
let token = base.get_token()?;

if let Some(false) = self.no_confirm {
let prompt = format!("Org: {}, Environment: {}, Secret: {}. Are you sure you want to delete this secret?", org_name, self.env, self.name);
Expand All @@ -149,7 +140,7 @@ impl Delete {
}

base.api_client()
.delete_secret(token, &org_name, &self.env, &self.name)?;
.delete_secret(&token, &org_name, &self.env, &self.name)?;

println!("Secret {} deleted", self.name);
Ok(())
Expand Down
Loading