Skip to content

Commit

Permalink
initial scaffolding for directive validation and git-based directives
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Rancourt <[email protected]>
  • Loading branch information
krancour committed Aug 30, 2024
1 parent 367ad99 commit 3ffeaa8
Show file tree
Hide file tree
Showing 17 changed files with 1,010 additions and 9 deletions.
23 changes: 14 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ build-cli-with-ui: build-ui build-cli
################################################################################

.PHONY: codegen
codegen: codegen-proto codegen-controller codegen-ui codegen-docs
codegen: codegen-proto codegen-controller codegen-directive-configs codegen-ui codegen-docs

.PHONY: codegen-proto
codegen-proto: install-protoc install-go-to-protobuf install-protoc-gen-gogo install-goimports install-buf
./hack/codegen/proto.sh

.PHONY: codegen-controller
codegen-controller: install-controller-gen
Expand All @@ -166,20 +170,21 @@ codegen-controller: install-controller-gen
object:headerFile=hack/boilerplate.go.txt \
paths=./...

.PHONY: codegen-docs
codegen-docs:
npm install -g @bitnami/readme-generator-for-helm
bash hack/helm-docs/helm-docs.sh

.PHONY: codegen-proto
codegen-proto: install-protoc install-go-to-protobuf install-protoc-gen-gogo install-goimports install-buf
./hack/codegen/proto.sh
.PHONY: codegen-directive-configs
codegen-directive-configs:
npm install -g quicktype
./hack/codegen/directive-configs.sh

.PHONY: codegen-ui
codegen-ui:
pnpm --dir=ui install --dev
pnpm --dir=ui run generate:schema

.PHONY: codegen-docs
codegen-docs:
npm install -g @bitnami/readme-generator-for-helm
bash hack/helm-docs/helm-docs.sh

################################################################################
# Hack: Targets to help you hack #
# #
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
github.com/technosophos/moniker v0.0.0-20210218184952-3ea787d3943b
github.com/xeipuuv/gojsonschema v1.2.0
go.uber.org/ratelimit v0.3.1
golang.org/x/crypto v0.26.0
golang.org/x/net v0.28.0
Expand Down Expand Up @@ -77,6 +78,8 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
go.opencensus.io v0.24.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240730163845-b1a4ccb954bf // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,12 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xanzy/go-gitlab v0.107.0 h1:P2CT9Uy9yN9lJo3FLxpMZ4xj6uWcpnigXsjvqJ6nd2Y=
github.com/xanzy/go-gitlab v0.107.0/go.mod h1:wKNKh3GkYDMOsGmnfuX+ITCmDuSDWFO0G+C4AygL9RY=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
26 changes: 26 additions & 0 deletions hack/codegen/directive-configs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

set -euxo pipefail

out_file=internal/directives/zz_config_types.go
generated_code_warning="// Code generated by quicktype. DO NOT EDIT.\n\n"

rm -rf ${out_file}

quicktype \
--src-lang schema --alphabetize-properties \
--lang go --just-types-and-package --package directives --omit-empty \
-o internal/directives/zz_config_types.go \
internal/directives/schemas/*.json

printf "${generated_code_warning}$(cat ${out_file})" > ${out_file}

# Pointers to bools and strings don't make a lot of sense in most cases.
#
# Note that -i works on Linux, but not on macOS. -i '' works on macOS, but not
# on Linux. So we use -i.bak, which works on both.
sed -i.bak 's/\*bool/bool/g' ${out_file}
sed -i.bak 's/\*string/string/g' ${out_file}
rm ${out_file}.bak

gofmt -w ${out_file}
52 changes: 52 additions & 0 deletions internal/directives/git_clone_directive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package directives

import (
"context"
"fmt"

"github.com/xeipuuv/gojsonschema"
)

func init() {
// Register the git-clone directive with the builtins registry.
builtins.RegisterDirective(newGitCloneDirective())
}

// gitCloneDirective is a directive that clones one or more refs from a remote
// Git repository to one or more working directories.
type gitCloneDirective struct {
schemaLoader gojsonschema.JSONLoader
}

// newGitCloneDirective creates a new git-clone directive.
func newGitCloneDirective() Directive {
return &gitCloneDirective{
schemaLoader: getConfigSchemaLoader("git-clone"),
}
}

// Name implements the Directive interface.
func (g *gitCloneDirective) Name() string {
return "git-clone"
}

// Run implements the Directive interface.
func (g *gitCloneDirective) Run(
_ context.Context,
stepCtx *StepContext,
) (Result, error) {
// Validate the configuration against the JSON Schema
if err := validate(
g.schemaLoader,
gojsonschema.NewGoLoader(stepCtx.Config),
"git-clone",
); err != nil {
return ResultFailure, err
}
if _, err := configToStruct[GitCloneConfig](stepCtx.Config); err != nil {
return ResultFailure,
fmt.Errorf("could not convert config into git-clone config: %w", err)
}
// TODO: Add implementation here
return ResultSuccess, nil
}
Loading

0 comments on commit 3ffeaa8

Please sign in to comment.