Skip to content

Commit

Permalink
add export-templates command
Browse files Browse the repository at this point in the history
  • Loading branch information
coryb committed Aug 21, 2017
1 parent da39323 commit abaad56
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 36 deletions.
12 changes: 8 additions & 4 deletions cmd/jira/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -307,7 +314,6 @@ func main() {
// }

// jiraCommands := map[string]string{
// "export-templates": "export-templates",
// "browse": "browse",
// "req": "request",
// "request": "request",
Expand Down Expand Up @@ -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 := ""
Expand Down
32 changes: 0 additions & 32 deletions jiracli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
64 changes: 64 additions & 0 deletions jiracli/exportTemplates.go
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 53 additions & 0 deletions jiracli/unexportTemplates.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit abaad56

Please sign in to comment.