Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 1 | package image_repositories |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | |
| 6 | "github.com/go-git/go-billy/v5" |
| 7 | "github.com/goccy/go-yaml" |
| 8 | ) |
| 9 | |
| 10 | type PreCommitConfig struct { |
| 11 | Repositories []PreCommitRepository `yaml:"repos"` |
| 12 | } |
| 13 | |
| 14 | type PreCommitRepository struct { |
| 15 | Repository string `yaml:"repo"` |
| 16 | Revision string `yaml:"rev"` |
| 17 | Hooks []PreCommitHook `yaml:"hooks"` |
| 18 | } |
| 19 | |
| 20 | type PreCommitHook struct { |
| 21 | ID string `yaml:"id"` |
| 22 | Stages []string `yaml:"stages"` |
| 23 | } |
| 24 | |
| 25 | func 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 | |
| 42 | func (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 | |
| 52 | func (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 | } |