Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create-mode PR templating #487

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading