From 394b40c665ca1a9795494c5131d3aeef0179f553 Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Mon, 5 Feb 2024 16:32:01 -0500 Subject: [PATCH] Create-mode PR templating (#487) * Create-mode PR templating Adds the ability to create new files to a pr template, sourced either in-repo or externally * add more goreleaser release targets --- .goreleaser.yaml | 6 +----- cmd/plural/pr.go | 9 +++++++++ pkg/pr/creates.go | 21 +++++++++++++++++++++ pkg/pr/types.go | 8 ++++++++ pkg/pr/utils.go | 12 ++++++++---- 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index bb7ecf50e..9af794a4d 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -29,10 +29,6 @@ builds: - -X "github.com/pluralsh/plural-cli/cmd/plural.Commit={{.Commit}}" - -X "github.com/pluralsh/plural-cli/cmd/plural.Date={{.Date}}" - -X "github.com/pluralsh/plural-cli/pkg/scm.GitlabClientSecret={{.Env.GITLAB_CLIENT_SECRET}}" - tags: - - desktop - - production - - ui binary: plural # Do not embed UI into linux/arm64 and windows/arm64 binaries overrides: @@ -46,7 +42,7 @@ builds: tags: [ windows ] env: - CGO_ENABLED=0 - # Build CLI binary without embedded UI for linux. Required by our Console. + # Build CLI binary without embedded UI for linux. - id: plural-cli-console targets: - linux_amd64 diff --git a/cmd/plural/pr.go b/cmd/plural/pr.go index 0df768709..adf00a9e1 100644 --- a/cmd/plural/pr.go +++ b/cmd/plural/pr.go @@ -17,6 +17,11 @@ func prCommands() []cli.Command { Usage: "the file the template was placed in", Required: true, }, + cli.StringFlag{ + Name: "templates", + Usage: "a directory of external templates to use for creating new files", + Required: false, + }, }, }, } @@ -28,5 +33,9 @@ func handlePrTemplate(c *cli.Context) error { return err } + if template.Spec.Creates != nil { + template.Spec.Creates.ExternalDir = c.String("templates") + } + return pr.Apply(template) } diff --git a/pkg/pr/creates.go b/pkg/pr/creates.go index 6b14e4545..e1a87dd60 100644 --- a/pkg/pr/creates.go +++ b/pkg/pr/creates.go @@ -1,5 +1,26 @@ package pr +import ( + "path/filepath" +) + func applyCreates(creates *CreateSpec, ctx map[string]interface{}) error { + if creates == nil { + return nil + } + + for _, tpl := range creates.Templates { + source := tpl.Source + if tpl.External { + source = filepath.Join(creates.ExternalDir, source) + } + + if err := replaceTo(source, tpl.Destination, func(data []byte) ([]byte, error) { + return templateReplacement(data, ctx) + }); err != nil { + return err + } + } + return nil } diff --git a/pkg/pr/types.go b/pkg/pr/types.go index dcecdd8a1..71534cebd 100644 --- a/pkg/pr/types.go +++ b/pkg/pr/types.go @@ -28,6 +28,14 @@ type UpdateSpec struct { } type CreateSpec struct { + ExternalDir string + Templates []*CreateTemplate `json:"templates"` +} + +type CreateTemplate struct { + Source string `json:"source"` + Destination string `json:"destination"` + External bool `json:"external"` } func Build(path string) (*PrTemplate, error) { diff --git a/pkg/pr/utils.go b/pkg/pr/utils.go index 75d3c0e0b..4cbb4eceb 100644 --- a/pkg/pr/utils.go +++ b/pkg/pr/utils.go @@ -17,13 +17,13 @@ func templateReplacement(data []byte, ctx map[string]interface{}) ([]byte, error return liquidEngine.ParseAndRender(data, bindings) } -func replaceInPlace(path string, rep func(data []byte) ([]byte, error)) error { - info, err := os.Stat(path) +func replaceTo(from, to string, rep func(data []byte) ([]byte, error)) error { + info, err := os.Stat(from) if err != nil { return err } - data, err := os.ReadFile(path) + data, err := os.ReadFile(from) if err != nil { return err } @@ -32,5 +32,9 @@ func replaceInPlace(path string, rep func(data []byte) ([]byte, error)) error { if err != nil { return err } - return os.WriteFile(path, resData, info.Mode()) + return os.WriteFile(to, resData, info.Mode()) +} + +func replaceInPlace(path string, rep func(data []byte) ([]byte, error)) error { + return replaceTo(path, path, rep) }