From 4a54755c3d2722717aa390bd3ed6b7eec314c977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyrill=20N=C3=A4f?= Date: Mon, 17 Jun 2024 10:44:49 +0000 Subject: [PATCH] Enhance init.sh accepting Go module + README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Cyrill Näf --- README.md | 2 +- init.sh | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5b022051..c3a078ad 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ If you just want to jump in and get started: 1. Replace `function-template-go` with your function in `go.mod`, `package/crossplane.yaml`, and any Go imports. (You can also do this - automatically by running the `./init.sh ` script.) + automatically by running the `./init.sh ` script.) 1. Update `input/v1beta1/` to reflect your desired input (and run `go generate`) 1. Add your logic to `RunFunction` in `fn.go` 1. Add tests for your logic in `fn_test.go` diff --git a/init.sh b/init.sh index 523fbb50..8a453d47 100755 --- a/init.sh +++ b/init.sh @@ -3,19 +3,25 @@ # This script helps initialize a new function project by # replacing all instances of function-template-go with the # name of your function. The scripts accepts two arguments: -# 1. The name of your function +# 1. The go module of your function, example: github.com/my-org/my-function # 2. The path to your function directory set -e -cd "$2" || return +# Arguments +module_path="$1" +project_dir="$2" +# Extract the last element of the module path using `basename` +function_name=$(basename "${module_path}") -# Replace function-template-go with the name of your function +cd "$project_dir" || return + +# Replaces function-template-go with the name of your function # in go.mod -perl -pi -e s,function-template-go,"$1",g go.mod +perl -pi -e s,github.com/crossplane/function-template-go,"${module_path}",g go.mod # in fn.go -perl -pi -e s,function-template-go,"$1",g fn.go +perl -pi -e s,github.com/crossplane/function-template-go,"${module_path}",g fn.go # in examples -perl -pi -e s,function-template-go,"$1",g example/* +perl -pi -e s,function-template-go,"${function_name}",g example/* -echo "Function $1 has been initialised successfully" +echo "Function ${function_name} has been initialised successfully"