blob: 7c98576c158a77ad9f88e3ab3e9c30a463b1272a [file] [log] [blame]
Mohammed Naserdabb1dc2022-09-06 14:45:59 -04001package image_repositories
2
3import (
4 "io"
5
6 "github.com/go-git/go-billy/v5"
7 "github.com/goccy/go-yaml"
8)
9
10type PreCommitConfig struct {
11 Repositories []PreCommitRepository `yaml:"repos"`
12}
13
14type PreCommitRepository struct {
15 Repository string `yaml:"repo"`
16 Revision string `yaml:"rev"`
17 Hooks []PreCommitHook `yaml:"hooks"`
18}
19
20type PreCommitHook struct {
21 ID string `yaml:"id"`
22 Stages []string `yaml:"stages"`
23}
24
25func NewPreCommitConfig() *PreCommitConfig {
26 return &PreCommitConfig{
27 Repositories: []PreCommitRepository{
28 {
29 Repository: "https://github.com/compilerla/conventional-pre-commit",
30 Revision: "v2.0.0",
31 Hooks: []PreCommitHook{
32 {
33 ID: "conventional-pre-commit",
34 Stages: []string{"commit-msg"},
35 },
36 },
37 },
38 },
39 }
40}
41
42func (c *PreCommitConfig) Write(wr io.Writer) error {
43 bytes, err := yaml.Marshal(c)
44 if err != nil {
45 return err
46 }
47
48 _, err = wr.Write(bytes)
49 return err
50}
51
52func (c *PreCommitConfig) WriteFile(fs billy.Filesystem) error {
53 f, err := fs.Create(".pre-commit-config.yaml")
54 if err != nil {
55 return err
56 }
57 defer f.Close()
58
59 return c.Write(f)
60}