-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add pr template command Will be used for on-demand pr gen * switch to liquid templating to remain consistent w/ api
- Loading branch information
1 parent
3861ee3
commit 8d53ef3
Showing
8 changed files
with
294 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package plural | ||
|
||
import ( | ||
"github.com/pluralsh/plural-cli/pkg/pr" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func prCommands() []cli.Command { | ||
return []cli.Command{ | ||
{ | ||
Name: "template", | ||
Usage: "applies a pr template resource in the local source tree", | ||
Action: handlePrTemplate, | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "file", | ||
Usage: "the file the template was placed in", | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func handlePrTemplate(c *cli.Context) error { | ||
template, err := pr.Build(c.String("file")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return pr.Apply(template) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package pr | ||
|
||
func Apply(template *PrTemplate) error { | ||
if err := applyUpdates(template.Spec.Updates, template.Context); err != nil { | ||
return err | ||
} | ||
|
||
return applyCreates(template.Spec.Creates, template.Context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package pr | ||
|
||
func applyCreates(creates *CreateSpec, ctx map[string]interface{}) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package pr | ||
|
||
import ( | ||
"os" | ||
|
||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type PrTemplate struct { | ||
ApiVersion string `json:"apiVersion"` | ||
Kind string `json:"kind"` | ||
Metadata map[string]interface{} `json:"metadata"` | ||
Context map[string]interface{} `json:"context"` | ||
Spec PrTemplateSpec `json:"spec"` | ||
} | ||
|
||
type PrTemplateSpec struct { | ||
Updates *UpdateSpec `json:"updates"` | ||
Creates *CreateSpec `json:"creates"` | ||
} | ||
|
||
type UpdateSpec struct { | ||
Regexes []string `json:"regexes"` | ||
Files []string `json:"files"` | ||
ReplaceTemplate string `json:"replace_template"` | ||
Yq string `json:"yq"` | ||
MatchStrategy string `json:"match_strategy"` | ||
} | ||
|
||
type CreateSpec struct { | ||
} | ||
|
||
func Build(path string) (*PrTemplate, error) { | ||
pr := &PrTemplate{} | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
return pr, err | ||
} | ||
|
||
if err := yaml.Unmarshal(data, pr); err != nil { | ||
return pr, err | ||
} | ||
|
||
return pr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package pr | ||
|
||
import ( | ||
"io/fs" | ||
"path/filepath" | ||
"regexp" | ||
) | ||
|
||
func applyUpdates(updates *UpdateSpec, ctx map[string]interface{}) error { | ||
replacement, err := templateReplacement([]byte(updates.ReplaceTemplate), ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return filepath.Walk(".", func(path string, info fs.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
ok, err := filenameMatches(path, updates.Files) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if ok { | ||
return updateFile(path, updates, replacement) | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
func updateFile(path string, updates *UpdateSpec, replacement []byte) error { | ||
switch updates.MatchStrategy { | ||
case "any": | ||
return anyUpdateFile(path, updates, replacement) | ||
case "all": | ||
return allUpdateFile(path, updates) | ||
case "recursive": | ||
return recursiveUpdateFile(path, updates, replacement) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func anyUpdateFile(path string, updates *UpdateSpec, replacement []byte) error { | ||
return replaceInPlace(path, func(data []byte) ([]byte, error) { | ||
for _, reg := range updates.Regexes { | ||
r, err := regexp.Compile(reg) | ||
if err != nil { | ||
return data, err | ||
} | ||
data = r.ReplaceAll(data, replacement) | ||
} | ||
return data, nil | ||
}) | ||
} | ||
|
||
func allUpdateFile(path string, updates *UpdateSpec) error { | ||
return nil | ||
} | ||
|
||
func recursiveUpdateFile(path string, updates *UpdateSpec, replacement []byte) error { | ||
return replaceInPlace(path, func(data []byte) ([]byte, error) { | ||
return recursiveReplace(data, updates.Regexes, replacement) | ||
}) | ||
} | ||
|
||
func recursiveReplace(data []byte, regexes []string, replacement []byte) ([]byte, error) { | ||
if len(regexes) == 0 { | ||
return []byte(replacement), nil | ||
} | ||
|
||
r, err := regexp.Compile(regexes[0]) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
res := r.ReplaceAllFunc(data, func(d []byte) []byte { | ||
res, err := recursiveReplace(d, regexes[1:], replacement) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return res | ||
}) | ||
|
||
return res, nil | ||
} | ||
|
||
func filenameMatches(path string, files []string) (bool, error) { | ||
for _, f := range files { | ||
r, err := regexp.Compile(f) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if r.MatchString(path) { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package pr | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/osteele/liquid" | ||
) | ||
|
||
var ( | ||
liquidEngine = liquid.NewEngine() | ||
) | ||
|
||
func templateReplacement(data []byte, ctx map[string]interface{}) ([]byte, error) { | ||
bindings := map[string]interface{}{ | ||
"context": ctx, | ||
} | ||
return liquidEngine.ParseAndRender(data, bindings) | ||
} | ||
|
||
func replaceInPlace(path string, rep func(data []byte) ([]byte, error)) error { | ||
info, err := os.Stat(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resData, err := rep(data) | ||
if err != nil { | ||
return err | ||
} | ||
return os.WriteFile(path, resData, info.Mode()) | ||
} |