Debian Codegraph @

cmd/init.go

raw
1 package cmd
2
3 import (
4 "errors"
5 "os"
6 "path/filepath"
7
8 "github.com/spf13/cobra"
9
10 "github.com/miniscruff/changie/core"
11 )
12
13 var errConfigExists = errors.New("changie config already exists")
14
15 type Init struct {
16 *cobra.Command
17
18 // CLI args
19 ChangesDir string
20 ChangelogPath string
21 Force bool
22 }
23
24 func NewInit() *Init {
25 i := &Init{}
26
27 cmd := &cobra.Command{
28 Use: "init",
29 Short: "Initialize a new changie skeleton",
30 Long: `Initialize a few changie specifics.
31
32 * Folder to place all change files
33 * Subfolder to place all unreleased changes
34 * Header file to place on top of the changelog
35 * Output file when generating a changelog
36 * Unreleased folder includes a .gitkeep file
37
38 Values will also be saved in a changie config at .changie.yaml.
39 Default values follow keep a changelog and semver specs but are customizable.`,
40 Args: cobra.NoArgs,
41 RunE: i.Run,
42 }
43 cmd.Flags().StringVarP(&i.ChangesDir, "dir", "d", ".changes", "directory for all changes")
44 cmd.Flags().StringVarP(
45 &i.ChangelogPath,
46 "output",
47 "o",
48 "CHANGELOG.md",
49 "file path to output our changelog",
50 )
51 cmd.Flags().BoolVarP(&i.Force, "force", "f", false, "force initialize even if config already exist")
52
53 i.Command = cmd
54
55 return i
56 }
57
58 func (i *Init) Run(cmd *cobra.Command, args []string) error {
59 config := core.Config{
60 ChangesDir: i.ChangesDir,
61 UnreleasedDir: "unreleased",
62 HeaderPath: "header.tpl.md",
63 ChangelogPath: i.ChangelogPath,
64 VersionExt: "md",
65 VersionFormat: "## {{.Version}} - {{.Time.Format \"2006-01-02\"}}",
66 KindFormat: "### {{.Kind}}",
67 ChangeFormat: "* {{.Body}}",
68 Kinds: []core.KindConfig{
69 {
70 Label: "Added",
71 AutoLevel: core.MinorLevel,
72 },
73 {
74 Label: "Changed",
75 AutoLevel: core.MajorLevel,
76 },
77 {
78 Label: "Deprecated",
79 AutoLevel: core.MinorLevel,
80 },
81 {
82 Label: "Removed",
83 AutoLevel: core.MajorLevel,
84 },
85 {
86 Label: "Fixed",
87 AutoLevel: core.PatchLevel,
88 },
89 {
90 Label: "Security",
91 AutoLevel: core.PatchLevel,
92 },
93 },
94 Newlines: core.NewlinesConfig{
95 AfterChangelogHeader: 1,
96 BeforeChangelogVersion: 1,
97 EndOfVersion: 1,
98 },
99 EnvPrefix: "CHANGIE_",
100 }
101
102 headerPath := filepath.Join(config.ChangesDir, config.HeaderPath)
103 unreleasedPath := filepath.Join(config.ChangesDir, config.UnreleasedDir)
104 keepPath := filepath.Join(unreleasedPath, ".gitkeep")
105
106 if !i.Force {
107 exists, existErr := config.Exists()
108 if exists || existErr != nil {
109 return errConfigExists
110 }
111 }
112
113 err := config.Save()
114 if err != nil {
115 return err
116 }
117
118 err = os.MkdirAll(unreleasedPath, core.CreateDirMode)
119 if err != nil {
120 return err
121 }
122
123 err = os.WriteFile(keepPath, []byte{}, core.CreateFileMode)
124 if err != nil {
125 return err
126 }
127
128 err = os.WriteFile(headerPath, []byte(defaultHeader), core.CreateFileMode)
129 if err != nil {
130 return err
131 }
132
133 err = os.WriteFile(config.ChangelogPath, []byte(defaultChangelog), core.CreateFileMode)
134 if err != nil {
135 return err
136 }
137
138 return nil
139 }