-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit-msg.go
61 lines (51 loc) · 1.38 KB
/
commit-msg.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
package cmd
import (
"fmt"
"os"
"text/template"
gitutils "github.com/bable5/git-utils/git"
"github.com/spf13/cobra"
)
type templateParams struct {
US string
}
// Stack overflow solution. https://stackoverflow.com/questions/32551811/read-file-as-template-execute-it-and-write-it-back
func applyTemplate(fileName string, p templateParams) error {
//open the template, if it exists
t, err := template.ParseFiles(fileName)
if err != nil {
return err
}
f, err := os.Create(fileName)
if err != nil {
return err
}
defer f.Close()
return t.Execute(f, p)
}
func newCommitMsgCmd() *cobra.Command {
var gitConfigSection string
commitMsgCmd := &cobra.Command{
Use: "commit-msg",
Short: "git commit-msg hook",
Long: `Treat the commit message as a template and template in the userstory, if set.`,
RunE: func(cmd *cobra.Command, args []string) error {
userstory, err := gitutils.GetFromConfig(gitConfigSection)
if err != nil {
if _, ok := err.(*gitutils.UserStoryNotFound); ok {
return nil
} else {
return err
}
}
if len(args) < 1 {
return fmt.Errorf("missing template file")
}
return applyTemplate(args[0], templateParams{
US: userstory,
})
},
}
commitMsgCmd.PersistentFlags().StringVarP(&gitConfigSection, "git-config-section", "", "bable5.userstory", "Name of the config entry to store the userstory in.")
return commitMsgCmd
}