-
Notifications
You must be signed in to change notification settings - Fork 326
/
unexportTemplates.go
62 lines (57 loc) · 1.64 KB
/
unexportTemplates.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
package jiracmd
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/coryb/figtree"
"github.com/coryb/oreo"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiracli"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
func CmdUnexportTemplatesRegistry() *jiracli.CommandRegistryEntry {
opts := ExportTemplatesOptions{}
return &jiracli.CommandRegistryEntry{
"Remove unmodified exported templates",
func(fig *figtree.FigTree, cmd *kingpin.CmdClause) error {
jiracli.LoadConfigs(cmd, fig, &opts)
return CmdExportTemplatesUsage(cmd, &opts)
},
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
if opts.Dir != "" {
opts.Dir = fmt.Sprintf("%s/.jira.d/templates", jiracli.Homedir())
}
return CmdUnexportTemplates(globals, &opts)
},
}
}
// CmdUnexportTemplates will remove unmodified templates from export directory
func CmdUnexportTemplates(globals *jiracli.GlobalOptions, opts *ExportTemplatesOptions) error {
for name, template := range jiracli.AllTemplates {
if opts.Template != "" && opts.Template != name {
continue
}
templateFile := path.Join(opts.Dir, name)
if _, err := os.Stat(templateFile); err != nil {
log.Warning("Skipping %s, not found", templateFile)
continue
}
// open, read, compare
contents, err := ioutil.ReadFile(templateFile)
if err != nil {
return err
}
if bytes.Compare([]byte(template), contents) == 0 {
if !globals.Quiet.Value {
log.Notice("Removing %s, template identical to default", templateFile)
}
os.Remove(templateFile)
} else {
if !globals.Quiet.Value {
log.Notice("Skipping %s, found customizations to template", templateFile)
}
}
}
return nil
}