Skip to content

Commit

Permalink
Create-mode PR templating (#487)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
michaeljguarino committed Aug 28, 2024
1 parent 937fb05 commit 394b40c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
6 changes: 1 addition & 5 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions cmd/plural/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
}
Expand All @@ -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)
}
21 changes: 21 additions & 0 deletions pkg/pr/creates.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions pkg/pr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 8 additions & 4 deletions pkg/pr/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}

0 comments on commit 394b40c

Please sign in to comment.