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

Enhance init.sh to update Go module paths as well #75

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <function-name>` script.)
automatically by running the `./init.sh <Go module: github.com/my-org/my-fn-name>` 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`
Expand Down
20 changes: 13 additions & 7 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"