From e9bb462f7f73584759ebe3acd6680d6b572be310 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Sat, 27 Feb 2021 17:31:18 +0100 Subject: [PATCH 1/3] remove endpoint.go in the package project, it will be generated --- internal/api/impl/v1/project/endpoint.go | 28 ------------------------ 1 file changed, 28 deletions(-) delete mode 100644 internal/api/impl/v1/project/endpoint.go diff --git a/internal/api/impl/v1/project/endpoint.go b/internal/api/impl/v1/project/endpoint.go deleted file mode 100644 index 3140905e97..0000000000 --- a/internal/api/impl/v1/project/endpoint.go +++ /dev/null @@ -1,28 +0,0 @@ -package project - -import ( - "github.com/labstack/echo/v4" - "github.com/perses/perses/internal/api/interface/v1/project" - "github.com/perses/perses/internal/api/shared" - v1 "github.com/perses/perses/pkg/model/api/v1" -) - -type Endpoint struct { - toolbox shared.Toolbox -} - -func NewEndpoint(service project.Service) *Endpoint { - return &Endpoint{ - toolbox: shared.NewToolBox(service), - } -} - -func (e *Endpoint) RegisterRoutes(g *echo.Group) { - projectGroup := g.Group("/projects") - projectGroup.POST("", e.Create) -} - -func (e *Endpoint) Create(ctx echo.Context) error { - entity := &v1.Project{} - return e.toolbox.Create(ctx, entity) -} From 82e471d4771e23833c905be86914eb31122d3dfb Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Sat, 27 Feb 2021 17:55:28 +0100 Subject: [PATCH 2/3] implement a way to generate the endpoints --- .gitignore | 2 + Makefile | 6 ++- internal/api/doc.go | 4 ++ internal/api/generate.go | 93 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 internal/api/doc.go create mode 100644 internal/api/generate.go diff --git a/.gitignore b/.gitignore index c875fbdc04..40ed033806 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea/ bin/ + +internal/api/impl/v1/project/endpoint.go diff --git a/Makefile b/Makefile index fa6afd368b..c52dcd793b 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,10 @@ test: $(GO) test -count=1 -v $(pkgs) .PHONY: build -build: +build: generate @echo ">> build the perses api" CGO_ENABLED=0 GOARCH=${GOARCH} $(GO) build -a -installsuffix cgo ${LDFLAGS} -o ./bin/perses ./cmd/perses + +.PHONY: generate +generate: + $(GO) generate ./internal/api diff --git a/internal/api/doc.go b/internal/api/doc.go new file mode 100644 index 0000000000..650491eacf --- /dev/null +++ b/internal/api/doc.go @@ -0,0 +1,4 @@ +package api + +// this file is just there to run the command generate +//go:generate go run generate.go -package=project -plural=projects -kind=Project diff --git a/internal/api/generate.go b/internal/api/generate.go new file mode 100644 index 0000000000..ed48109701 --- /dev/null +++ b/internal/api/generate.go @@ -0,0 +1,93 @@ +// +build ignore + +package main + +import ( + "bytes" + "flag" + "fmt" + "go/format" + "io/ioutil" + "log" + "os" + "text/template" +) + +const tpl = `{{- $endpoint := . -}} +{{- $package := $endpoint.PackageName -}} +{{- $plural := $endpoint.Plural -}} +{{- $kind := $endpoint.Kind -}} +// Code generated. DO NOT EDIT +package {{ $package }} + +import ( + "github.com/labstack/echo/v4" + "github.com/perses/perses/internal/api/interface/v1/{{ $package }}" + "github.com/perses/perses/internal/api/shared" + v1 "github.com/perses/perses/pkg/model/api/v1" +) + +type Endpoint struct { + toolbox shared.Toolbox +} + +func NewEndpoint(service {{ $package }}.Service) *Endpoint { + return &Endpoint{ + toolbox: shared.NewToolBox(service), + } +} + +func (e *Endpoint) RegisterRoutes(g *echo.Group) { + projectGroup := g.Group("/{{ $plural }}") + projectGroup.POST("", e.Create) +} + +func (e *Endpoint) Create(ctx echo.Context) error { + entity := &v1.{{ $kind }}{} + return e.toolbox.Create(ctx, entity) +} +` + +var endpointTemplate = template.Must( + template.New("").Parse(tpl)) + +// endpoint is the struct that will be used to generate the template +type endpoint struct { + PackageName string + Plural string + Kind string +} + +func main() { + pkg := flag.String("package", "", "the name of the package that needs to be generated. It should match the name of the resource you would like to expose through http") + plural := flag.String("plural", "", "the plural of the resource name") + kind := flag.String("kind", "", "the name of the resource with the appropriate cases") + flag.Parse() + + if len(*pkg) == 0 || len(*plural) == 0 || len(*kind) == 0 { + log.Fatal("unable to generate endpoint, missing parameter") + } + + folder := fmt.Sprintf("./impl/v1/%s", *pkg) + if _, err := os.Stat(folder); os.IsNotExist(err) { + os.Mkdir(folder, 0755) + } + endpointFile := fmt.Sprintf("%s/endpoint.go", folder) + ept := endpoint{ + PackageName: *pkg, + Plural: *plural, + Kind: *kind, + } + builder := &bytes.Buffer{} + if err := endpointTemplate.Execute(builder, ept); err != nil { + log.Fatal("Error while executing the template:", err) + } + data, err := format.Source(builder.Bytes()) + if err != nil { + log.Fatal("Error while formatting generated code:", err) + } + if err := ioutil.WriteFile(endpointFile, data, 0644); err != nil { + log.Fatal("Error writing endpoint file:", err) + } + +} From f57a42c19a747a9d1de9c84a03d65c857261d454 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Sat, 27 Feb 2021 18:07:04 +0100 Subject: [PATCH 3/3] modify github actions and makefile to generate code when needed --- .github/workflows/golangci-lint.yml | 5 ++++- Makefile | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 091dc7911e..c0fe8cca3c 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -9,7 +9,10 @@ jobs: name: lint runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - name: checkout + uses: actions/checkout@v2 + - name: generate files + run: make generate - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: diff --git a/Makefile b/Makefile index c52dcd793b..62919f1716 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ fmt: @echo ">> format code" $(GO) fmt $(pkgs) -test: +test: generate @echo ">> running all tests" $(GO) test -count=1 -v $(pkgs)