Skip to content

Commit

Permalink
cmd: prepare cli plugin (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Jul 4, 2024
1 parent 726b3fb commit ebee0c0
Show file tree
Hide file tree
Showing 23 changed files with 121 additions and 349 deletions.
10 changes: 10 additions & 0 deletions .github/scripts/plugin-manifest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

export CLI_VERSION=$GITHUB_REF_NAME
export MACOS_AMD64_SHA256=$(sha256sum "_output/hasura-ndc-go-darwin-amd64" | awk '{ print $1 }')
export MACOS_ARM64_SHA256=$(sha256sum "_output/hasura-ndc-go-darwin-arm64" | awk '{ print $1 }')
export LINUX_AMD64_SHA256=$(sha256sum "_output/hasura-ndc-go-linux-amd64" | awk '{ print $1 }')
export LINUX_ARM64_SHA256=$(sha256sum "_output/hasura-ndc-go-linux-arm64" | awk '{ print $1 }')
export WINDOWS_AMD64_SHA256=$(sha256sum "_output/hasura-ndc-go-windows-amd64.exe" | awk '{ print $1 }')

envsubst < .github/scripts/plugin-manifest.yaml > release/manifest.yaml
41 changes: 41 additions & 0 deletions .github/scripts/plugin-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: ndc-go
version: "${CLI_VERSION}"
shortDescription: "CLI plugin for Hasura ndc-go"
homepage: https://github.com/hasura/ndc-sdk-go
hidden: true
platforms:
- selector: darwin-arm64
uri: "https://github.com/hasura/ndc-sdk-go/releases/download/${CLI_VERSION}/hasura-ndc-go-darwin-arm64"
sha256: "${MACOS_ARM64_SHA256}"
bin: "hasura-ndc-go"
files:
- from: "./hasura-ndc-go-darwin-arm64"
to: "hasura-ndc-go"
- selector: linux-arm64
uri: "https://github.com/hasura/ndc-sdk-go/releases/download/${CLI_VERSION}/hasura-ndc-go-linux-arm64"
sha256: "${LINUX_ARM64_SHA256}"
bin: "hasura-ndc-go"
files:
- from: "./hasura-ndc-go-linux-arm64"
to: "hasura-ndc-go"
- selector: darwin-amd64
uri: "https://github.com/hasura/ndc-sdk-go/releases/download/${CLI_VERSION}/hasura-ndc-go-darwin-amd64"
sha256: "${MACOS_AMD64_SHA256}"
bin: "hasura-ndc-go"
files:
- from: "./hasura-ndc-go-darwin-amd64"
to: "hasura-ndc-go"
- selector: windows-amd64
uri: "https://github.com/hasura/ndc-sdk-go/releases/download/${CLI_VERSION}/hasura-ndc-go-windows-amd64.exe"
sha256: "${WINDOWS_AMD64_SHA256}"
bin: "hasura-ndc-go.exe"
files:
- from: "./hasura-ndc-go-windows-amd64.exe"
to: "hasura-ndc-go.exe"
- selector: linux-amd64
uri: "https://github.com/hasura/ndc-sdk-go/releases/download/${CLI_VERSION}/hasura-ndc-go-linux-amd64"
sha256: "${LINUX_AMD64_SHA256}"
bin: "hasura-ndc-go"
files:
- from: "./hasura-ndc-go-linux-amd64"
to: "hasura-ndc-go"
6 changes: 6 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ jobs:
run: |
VERSION="$GITHUB_REF_NAME" make ci-build-codegen
mkdir release
.github/scripts/plugin-manifest.sh
mv _output/* release
- uses: actions/upload-artifact@v4
with:
path: release/manifest.yaml
if-no-files-found: error

- uses: actions/upload-artifact@v4
with:
path: release/hasura-ndc-go-*
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
run: |
make build-codegen
pushd example/codegen
../../_output/hasura-ndc-go generate
../../_output/hasura-ndc-go update
go test -v -race -timeout 3m ./...
popd
- name: Run ndc-test
Expand Down
10 changes: 5 additions & 5 deletions cmd/hasura-ndc-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Commands:

hasura-ndc-go new -n example -m github.com/foo/example

generate
update
Generate schema and implementation for the connector from functions.

test snapshots
Expand All @@ -44,9 +44,9 @@ Commands:

The `new` command generates a boilerplate project for connector development from [template](templates/new) with the following folder structure:

- `functions`: the folder contains query and mutation functions. The `generate` command will parse `.go` files in this folder.
- `functions`: the folder contains query and mutation functions. The `update` command will parse `.go` files in this folder.
- `types`: the folder contains reusable types such as `RawConfiguration`, `Configuration` and `State`.
- `connector.go`: parts of Connector methods, except `GetSchema`, `Query` and `Mutation` methods that will be generated by the `generate` command.
- `connector.go`: parts of Connector methods, except `GetSchema`, `Query` and `Mutation` methods that will be generated by the `update` command.
- `main.go`: the main function that runs the connector CLI.
- `go.mod`: the module file with required dependencies.
- `README.md`: the index README file.
Expand All @@ -60,13 +60,13 @@ hasura-ndc-go new -n example -m github.com/foo/example -o .

### Generate queries and mutations

The `generate` command parses code in the `functions` folder, finds functions and types that are allowed to be exposed and generates the following files:
The `update` command parses code in the `functions` folder, finds functions and types that are allowed to be exposed and generates the following files:

- `schema.generated.json`: the generated connector schema in JSON format.
- `connector.generated.go`: implement `GetSchema`, `Query` and `Mutation` methods with exposed functions.

```bash
hasura-ndc-go generate
hasura-ndc-go update
```

## How it works
Expand Down
2 changes: 1 addition & 1 deletion cmd/hasura-ndc-go/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewConnectorGenerator(basePath string, connectorDir string, moduleName stri
}
}

func parseAndGenerateConnector(args *GenerateArguments, moduleName string) error {
func parseAndGenerateConnector(args *UpdateArguments, moduleName string) error {
if cli.Generate.Trace != "" {
w, err := os.Create(cli.Generate.Trace)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/hasura-ndc-go/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestConnectorGeneration(t *testing.T) {
srcDir := path.Join(tc.BasePath, "source")
assert.NoError(t, os.Chdir(srcDir))

err = parseAndGenerateConnector(&GenerateArguments{
err = parseAndGenerateConnector(&UpdateArguments{
ConnectorDir: tc.ConnectorDir,
PackageTypes: tc.PackageTypes,
Directories: tc.Directories,
Expand Down
64 changes: 36 additions & 28 deletions cmd/hasura-ndc-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
"github.com/rs/zerolog/log"
)

type GenerateArguments struct {
Path string `help:"The path of the root directory where the go.mod file is present" short:"p" default:"."`
// UpdateArguments represent input arguments of the `update` command
type UpdateArguments struct {
Path string `help:"The path of the root directory where the go.mod file is present" short:"p" env:"HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH" default:"."`
ConnectorDir string `help:"The directory where the connector.go file is placed" default:"."`
PackageTypes string `help:"The name of types package where the State struct is in"`
Directories []string `help:"Folders contain NDC operation functions" short:"d"`
Expand All @@ -28,9 +29,10 @@ type NewArguments struct {
}

var cli struct {
LogLevel string `help:"Log level." enum:"debug,info,warn,error" default:"info"`
New NewArguments `cmd:"" help:"Initialize an NDC connector boilerplate. For example:\n hasura-ndc-go new -n example -m github.com/foo/example"`
Generate GenerateArguments `cmd:"" help:"Generate schema and implementation for the connector from functions."`
LogLevel string `help:"Log level." enum:"debug,info,warn,error" env:"HASURA_PLUGIN_LOG_LEVEL" default:"info"`
New NewArguments `cmd:"" help:"Initialize an NDC connector boilerplate. For example:\n hasura-ndc-go new -n example -m github.com/foo/example"`
Update UpdateArguments `cmd:"" help:"Generate schema and implementation for the connector from functions."`
Generate UpdateArguments `cmd:"" help:"(deprecated) The alias of the 'update' command."`
Test struct {
Snapshots command.GenTestSnapshotArguments `cmd:"" help:"Generate test snapshots."`
} `cmd:"" help:"Test helpers."`
Expand Down Expand Up @@ -61,30 +63,10 @@ func main() {
}
log.Info().Str("exec_time", time.Since(start).Round(time.Second).String()).
Msg("generated successfully")
case "update":
execUpdate(&cli.Update, start)
case "generate":
log.Info().
Str("path", cli.Generate.Path).
Str("connector_dir", cli.Generate.ConnectorDir).
Str("package_types", cli.Generate.PackageTypes).
Msg("generating connector schema...")

moduleName, err := getModuleName(cli.Generate.Path)
if err != nil {
log.Fatal().Err(err).Msg("failed to get module name. The base path must contain a go.mod file")
}

if err := os.Chdir(cli.Generate.Path); err != nil {
log.Fatal().Err(err).Msg("")
}

if err = parseAndGenerateConnector(&cli.Generate, moduleName); err != nil {
log.Fatal().Err(err).Msg("failed to generate connector schema")
}
if err := execGoFormat("."); err != nil {
log.Fatal().Err(err).Msg("failed to format code")
}
log.Info().Str("exec_time", time.Since(start).Round(time.Millisecond).String()).
Msg("generated successfully")
execUpdate(&cli.Generate, start)
case "test snapshots":
log.Info().
Str("endpoint", cli.Test.Snapshots.Endpoint).
Expand All @@ -102,6 +84,32 @@ func main() {
}
}

func execUpdate(args *UpdateArguments, start time.Time) {
log.Info().
Str("path", args.Path).
Str("connector_dir", args.ConnectorDir).
Str("package_types", args.PackageTypes).
Msg("generating connector schema...")

moduleName, err := getModuleName(args.Path)
if err != nil {
log.Fatal().Err(err).Msg("failed to get module name. The base path must contain a go.mod file")
}

if err := os.Chdir(args.Path); err != nil {
log.Fatal().Err(err).Msg("")
}

if err = parseAndGenerateConnector(args, moduleName); err != nil {
log.Fatal().Err(err).Msg("failed to generate connector schema")
}
if err := execGoFormat("."); err != nil {
log.Fatal().Err(err).Msg("failed to format code")
}
log.Info().Str("exec_time", time.Since(start).Round(time.Millisecond).String()).
Msg("generated successfully")
}

func setupGlobalLogger(level string) {
logLevel, err := zerolog.ParseLevel(level)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/hasura-ndc-go/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (sp SchemaParser) FindPackageByPath(input string) *packages.Package {
return nil
}

func parseRawConnectorSchemaFromGoCode(ctx context.Context, moduleName string, filePath string, args *GenerateArguments) (*RawConnectorSchema, error) {
func parseRawConnectorSchemaFromGoCode(ctx context.Context, moduleName string, filePath string, args *UpdateArguments) (*RawConnectorSchema, error) {
rawSchema := NewRawConnectorSchema()

pkgTypes, err := evalPackageTypesLocation(args.PackageTypes, moduleName, filePath, args.ConnectorDir)
Expand Down
2 changes: 1 addition & 1 deletion cmd/hasura-ndc-go/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func generateNewProject(args *NewArguments, silent bool) error {
}

log.Info().Msg("generating connector functions...")
if err := parseAndGenerateConnector(&GenerateArguments{
if err := parseAndGenerateConnector(&UpdateArguments{
Path: ".",
Directories: []string{"functions", "types"},
}, args.Module); err != nil {
Expand Down
63 changes: 0 additions & 63 deletions cmd/hasura-ndc-go/templates/new/.air.toml.tmpl

This file was deleted.

63 changes: 0 additions & 63 deletions cmd/hasura-ndc-go/templates/new/.air.windows.toml.tmpl

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@ packagingDefinition:
type: ManagedDockerBuild
supportedEnvironmentVariables: []
commands:
commands:
start:
type: ShellScript
bash: ./start.sh
powershell: ./start.ps1
update:
type: ShellScript
bash: ./update.sh
powershell: ./update.ps1
watch:
type: ShellScript
bash: ./watch.sh
powershell: ./watch.ps1
update: hasura-ndc-go update
cliPlugin:
name: ndc-go
version: {{.Version}}
dockerComposeWatch:
# Rebuild the container if root files or dependencies are changed
- path: go.mod
Expand Down
Loading

0 comments on commit ebee0c0

Please sign in to comment.