Skip to content

Commit

Permalink
pull apis and mails
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Sep 19, 2024
1 parent dd79fd5 commit cf7904e
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 64 deletions.
5 changes: 4 additions & 1 deletion cli/src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ pub enum EnvironmentsCommands {
// name: Option<String>,
},
/// ⚠️ be careful, this will clear all environments
Clear {},
Clear {
#[arg(value_name = "FORCE", short = 'f', long = "force")]
force: Option<bool>,
},
/// change the default environment to the specified name
Switch {
#[arg(value_name = "NAME", short = 'n', long = "name")]
Expand Down
32 changes: 20 additions & 12 deletions cli/src/commands/enviroments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) struct Environment {

pub(crate) async fn run(command: EnvironmentsCommands) -> DaikokuResult<()> {
match command {
EnvironmentsCommands::Clear {} => clear(),
EnvironmentsCommands::Clear { force } => clear(force.unwrap_or(false)),
EnvironmentsCommands::Add {
name,
server,
Expand Down Expand Up @@ -93,22 +93,30 @@ fn set_content_file(content: &String) -> DaikokuResult<()> {
.map_err(|err| DaikokuCliError::FileSystem(err.to_string()))
}

fn clear() -> DaikokuResult<()> {
fn clearing() -> DaikokuResult<()> {
match set_content_file(&"".to_string()) {
Ok(_) => {
logger::println("<green>Environments erased</>".to_string());
Ok(())
}
Err(e) => Err(DaikokuCliError::FileSystem(format!(
"failed to reset the environments file : {}",
e.to_string()
))),
}
}

fn clear(force: bool) -> DaikokuResult<()> {
if force {
return clearing();
}

logger::error("Are you to delete all environments ? [yN]".to_string());

let choice = prompt()?;

if choice.trim() == "y" {
match set_content_file(&"".to_string()) {
Ok(_) => {
logger::println("<green>Environments erased</>".to_string());
Ok(())
}
Err(e) => Err(DaikokuCliError::FileSystem(format!(
"failed to reset the environments file : {}",
e.to_string()
))),
}
clearing()
} else {
Ok(())
}
Expand Down
80 changes: 80 additions & 0 deletions cli/tests/commands/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::ffi;

use assert_cmd::{assert::Assert, Command};

pub(crate) fn run<I, S>(args: I) -> Assert
where
I: IntoIterator<Item = S>,
S: AsRef<ffi::OsStr>,
{
Command::cargo_bin("daikoku").unwrap().args(args).run()
}

pub(crate) trait AssertCommand {
fn run(&mut self) -> Assert;
}

impl AssertCommand for Command {
fn run(&mut self) -> Assert {
self.assert().success()
}
}

pub struct Environment {}

impl Environment {
pub(crate) fn info(name: &str) -> Assert {
run(["environments", "info", format!("--name={}", name).as_str()])
}

pub(crate) fn clear(force: bool) -> Assert {
run([
"environments",
"clear",
format!("--force={}", force.to_string()).as_str(),
])
}

pub(crate) fn add(name: &str, daikoku_ip: &str /* apikey: String*/) -> Assert {
run([
"environments",
"add",
format!("--name={}", name).as_str(),
format!("--server=http://{}:8080", daikoku_ip).as_str(),
"--apikey=amJ1UWtEYWpZZThWVTU0a2RjVW1oWjhWM0I1Q0NmV1I6eTJXNmtYV21yRzBxdm8xU2psSjdYU1M0ZEE5cGc5dDlZZ25wMTlIOXR5cUJaZE5NSkZDQmRJUXVKQ3haMXk4VQ=="
])
}

pub(crate) fn switch(name: &str) -> Assert {
run([
"environments",
"switch",
format!("--name={}", name).as_str(),
])
}

pub(crate) fn login() -> Assert {
run(["login"])
}
}

pub struct Cms {}

impl Cms {
pub(crate) fn clear(force: bool) -> Assert {
run([
"cms",
"clear",
format!("--force={}", force.to_string()).as_str(),
])
}

pub(crate) fn init(name: &str, path: String) -> Assert {
run([
"cms",
"init",
format!("--name={}", name).as_str(),
format!("--path={}", path).as_str(),
])
}
}
1 change: 1 addition & 0 deletions cli/tests/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod cli;
90 changes: 42 additions & 48 deletions cli/tests/login.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,52 @@
// use assert_cmd::prelude::*;
// use serial_test::serial;
// use std::fs;
use testcontainers::{
core::{IntoContainerPort, WaitFor},
runners::AsyncRunner,
GenericImage, ImageExt,
};

// const WASMO_TEST_FOLDER: &str = "/tmp/daikoku";
// struct Setup {
// temporary_path: String,
// }

// impl Setup {
// fn new() -> Self {
// let temporary_path = WASMO_TEST_FOLDER.to_string();

// let _ = fs::remove_dir_all(&temporary_path);

// match fs::create_dir(&temporary_path) {
// Err(err) => println!("{:?}", err),
// Ok(v) => println!("{:?}", v),
// }
// Setup {
// temporary_path: temporary_path,
// }
// }

// fn clean(&self) {
// fs::remove_dir_all(&self.temporary_path).expect("Failed to remove folder")
// }
// }
mod commands;
mod setup;

use std::fs;

use commands::cli::{Cms, Environment};
use setup::start_containers;

async fn create_cms() -> Result<(), Box<dyn std::error::Error + 'static>> {
let temporary_path = std::env::temp_dir()
.join("daikoku")
.into_os_string()
.into_string()
.unwrap();

let _ = fs::remove_dir_all(&temporary_path);

Cms::clear(true);

let _ = fs::create_dir(&temporary_path);

Cms::init("cms", temporary_path);

Ok(())
}

fn test_check_info_of_environment() {
let result = Environment::info("dev");
let output = String::from_utf8(result.get_output().stdout.clone()).unwrap();

assert!(output.contains("http://localhost:8080"));
assert!(output.contains("dev"));
}

#[tokio::test]
async fn login() -> Result<(), Box<dyn std::error::Error + 'static>> {
let postgres = GenericImage::new("postgres", "13")
.with_exposed_port(5432.tcp())
.with_wait_for(WaitFor::seconds(1))
.with_env_var("POSTGRES_USER", "postgres")
.with_env_var("POSTGRES_PASSWORD", "postgres")
.with_env_var("POSTGRES_DB", "daikoku");
let (_postgres_container, _daikoku_container) = start_containers().await?;

let daikoku_ip = "localhost"; //daikoku_container.get_host().await?.to_string();

create_cms().await?;

let daikoku = GenericImage::new("maif/daikoku", "17.5.0")
.with_wait_for(WaitFor::seconds(1))
.with_env_var("daikoku.mode", "dev")
.with_env_var("Ddaikoku.postgres.database", "daikoku")
.with_env_var("daikoku.exposedOn", "9000");
Environment::add("dev", &daikoku_ip);

let postgres_container = postgres.start().await?;
let daikoku_container = daikoku.start().await?;
test_check_info_of_environment();

println!("{:#?}", postgres_container);
Environment::switch("dev");


Environment::login();

Ok(())
}
Loading

0 comments on commit cf7904e

Please sign in to comment.