Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add lagon link command #275

Merged
merged 1 commit into from
Nov 24, 2022
Merged
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
5 changes: 5 additions & 0 deletions .changeset/twelve-rats-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/cli': minor
---

Add `lagon link` command
16 changes: 8 additions & 8 deletions packages/cli/src/commands/deploy.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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