-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
116 lines (97 loc) · 2.75 KB
/
add.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
author string
merge string
message string
platform string
entry ChangelogEntry
user bool
)
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "Adds a new changelog entry file",
Long: `Adds a changelog entry file requiring description and author. Example usage:
add -a="John Smith" -d="full description text here" -m="123"`,
Run: func(cmd *cobra.Command, args []string) {
if len(author) == 0 && !user {
fmt.Println("Either author or user is required see --help for usage")
return
} else if len(message) == 0 {
fmt.Println("Description is required see --help for usage")
return
}
authorName := author
if user {
authorName, _ = getGitUsername()
}
entry.Author = authorName
entry.Platform = strings.ToLower(platform)
entry.Message = message
entry.Merge = merge
entryJSON, err := json.Marshal(entry)
if err != nil {
fmt.Println(err)
return
}
if _, err := os.Stat(changelogEntriesPath); os.IsNotExist(err) {
pathErr := os.MkdirAll(changelogEntriesPath, 0777)
if pathErr != nil {
fmt.Println(err)
return
}
}
filename := strings.Replace(strings.Fields(time.Now().String())[0], "-", "", -1)
filename += "-" + sanitizeDescription(entry.Message) + ".json"
err = ioutil.WriteFile(changelogEntriesPath+filename, entryJSON, 0644)
if err != nil {
fmt.Println(err)
}
},
}
func init() {
RootCmd.AddCommand(addCmd)
addCmd.Flags().StringVarP(&author, "author", "a", "", "Required: name of the user submitting the message")
addCmd.Flags().StringVarP(&message, "description", "d", "", "Required: description to summarize changes")
addCmd.Flags().StringVarP(&merge, "merge", "m", "", "Merge request id")
addCmd.Flags().StringVarP(&platform, "platform", "p", "", "Platform affected by changes")
addCmd.Flags().BoolVarP(&user, "user", "u", false, "Use instead of author to pull the git username will override author")
}
func sanitizeDescription(msg string) string {
if msg == "" {
return msg
}
result := msg
invalidChars := []string{"*", "`", "_", "~", "]", "[", "!", "#", "(", ")"}
for _, c := range invalidChars {
result = strings.Replace(result, c, "", -1)
}
result = strings.Replace(result, " ", "-", -1)
return result
}
func getGitUsername() (string, error) {
gitCmd := exec.Command("git", "config", "user.name")
b := &bytes.Buffer{}
gitCmd.Stdout = b
err := gitCmd.Run()
if err != nil {
return "", errors.Wrap(err, "cannot get git user name")
}
output := b.String()
if len(output) == 0 {
return "", errors.New("git username had no output")
}
return strings.TrimSpace(output), nil
}