/
init.go
executable file
·75 lines (56 loc) · 1.68 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cmd
import (
"html/template"
"os"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/nicwest/kacl/prompt"
)
type initCmdConfig struct {
ProjectURL string
InitialTag string
}
// V1Template is the default keep a change log v1.0.0 template
const V1Template string = `# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- This CHANGELOG file!
[Unreleased]: {{ .ProjectURL }}/compare/{{ .InitialTag }}...HEAD `
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize a CHANGELOG.md file",
Long: `Initialize will create a new CHANGLE.md file in the
http://keepachangelog.com/en/1.0.0/ format.`,
Run: func(cmd *cobra.Command, args []string) {
force, _ := cmd.LocalFlags().GetBool("force")
if !force {
if _, err := os.Stat("./CHANGELOG.md"); !os.IsNotExist(err) {
color.Red("CHANGELOG.md already exists!")
return
}
}
var cfg initCmdConfig
prompt.For("Project URL", &cfg.ProjectURL)
prompt.ForWithDefault("Initial commit", "0.0.1", &cfg.InitialTag)
t := template.Must(template.New("version").Parse(V1Template))
f, err := os.Create("./CHANGELOG.md")
if err != nil {
color.Red(err.Error())
return
}
err = t.Execute(f, cfg)
if err != nil {
color.Red(err.Error())
return
}
f.Close()
},
}
func init() {
RootCmd.AddCommand(initCmd)
initCmd.Flags().BoolP("force", "f", false, "Will force the creation of the CHANGELOG.md file even if it already exists")
}