-
Notifications
You must be signed in to change notification settings - Fork 2
Add function create-toml cmd #118
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| use std::{fs, path::PathBuf, str::FromStr}; | ||
|
|
||
| use clap::Parser; | ||
| use serde::Serialize; | ||
| use thiserror::Error; | ||
|
|
||
| use crate::{ | ||
| commands::interact::{input, preset_input, select, validated_input, validators}, | ||
| CmdOutput, | ||
| }; | ||
|
|
||
| #[derive(Serialize)] | ||
| struct FunctionConfig { | ||
| name: String, | ||
| language: String, | ||
| handler: String, | ||
| } | ||
|
|
||
| #[derive(Serialize)] | ||
| struct FunctionToml { | ||
| function: FunctionConfig, | ||
| } | ||
|
|
||
| /// Generate a toml configuration file for your Function | ||
| #[derive(Parser, Debug)] | ||
| pub struct CreateTomlArgs {} | ||
|
|
||
| #[derive(strum_macros::Display, Debug)] | ||
| pub enum CreateTomlPrompt { | ||
| #[strum(to_string = "Give your Function a name:")] | ||
| Name, | ||
| #[strum(to_string = "Select your Function's language:")] | ||
| Language, | ||
| #[strum(to_string = "What is the entry point to your function?:")] | ||
| Handler, | ||
| } | ||
|
|
||
| #[derive(strum_macros::Display, Debug)] | ||
| pub enum CreateTomlMessage { | ||
| #[strum(to_string = "Function configuration saved to function.toml.")] | ||
| Success, | ||
| } | ||
|
|
||
| impl CmdOutput for CreateTomlMessage { | ||
| fn exitcode(&self) -> crate::errors::ExitCode { | ||
| crate::errors::OK | ||
| } | ||
|
|
||
| fn code(&self) -> String { | ||
| match self { | ||
| CreateTomlMessage::Success => "function-create-toml-success", | ||
| } | ||
| .to_string() | ||
| } | ||
| } | ||
|
|
||
| #[derive(Error, Debug)] | ||
| pub enum CreateTomlError { | ||
| #[error("An IO error occurred: {0}")] | ||
| Io(#[from] std::io::Error), | ||
| #[error("A function.toml file already exists in the current directory")] | ||
| AlreadyExists, | ||
| } | ||
|
|
||
| impl CmdOutput for CreateTomlError { | ||
| fn exitcode(&self) -> crate::errors::ExitCode { | ||
| match self { | ||
| CreateTomlError::Io(_) => crate::errors::IOERR, | ||
| CreateTomlError::AlreadyExists => crate::errors::SOFTWARE, | ||
| } | ||
| } | ||
|
|
||
| fn code(&self) -> String { | ||
| match self { | ||
| CreateTomlError::Io(_) => "function-create-toml-io-error", | ||
| CreateTomlError::AlreadyExists => "function-create-toml-already-exists", | ||
| } | ||
| .to_string() | ||
| } | ||
| } | ||
|
|
||
| pub async fn run(_: CreateTomlArgs) -> Result<CreateTomlMessage, CreateTomlError> { | ||
| if PathBuf::from_str("./function.toml") | ||
| .expect("infallible") | ||
| .exists() | ||
| { | ||
| return Err(CreateTomlError::AlreadyExists); | ||
| } | ||
|
|
||
| let valid_languages: [&str; 5] = [ | ||
| "node@18", | ||
| "node@20", | ||
| "python@3.9", | ||
| "python@3.10", | ||
| "python@3.11", | ||
| ]; | ||
|
|
||
| let name = validated_input( | ||
| CreateTomlPrompt::Name, | ||
| false, | ||
| Box::new(validators::validate_function_name), | ||
| )?; | ||
|
|
||
| let langs = valid_languages | ||
| .iter() | ||
| .map(|lang| lang.to_string()) | ||
| .collect::<Vec<String>>(); | ||
|
|
||
| let language = select(&langs, 0, CreateTomlPrompt::Language).unwrap(); | ||
|
|
||
| let handler = preset_input(CreateTomlPrompt::Handler, "index.handler".to_string()).unwrap(); | ||
|
|
||
| let config = FunctionToml { | ||
| function: FunctionConfig { | ||
| name, | ||
| language: valid_languages[language].to_string(), | ||
| handler: handler.to_string(), | ||
| }, | ||
| }; | ||
|
|
||
| let toml = toml::to_string(&config).unwrap(); | ||
| fs::write("function.toml", toml)?; | ||
|
|
||
| return Ok(CreateTomlMessage::Success); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::{ | ||
| fs::File, | ||
| io::{self, BufReader, Read}, | ||
| }; | ||
| use thiserror::Error; | ||
|
|
||
| #[derive(Debug, Error)] | ||
| pub enum FunctionTomlError { | ||
| #[error( | ||
| "Relay configuration could not be found at {0}, specify a relay config file \ | ||
| with the --file flag. Or create a relay with ev relay create." | ||
| )] | ||
| ConfigNotFound(String), | ||
| #[error("Error reading relay config file: {0}")] | ||
| IoError(#[from] io::Error), | ||
| #[error("Error parsing relay config file: {0}")] | ||
| ParseError(#[from] toml::de::Error), | ||
| } | ||
|
|
||
| #[derive(Debug, Deserialize, Serialize)] | ||
| struct FunctionToml { | ||
| function: FunctionProps, | ||
| } | ||
|
|
||
| #[derive(Debug, Deserialize, Serialize)] | ||
| struct FunctionProps { | ||
| name: String, | ||
| language: String, | ||
| #[serde(default = "default_handler")] | ||
| handler: String, | ||
| } | ||
|
|
||
| fn default_handler() -> String { | ||
| "index.handler".to_string() | ||
| } | ||
|
|
||
| impl TryFrom<&std::path::PathBuf> for FunctionToml { | ||
| type Error = FunctionTomlError; | ||
|
|
||
| fn try_from(path: &std::path::PathBuf) -> Result<Self, Self::Error> { | ||
| if !path.try_exists()? { | ||
| return Err(FunctionTomlError::ConfigNotFound( | ||
| path.to_string_lossy().into(), | ||
| )); | ||
| } | ||
|
|
||
| let file = File::open(&path)?; | ||
| let mut buf_reader = BufReader::new(file); | ||
| let mut contents = String::new(); | ||
| buf_reader.read_to_string(&mut contents)?; | ||
|
|
||
| Ok(toml::from_str(&contents)?) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ mod auth; | |
| mod commands; | ||
| mod errors; | ||
| mod fs; | ||
| mod function; | ||
| mod relay; | ||
| mod theme; | ||
| mod tty; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not used yet