-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |