From 42f43db2b1b2e1a01360d9538d025aefaeb46860 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 5 Jul 2024 10:23:46 -0300 Subject: [PATCH] Basic runtime template implementation --- examples/hello_crystal/README.md | 3 ++ runtimes/crystal/README.md | 4 +++ runtimes/crystal/template/README.md.j2 | 11 ++++++ runtimes/crystal/template/funko.cr | 16 +++++++++ runtimes/crystal/template/funko.yml | 2 ++ src/commands/new.cr | 47 +++++++++++++++++++++++++- 6 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 examples/hello_crystal/README.md create mode 100644 runtimes/crystal/README.md create mode 100644 runtimes/crystal/template/README.md.j2 create mode 100644 runtimes/crystal/template/funko.cr create mode 100644 runtimes/crystal/template/funko.yml diff --git a/examples/hello_crystal/README.md b/examples/hello_crystal/README.md new file mode 100644 index 0000000..95081fc --- /dev/null +++ b/examples/hello_crystal/README.md @@ -0,0 +1,3 @@ +# README + +This is the readme for people trying to *use* this runtime. \ No newline at end of file diff --git a/runtimes/crystal/README.md b/runtimes/crystal/README.md new file mode 100644 index 0000000..63918c9 --- /dev/null +++ b/runtimes/crystal/README.md @@ -0,0 +1,4 @@ +# README + +This is the readme for people wanting to change this runtime, +not for people trying to use it \ No newline at end of file diff --git a/runtimes/crystal/template/README.md.j2 b/runtimes/crystal/template/README.md.j2 new file mode 100644 index 0000000..0e8aef8 --- /dev/null +++ b/runtimes/crystal/template/README.md.j2 @@ -0,0 +1,11 @@ +# Readme for {{name | title}} + +This is a funko using the Crystal runtime for [FaaSO](https://git.ralsina.me/ralsina/faaso) + +## What is {{name | title}} + +Write here what it is + +## How to use {{name | title}} + +And so on. diff --git a/runtimes/crystal/template/funko.cr b/runtimes/crystal/template/funko.cr new file mode 100644 index 0000000..75828c0 --- /dev/null +++ b/runtimes/crystal/template/funko.cr @@ -0,0 +1,16 @@ +require "kemal" + +# This is a kemal app, you can add handlers, middleware, etc. + +# A basic hello world get endpoint +get "/" do + "Hello World Crystal!" +end + +# The `/ping/` endpoint is configured in the container as a healthcheck +# You can make it better by checking that your database is responding +# or whatever checks you think are important +# +get "/ping/" do + "OK" +end diff --git a/runtimes/crystal/template/funko.yml b/runtimes/crystal/template/funko.yml new file mode 100644 index 0000000..44354bb --- /dev/null +++ b/runtimes/crystal/template/funko.yml @@ -0,0 +1,2 @@ +name: hello +runtime: crystal diff --git a/src/commands/new.cr b/src/commands/new.cr index 44b6e57..628bd0b 100644 --- a/src/commands/new.cr +++ b/src/commands/new.cr @@ -22,15 +22,60 @@ module Faaso return 0 end - # Create folder with a preconfigured funko for this runtime + # Get runtime template files list + template_base = "" + template_files = [] of String if @@known.includes? "./runtimes/#{runtime}" Log.info { "Using known runtime #{runtime}" } + template_base = "./runtimes/#{runtime}/template" + template_files = @@filelist.select { |f| f.starts_with? template_base } elsif File.exists? runtime Log.info { "Using directory #{runtime} as runtime" } + template_base = "#{runtime}/template" + template_files = Dir.glob("#{template_base}/**/*") else Log.error { "Can't find runtime #{runtime}" } return 1 end + + pp! template_files + + # Create new folder + if Dir.exists? folder + Log.error { "Folder #{folder} already exists" } + return 1 + end + + Dir.mkdir_p folder + + template_files.each do |f| + content = IO::Memory.new + # We need to use RUCKSACK_MODE=0 so it + # fallbacks to the filesystem + rucksack(f).read(content) + if content.nil? + Log.error { "Can't find file #{f}" } + return 1 + end + + # f is like "#{template_base}/foo" + # dst is like #{folder}/foo + dst = Path[folder] / Path[f].relative_to(template_base) + # Render templated files + if f.ends_with? ".j2" + dst = dst.sibling(dst.stem) + Log.info { "Creating file #{dst} from #{f}" } + File.open(dst, "w") do |file| + file << Crinja.render(content.to_s, {"name" => Path[folder].basename}) + end + else # Just copy the file + Log.info { "Creating file #{dst} from #{f}" } + File.open(dst, "w") do |file| + file << content.to_s + end + end + end + 0 end end