Skip to content

Commit

Permalink
feat(cli): add lagon link command (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz committed Nov 24, 2022
1 parent 58bb4d9 commit 54b0714
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-rats-dance.md
@@ -0,0 +1,5 @@
---
'@lagon/cli': minor
---

Add `lagon link` command
16 changes: 8 additions & 8 deletions packages/cli/src/commands/deploy.rs
Expand Up @@ -13,9 +13,9 @@ use crate::utils::{
};

#[derive(Deserialize, Debug)]
struct Organization {
name: String,
id: String,
pub struct Organization {
pub name: String,
pub id: String,
}

impl Display for Organization {
Expand All @@ -24,7 +24,7 @@ impl Display for Organization {
}
}

type OrganizationsResponse = Vec<Organization>;
pub type OrganizationsResponse = Vec<Organization>;

#[derive(Serialize, Debug)]
struct CreateFunctionRequest {
Expand All @@ -40,9 +40,9 @@ struct CreateFunctionResponse {
}

#[derive(Deserialize, Debug)]
struct Function {
id: String,
name: String,
pub struct Function {
pub id: String,
pub name: String,
}

impl Display for Function {
Expand All @@ -51,7 +51,7 @@ impl Display for Function {
}
}

type FunctionsResponse = Vec<Function>;
pub type FunctionsResponse = Vec<Function>;

pub async fn deploy(
file: PathBuf,
Expand Down
64 changes: 64 additions & 0 deletions packages/cli/src/commands/link.rs
@@ -0,0 +1,64 @@
use anyhow::{anyhow, Result};
use std::path::PathBuf;

use dialoguer::Select;

use crate::{
commands::deploy::{FunctionsResponse, OrganizationsResponse},
utils::{
debug, get_function_config, success, validate_code_file, write_function_config, Config,
DeploymentConfig, TrpcClient,
},
};

pub async fn link(file: PathBuf) -> Result<()> {
let config = Config::new()?;

if config.token.is_none() {
return Err(anyhow!(
"You are not logged in. Please login with `lagon login`",
));
}

validate_code_file(&file)?;

match get_function_config(&file)? {
None => {
println!("{}", debug("No deployment config found..."));
println!();

let trpc_client = TrpcClient::new(&config);
let response = trpc_client
.query::<(), OrganizationsResponse>("organizationsList", None)
.await?;
let organizations = response.result.data;

let index = Select::new().items(&organizations).default(0).interact()?;
let organization = &organizations[index];

let response = trpc_client
.query::<(), FunctionsResponse>("functionsList", None)
.await?;

let index = Select::new()
.items(&response.result.data)
.default(0)
.interact()?;
let function = &response.result.data[index];

write_function_config(
&file,
DeploymentConfig {
function_id: function.id.clone(),
organization_id: organization.id.clone(),
},
)?;

println!("{}", success("Function linked!"));
println!();

Ok(())
}
Some(_) => Err(anyhow!("This file is already linked to a Function.")),
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/commands/mod.rs
@@ -1,13 +1,15 @@
mod build;
mod deploy;
mod dev;
mod link;
mod login;
mod logout;
mod rm;

pub use build::build;
pub use deploy::deploy;
pub use dev::dev;
pub use link::link;
pub use login::login;
pub use logout::logout;
pub use rm::rm;
7 changes: 7 additions & 0 deletions packages/cli/src/main.rs
Expand Up @@ -85,6 +85,12 @@ enum Commands {
#[clap(short, long, value_parser)]
public_dir: Option<PathBuf>,
},
/// Link a local Function file to an already deployed Function
Link {
/// Path to the file containing the Function
#[clap(value_parser)]
file: PathBuf,
},
}

#[tokio::main]
Expand Down Expand Up @@ -115,6 +121,7 @@ async fn main() {
client,
public_dir,
} => commands::build(file, client, public_dir),
Commands::Link { file } => commands::link(file).await,
} {
println!("{}", error(&err.to_string()));
}
Expand Down

0 comments on commit 54b0714

Please sign in to comment.