mirror of
https://github.com/Threnklyn/jira.git
synced 2026-05-18 20:23:28 +02:00
add export-templates command
This commit is contained in:
+8
-4
@@ -241,6 +241,14 @@ func main() {
|
||||
Command: "issuetypes",
|
||||
Entry: cli.CmdIssueTypesRegistry(),
|
||||
},
|
||||
jiracli.CommandRegistry{
|
||||
Command: "export-templates",
|
||||
Entry: cli.CmdExportTemplatesRegistry(),
|
||||
},
|
||||
jiracli.CommandRegistry{
|
||||
Command: "unexport-templates",
|
||||
Entry: cli.CmdUnexportTemplatesRegistry(),
|
||||
},
|
||||
}
|
||||
|
||||
cli.Register(app, registry)
|
||||
@@ -259,7 +267,6 @@ func main() {
|
||||
}
|
||||
|
||||
// Usage:
|
||||
// jira export-templates [-d DIR] [-t template]
|
||||
// jira (b|browse) ISSUE
|
||||
// jira request [-M METHOD] URI [DATA]
|
||||
// jira ISSUE
|
||||
@@ -307,7 +314,6 @@ func main() {
|
||||
// }
|
||||
|
||||
// jiraCommands := map[string]string{
|
||||
// "export-templates": "export-templates",
|
||||
// "browse": "browse",
|
||||
// "req": "request",
|
||||
// "request": "request",
|
||||
@@ -460,8 +466,6 @@ func main() {
|
||||
// requireArgs(1)
|
||||
// opts["browse"] = true
|
||||
// err = c.Browse(args[0])
|
||||
// case "export-templates":
|
||||
// err = c.CmdExportTemplates()
|
||||
// case "request":
|
||||
// requireArgs(1)
|
||||
// data := ""
|
||||
|
||||
@@ -25,10 +25,6 @@ type kingpinAppOrCommand interface {
|
||||
GetCommand(string) *kingpin.CmdClause
|
||||
}
|
||||
|
||||
// func NewCommand(app kingpinAppOrCommand, name string, entry *CommandRegistryEntry) *kingpin.CmdClause {
|
||||
// returnapp.Command(name, entry.Help)
|
||||
// }
|
||||
|
||||
func (jc *JiraCli) Register(app *kingpin.Application, reg []CommandRegistry) {
|
||||
for _, command := range reg {
|
||||
copy := command
|
||||
@@ -63,34 +59,6 @@ func (jc *JiraCli) Register(app *kingpin.Application, reg []CommandRegistry) {
|
||||
}
|
||||
}
|
||||
|
||||
// // CmdExportTemplates will export the default templates to the template directory.
|
||||
// func (c *Cli) CmdExportTemplates() error {
|
||||
// dir := c.opts["directory"].(string)
|
||||
// if err := mkdir(dir); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// for name, template := range allTemplates {
|
||||
// if wanted, ok := c.opts["template"]; ok && wanted != name {
|
||||
// continue
|
||||
// }
|
||||
// templateFile := fmt.Sprintf("%s/%s", dir, name)
|
||||
// if _, err := os.Stat(templateFile); err == nil {
|
||||
// log.Warning("Skipping %s, already exists", templateFile)
|
||||
// continue
|
||||
// }
|
||||
// fh, err := os.OpenFile(templateFile, os.O_WRONLY|os.O_CREATE, 0644)
|
||||
// if err != nil {
|
||||
// log.Errorf("Failed to open %s for writing: %s", templateFile, err)
|
||||
// return err
|
||||
// }
|
||||
// defer fh.Close()
|
||||
// log.Noticef("Creating %s", templateFile)
|
||||
// fh.Write([]byte(template))
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// // CmdRequest will use the given uri to make a request and potentially send provided content.
|
||||
// func (c *Cli) CmdRequest(uri, content string) (err error) {
|
||||
// log.Debugf("request called")
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package jiracli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
type ExportTemplatesOptions struct {
|
||||
Template string
|
||||
Dir string
|
||||
}
|
||||
|
||||
func (jc *JiraCli) CmdExportTemplatesRegistry() *CommandRegistryEntry {
|
||||
opts := ExportTemplatesOptions{
|
||||
Dir: fmt.Sprintf("%s/.jira.d/templates", homedir()),
|
||||
}
|
||||
|
||||
return &CommandRegistryEntry{
|
||||
"Export templates for customizations",
|
||||
func() error {
|
||||
return jc.CmdExportTemplates(&opts)
|
||||
},
|
||||
func(cmd *kingpin.CmdClause) error {
|
||||
return jc.CmdExportTemplatesUsage(cmd, &opts)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (jc *JiraCli) CmdExportTemplatesUsage(cmd *kingpin.CmdClause, opts *ExportTemplatesOptions) error {
|
||||
cmd.Flag("template", "Template to export").Short('t').StringVar(&opts.Template)
|
||||
cmd.Flag("dir", "directory to write tempates to").Short('d').StringVar(&opts.Dir)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CmdExportTemplates will export templates to directory
|
||||
func (jc *JiraCli) CmdExportTemplates(opts *ExportTemplatesOptions) error {
|
||||
if err := os.MkdirAll(opts.Dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for name, template := range 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, already exists", templateFile)
|
||||
continue
|
||||
}
|
||||
fh, err := os.OpenFile(templateFile, os.O_WRONLY|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to open %s for writing: %s", templateFile, err)
|
||||
return err
|
||||
}
|
||||
defer fh.Close()
|
||||
log.Noticef("Creating %s", templateFile)
|
||||
fh.Write([]byte(template))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package jiracli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
func (jc *JiraCli) CmdUnexportTemplatesRegistry() *CommandRegistryEntry {
|
||||
opts := ExportTemplatesOptions{
|
||||
Dir: fmt.Sprintf("%s/.jira.d/templates", homedir()),
|
||||
}
|
||||
|
||||
return &CommandRegistryEntry{
|
||||
"Remove unmodified exported templates",
|
||||
func() error {
|
||||
return jc.CmdUnexportTemplates(&opts)
|
||||
},
|
||||
func(cmd *kingpin.CmdClause) error {
|
||||
return jc.CmdExportTemplatesUsage(cmd, &opts)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// CmdUnexportTemplates will remove unmodified templates from export directory
|
||||
func (jc *JiraCli) CmdUnexportTemplates(opts *ExportTemplatesOptions) error {
|
||||
for name, template := range 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 {
|
||||
log.Warning("Removing %s, template identical to default", templateFile)
|
||||
os.Remove(templateFile)
|
||||
} else {
|
||||
log.Warning("Skipping %s, found customizations to template", templateFile)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user