generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add "ftl init" with an initial scaffolder for Go (#491)
Will add Kotlin in a followup. This currently works, but `ftl-go deploy` needs to be adjusted to support go.mod in the same directory as ftl.toml. ``` ftl init go ./examples/foo --go-module=github.com/TBD54566975/ftl/examples/foo ```
- Loading branch information
1 parent
c43ad68
commit e689666
Showing
25 changed files
with
323 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -12,5 +12,5 @@ reflex.conf | |
/node_modules | ||
*.tsbuildinfo | ||
generated_ftl_module.go | ||
|
||
scaffolding.zip | ||
dist/ |
This file contains 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 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 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,52 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/alecthomas/errors" | ||
|
||
"github.com/TBD54566975/ftl/backend/common/exec" | ||
"github.com/TBD54566975/ftl/backend/common/log" | ||
goruntime "github.com/TBD54566975/ftl/go-runtime" | ||
"github.com/TBD54566975/ftl/internal" | ||
) | ||
|
||
type initCmd struct { | ||
Hermit bool `default:"true" help:"Include Hermit language-specific toolchain binaries in the module." negatable:""` | ||
Go initGoCmd `cmd:"" help:"Initialize a new FTL Go module."` | ||
Kotlin initKotlinCmd `cmd:"" help:"Initialize a new FTL Kotlin module."` | ||
} | ||
|
||
type initGoCmd struct { | ||
Dir string `arg:"" default:"." type:"dir" help:"Directory to initialize the module in."` | ||
Name string `help:"Name of the FTL module (defaults to name of directory)."` | ||
GoModule string `required:"" help:"Go module path."` | ||
} | ||
|
||
func (i initGoCmd) Run(ctx context.Context, parent *initCmd) error { | ||
if i.Name == "" { | ||
i.Name = filepath.Base(i.Dir) | ||
} | ||
if err := internal.Scaffold(goruntime.Files, i.Dir, i); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
if !parent.Hermit { | ||
if err := os.RemoveAll(filepath.Join(i.Dir, "bin")); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
} | ||
if err := exec.Command(ctx, log.Info, i.Dir, "go", "mod", "tidy").Run(); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
return nil | ||
} | ||
|
||
type initKotlinCmd struct { | ||
Dir string `arg:"" default:"." help:"Directory to initialize the module in."` | ||
} | ||
|
||
func (i *initKotlinCmd) Run() error { | ||
panic("??") | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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,22 @@ | ||
//go:build !release | ||
|
||
package goruntime | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// Files is the FTL Go runtime scaffolding files. | ||
var Files = func() fs.FS { | ||
cmd := exec.Command("git", "rev-parse", "--show-toplevel") | ||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
panic(err) | ||
} | ||
dir := filepath.Join(strings.TrimSpace(string(out)), "go-runtime", "scaffolding") | ||
return os.DirFS(dir) | ||
}() |
This file contains 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,25 @@ | ||
//go:build release | ||
|
||
package goruntime | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
_ "embed" | ||
"io/fs" | ||
) | ||
|
||
//go:embed scaffolding.zip | ||
var archive []byte | ||
|
||
// Files is the FTL Go runtime scaffolding files. | ||
// | ||
// scaffolding.zip can be generated by running `bit go-runtime/scaffolding.zip` | ||
// or indirectly via `bit build/release/ftl`. | ||
var Files fs.FS = func() fs.FS { | ||
zr, err := zip.NewReader(bytes.NewReader(archive), int64(len(archive))) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return zr | ||
}() |
This file contains 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 @@ | ||
hermit |
This file contains 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,7 @@ | ||
# Hermit environment | ||
|
||
This is a [Hermit](https://github.com/cashapp/hermit) bin directory. | ||
|
||
The symlinks in this directory are managed by Hermit and will automatically | ||
download and install Hermit itself as well as packages. These packages are | ||
local to this environment. |
This file contains 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,21 @@ | ||
#!/bin/bash | ||
# This file must be used with "source bin/activate-hermit" from bash or zsh. | ||
# You cannot run it directly | ||
# | ||
# THIS FILE IS GENERATED; DO NOT MODIFY | ||
|
||
if [ "${BASH_SOURCE-}" = "$0" ]; then | ||
echo "You must source this script: \$ source $0" >&2 | ||
exit 33 | ||
fi | ||
|
||
BIN_DIR="$(dirname "${BASH_SOURCE[0]:-${(%):-%x}}")" | ||
if "${BIN_DIR}/hermit" noop > /dev/null; then | ||
eval "$("${BIN_DIR}/hermit" activate "${BIN_DIR}/..")" | ||
|
||
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ]; then | ||
hash -r 2>/dev/null | ||
fi | ||
|
||
echo "Hermit environment $("${HERMIT_ENV}"/bin/hermit env HERMIT_ENV) activated" | ||
fi |
This file contains 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 @@ | ||
.go-1.21.3.pkg |
This file contains 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 @@ | ||
.go-1.21.3.pkg |
This file contains 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,43 @@ | ||
#!/bin/bash | ||
# | ||
# THIS FILE IS GENERATED; DO NOT MODIFY | ||
|
||
set -eo pipefail | ||
|
||
export HERMIT_USER_HOME=~ | ||
|
||
if [ -z "${HERMIT_STATE_DIR}" ]; then | ||
case "$(uname -s)" in | ||
Darwin) | ||
export HERMIT_STATE_DIR="${HERMIT_USER_HOME}/Library/Caches/hermit" | ||
;; | ||
Linux) | ||
export HERMIT_STATE_DIR="${XDG_CACHE_HOME:-${HERMIT_USER_HOME}/.cache}/hermit" | ||
;; | ||
esac | ||
fi | ||
|
||
export HERMIT_DIST_URL="${HERMIT_DIST_URL:-https://github.com/cashapp/hermit/releases/download/stable}" | ||
HERMIT_CHANNEL="$(basename "${HERMIT_DIST_URL}")" | ||
export HERMIT_CHANNEL | ||
export HERMIT_EXE=${HERMIT_EXE:-${HERMIT_STATE_DIR}/pkg/hermit@${HERMIT_CHANNEL}/hermit} | ||
|
||
if [ ! -x "${HERMIT_EXE}" ]; then | ||
echo "Bootstrapping ${HERMIT_EXE} from ${HERMIT_DIST_URL}" 1>&2 | ||
INSTALL_SCRIPT="$(mktemp)" | ||
# This value must match that of the install script | ||
INSTALL_SCRIPT_SHA256="180e997dd837f839a3072a5e2f558619b6d12555cd5452d3ab19d87720704e38" | ||
if [ "${INSTALL_SCRIPT_SHA256}" = "BYPASS" ]; then | ||
curl -fsSL "${HERMIT_DIST_URL}/install.sh" -o "${INSTALL_SCRIPT}" | ||
else | ||
# Install script is versioned by its sha256sum value | ||
curl -fsSL "${HERMIT_DIST_URL}/install-${INSTALL_SCRIPT_SHA256}.sh" -o "${INSTALL_SCRIPT}" | ||
# Verify install script's sha256sum | ||
openssl dgst -sha256 "${INSTALL_SCRIPT}" | \ | ||
awk -v EXPECTED="$INSTALL_SCRIPT_SHA256" \ | ||
'$2!=EXPECTED {print "Install script sha256 " $2 " does not match " EXPECTED; exit 1}' | ||
fi | ||
/bin/bash "${INSTALL_SCRIPT}" 1>&2 | ||
fi | ||
|
||
exec "${HERMIT_EXE}" --level=fatal exec "$0" -- "$@" |
Empty file.
This file contains 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,2 @@ | ||
module = "{{ .Name | lower }}" | ||
language = "go" |
This file contains 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,5 @@ | ||
module {{ .GoModule }} | ||
|
||
go 1.21.0 | ||
|
||
require github.com/TBD54566975/ftl latest |
This file contains 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,18 @@ | ||
//ftl:module {{ .Name | lower }} | ||
package {{ .Name | lower }} | ||
|
||
import ( | ||
"context" | ||
_ "github.com/TBD54566975/ftl/go-runtime/sdk" // Import the FTL SDK. | ||
) | ||
|
||
type {{ .Name | camel }}Request struct { | ||
} | ||
|
||
type {{ .Name | camel }}Response struct { | ||
} | ||
|
||
//ftl:verb | ||
func {{ .Name | camel }}(ctx context.Context, req {{ .Name | camel }}Request) ({{ .Name | camel }}Response, error) { | ||
return {{ .Name | camel }}Response{}, nil | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.