#![allow(warnings)]
#[macro_use]
extern crate async_trait;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate starlane_macros;
pub static VERSION: Lazy<semver::Version> =
Lazy::new(|| semver::Version::from_str(env!("CARGO_PKG_VERSION").trim()).unwrap() );
pub mod template;
pub mod err;
pub mod properties;
pub mod env;
pub mod platform;
#[cfg(test)]
pub mod test;
#[cfg(feature="space")]
pub mod space {
pub use starlane_space::space::*;
}
#[cfg(feature="service")]
pub mod service;
#[cfg(feature = "hyperspace")]
pub mod hyperspace;
#[cfg(feature = "hyperlane")]
pub mod hyperlane;
pub mod registry;
pub mod host;
pub mod executor;
#[cfg(feature = "cli")]
pub mod cli;
pub mod driver;
#[cfg(feature = "server")]
mod server;
#[cfg(feature = "server")]
pub use server::*;
use crate::cli::{Cli, Commands};
use clap::Parser;
use starlane::space::loc::ToBaseKind;
use std::fs::File;
use std::io::{Read, Seek, Write};
use std::path::Path;
use std::process;
use std::str::FromStr;
use std::time::Duration;
use once_cell::sync::Lazy;
use tokio::fs::DirEntry;
use tokio::runtime::Builder;
use zip::write::FileOptions;
use crate::platform::Platform;
pub fn init() {
#[cfg(feature = "cli")]
{
use rustls::crypto::aws_lc_rs::default_provider;
default_provider()
.install_default()
.expect("crypto provider could not be installed");
}
}
#[cfg(feature = "cli")]
pub fn main() -> Result<(), anyhow::Error> {
init();
let cli = Cli::parse();
match cli.command {
Commands::Serve => server(),
Commands::Term(args) => {
let runtime = Builder::new_multi_thread().enable_all().build()?;
match runtime.block_on(async move { cli::term(args).await }) {
Ok(_) => Ok(()),
Err(err) => {
println!("err! {}", err.to_string());
Err(err.into())
}
}
}
Commands::Version => {
println!("{}", VERSION.to_string());
Ok(())
}
}
}
#[cfg(not(feature = "server"))]
fn server() -> Result<(), OldStarErr> {
println!("'serve' feature is not enabled in this starlane installation")
}
#[cfg(feature = "server")]
fn server() -> Result<(), anyhow::Error> {
ctrlc::set_handler(move || {
std::process::exit(1);
});
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
runtime.block_on(async move {
let starlane = Starlane::new().await.unwrap();
let machine_api = starlane.machine();
let api = tokio::time::timeout(Duration::from_secs(30), machine_api)
.await
.unwrap().unwrap();
loop {
tokio::time::sleep(Duration::from_secs(60)).await;
}
});
Ok(())
}
#[cfg(test)]
pub mod test {
#[test]
pub fn test() {}
}
pub fn zip_dir<T>(
it: impl Iterator<Item = DirEntry>,
prefix: &str,
writer: T,
method: zip::CompressionMethod,
) -> zip::result::ZipResult<T>
where
T: Write + Seek,
{
let mut zip = zip::ZipWriter::new(writer);
let options = FileOptions::default()
.compression_method(method)
.unix_permissions(0o755);
let mut buffer = Vec::new();
for entry in it {
let path = entry.path();
let name = path.strip_prefix(Path::new(prefix)).unwrap();
if path.is_file() {
zip.start_file(name.to_str().unwrap(), options)?;
let mut f = File::open(path)?;
f.read_to_end(&mut buffer)?;
zip.write_all(&*buffer)?;
buffer.clear();
} else if !name.as_os_str().is_empty() {
zip.add_directory(name.to_str().unwrap(), options)?;
}
}
let result = zip.finish()?;
Result::Ok(result)
}