Skip to content

Commit

Permalink
Improved project management with make, burn, and new move comma…
Browse files Browse the repository at this point in the history
…nds (#81)

* rw `burn`; sync on `make` & `burn`
* implement function move operation
  • Loading branch information
dotxlem authored Jul 19, 2022
1 parent 5c41d5e commit bf0d5ab
Show file tree
Hide file tree
Showing 11 changed files with 941 additions and 449 deletions.
827 changes: 478 additions & 349 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "assemblylift-cli"
version = "0.4.0-alpha.4"
description = "AssemblyLift command line interface"
description = "The AssemblyLift Command Line Interface"
authors = ["Akkoro and the AssemblyLift contributors <assemblylift@akkoro.io>"]
edition = "2018"
license-file = "../LICENSE.md"
Expand All @@ -16,8 +16,8 @@ path = "src/main.rs"
[dependencies]
base64 = "0.13"
bytes = "1.1"
z85 = "3"
clap = "2.33"
dialoguer = "0.10"
handlebars = "3.5"
itertools = "0.10"
once_cell = "1.7"
Expand All @@ -26,6 +26,7 @@ serde = { version = "1", features = ["derive", "rc"] }
serde_json = "1"
toml = "0.5"
walkdir = "2.3"
z85 = "3"
zip = "0.6"

kube = { version = "0.71.0", features = ["runtime", "derive"] }
Expand Down
98 changes: 96 additions & 2 deletions cli/src/commands/burn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,101 @@
use std::rc::Rc;

use clap::ArgMatches;
use dialoguer::Confirm;

use crate::projectfs::{locate_asml_manifest, Project};
use crate::terraform;
use crate::transpiler::toml::asml::ServiceRef;
use crate::transpiler::toml::{asml, service};

pub fn command(matches: Option<&ArgMatches>) {
let matches = match matches {
Some(matches) => matches,
_ => panic!("could not get matches for make command"),
};

let manifest = match locate_asml_manifest() {
Some(manifest) => manifest,
None => panic!("could not find assemblylift.toml in tree"),
};
let mut manifest_dir = manifest.1.clone();
manifest_dir.pop();

let project = Project::new(manifest.0.project.name.clone(), Some(manifest_dir.clone()));

let mut resource_type: Option<&str> = None;
let mut resource_name: Option<&str> = None;
for el in matches
.values_of("resource")
.expect("must specify either 'service', 'function' as an argument to burn")
{
if resource_type.is_none() {
resource_type = Some(el);
continue;
}
if resource_name.is_none() {
resource_name = Some(el);
continue;
}
}

match resource_type {
// Some("all") => terraform::commands::destroy(),

Some("service") => {
let service_name = resource_name.unwrap();
if Confirm::new()
.with_prompt(format!(
"Are you sure you want to destroy service \"{}\"?\nThis is PERMANENT!",
service_name
))
.interact()
.unwrap()
{
let mut manifest: asml::Manifest = manifest.0.clone();
manifest.remove_service(service_name);
manifest
.write(manifest_dir.clone())
.expect("could not write assemblylift.toml");

let mut service_dir = manifest_dir.clone();
service_dir.push("services");
service_dir.push(service_name);
std::fs::remove_dir_all(service_dir).expect("could not remove service directory");
}
}

Some("function") => {
let resource_name = resource_name.unwrap().to_string();
let function_name: Vec<&str> = resource_name.split(".").collect();
if function_name.len() != 2 {
panic!("syntax is `burn function <service>.<function>`")
}

if Confirm::new()
.with_prompt(format!(
"Are you sure you want to destroy function \"{}\"\nThis is PERMANENT!",
resource_name
))
.interact()
.unwrap()
{
let service_dir = &*project.service_dir(function_name[0].into()).dir().clone();
let mut manifest_file = service_dir.clone();
manifest_file.push("service.toml");
let mut service_manifest = service::Manifest::read(&manifest_file).unwrap();
service_manifest.remove_function(function_name[1]);
service_manifest.write(service_dir.clone()).unwrap();
std::fs::remove_dir_all(
&*project
.service_dir(function_name[0].into())
.function_dir(function_name[1].into()),
)
.expect("could not remove service directory");
}
}

pub fn command(_matches: Option<&ArgMatches>) {
terraform::commands::destroy();
Some(_) => panic!("must specify either 'service', 'function', or 'all' as an argument to burn"),
None => panic!("must specify either 'service', 'function' or 'all' as an argument to burn"),
}
}
88 changes: 44 additions & 44 deletions cli/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn command(matches: Option<&ArgMatches>) {
let default_service_name = "my-service";
let default_function_name = "my-function";
let project_name = matches.value_of("project_name").unwrap();
let function_language = matches.value_of("language").unwrap();
// let function_language = matches.value_of("language").unwrap();

let project = Project::new(project_name.parse().unwrap(), None);

Expand All @@ -45,10 +45,10 @@ pub fn command(matches: Option<&ArgMatches>) {
"service_name".to_string(),
to_json(default_service_name.to_string()),
);
data.insert(
"function_language".to_string(),
to_json(function_language.to_string()),
);
// data.insert(
// "function_language".to_string(),
// to_json(function_language.to_string()),
// );
write_documents(
&project
.service_dir(String::from(default_service_name))
Expand All @@ -58,46 +58,46 @@ pub fn command(matches: Option<&ArgMatches>) {
);
}

match function_language {
"rust" => {
assert_prereqs();

std::fs::create_dir_all(format!(
"{}/src",
project
.service_dir(String::from(default_service_name))
.function_dir(String::from(default_function_name))
.to_str()
.unwrap()
))
.unwrap();

let data = &mut Map::<String, Json>::new();
data.insert(
"function_name".to_string(),
to_json(default_function_name.to_string()),
);
write_documents(
&project
.service_dir(String::from(default_service_name))
.function_dir(String::from(default_function_name)),
(*RUST_FUNCTION_DOCUMENTS).clone().as_ref(),
data,
);
}
"ruby" => {
write_documents(
&project
.service_dir(String::from(default_service_name))
.function_dir(String::from(default_function_name)),
(*RUBY_FUNCTION_DOCUMENTS).clone().as_ref(),
&mut Map::<String, Json>::new(),
);
}
unknown => panic!("unsupported language: {}", unknown),
}
// match function_language {
// "rust" => {
// assert_prereqs();
//
// std::fs::create_dir_all(format!(
// "{}/src",
// project
// .service_dir(String::from(default_service_name))
// .function_dir(String::from(default_function_name))
// .to_str()
// .unwrap()
// ))
// .unwrap();
//
// let data = &mut Map::<String, Json>::new();
// data.insert(
// "function_name".to_string(),
// to_json(default_function_name.to_string()),
// );
// write_documents(
// &project
// .service_dir(String::from(default_service_name))
// .function_dir(String::from(default_function_name)),
// (*RUST_FUNCTION_DOCUMENTS).clone().as_ref(),
// data,
// );
// }
// "ruby" => {
// write_documents(
// &project
// .service_dir(String::from(default_service_name))
// .function_dir(String::from(default_function_name)),
// (*RUBY_FUNCTION_DOCUMENTS).clone().as_ref(),
// &mut Map::<String, Json>::new(),
// );
// }
// unknown => panic!("unsupported language: {}", unknown),
// }

println!("\r\n✅ Done! Your project root is: {:?}", project.dir())
println!("\r\n✅ Done! Your project root is: {:?}", project.dir());
}

fn check_rust_prereqs() -> bool {
Expand Down
28 changes: 24 additions & 4 deletions cli/src/commands/make.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use clap::ArgMatches;
use handlebars::to_json;
use serde_json::value::{Map, Value as Json};
use std::rc::Rc;

use crate::projectfs::{locate_asml_manifest, Project};
use crate::templates::project::{RUBY_FUNCTION_DOCUMENTS, RUST_FUNCTION_DOCUMENTS, SERVICE_DOCUMENTS};
use crate::templates::project::{
RUBY_FUNCTION_DOCUMENTS, RUST_FUNCTION_DOCUMENTS, SERVICE_DOCUMENTS,
};
use crate::templates::write_documents;
use crate::transpiler::toml::{asml, service};
use crate::transpiler::toml::asml::ServiceRef;

pub fn command(matches: Option<&ArgMatches>) {
let matches = match matches {
Expand All @@ -16,10 +21,10 @@ pub fn command(matches: Option<&ArgMatches>) {
Some(manifest) => manifest,
None => panic!("could not find assemblylift.toml in tree"),
};
let mut manifest_dir = manifest.1;
let mut manifest_dir = manifest.1.clone();
manifest_dir.pop();

let project = Project::new(manifest.0.project.name, Some(manifest_dir));
let project = Project::new(manifest.0.project.name.clone(), Some(manifest_dir.clone()));

let mut resource_type: Option<&str> = None;
let mut resource_name: Option<&str> = None;
Expand All @@ -45,6 +50,10 @@ pub fn command(matches: Option<&ArgMatches>) {
.service_dir(String::from(resource_name.unwrap()))
.dir();
write_documents(&path, (*SERVICE_DOCUMENTS).clone().as_ref(), data);

let mut manifest: asml::Manifest = manifest.0.clone();
manifest.add_service(resource_name.unwrap());
manifest.write(manifest_dir.clone()).expect("could not write assemblylift.toml");
}

Some("function") => {
Expand All @@ -55,6 +64,13 @@ pub fn command(matches: Option<&ArgMatches>) {
panic!("syntax is `make function <service>.<function>`")
}

let service_dir = &*project.service_dir(function_name[0].into()).dir().clone();
let mut manifest_file = service_dir.clone();
manifest_file.push("service.toml");
let mut service_manifest = service::Manifest::read(&manifest_file).unwrap();
service_manifest.add_function(function_name[1], language);
service_manifest.write(service_dir.clone()).unwrap();

match language {
"rust" => {
let data = &mut Map::<String, Json>::new();
Expand All @@ -68,7 +84,11 @@ pub fn command(matches: Option<&ArgMatches>) {
let path = project
.service_dir(String::from(function_name[0]))
.function_dir(String::from(function_name[1]));
write_documents(&path, (*RUBY_FUNCTION_DOCUMENTS).clone().as_ref(), &mut Map::<String, Json>::new());
write_documents(
&path,
(*RUBY_FUNCTION_DOCUMENTS).clone().as_ref(),
&mut Map::<String, Json>::new(),
);
}
lang => panic!("function language `{}` is not supported", lang),
}
Expand Down
1 change: 1 addition & 0 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod burn;
pub mod cast;
pub mod init;
pub mod make;
pub mod r#move;
pub mod pack;
pub mod push;
pub mod user;
Loading

0 comments on commit bf0d5ab

Please sign in to comment.