From f84e77fd84d86799d9f5adf39897ee915cbba80a Mon Sep 17 00:00:00 2001 From: Cory Bennett Date: Tue, 15 Sep 2015 19:31:38 -0700 Subject: [PATCH] * use forked yaml.v2 so as to not lose line terminations present in jira fields * adding a |~ literal yaml syntax to just chomp a single newline (again to preserve existing formatting in jira fields) * for indent/comment allow for unicode line termination characters that yaml will use for parsing --- jira/cli/cli.go | 8 +++----- jira/cli/templates.go | 10 +++++----- jira/cli/util.go | 17 ++++++++++++++--- jira/main.go | 2 +- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/jira/cli/cli.go b/jira/cli/cli.go index fe9e769..564950e 100644 --- a/jira/cli/cli.go +++ b/jira/cli/cli.go @@ -6,7 +6,7 @@ import ( "fmt" "github.com/kballard/go-shellquote" "github.com/op/go-logging" - "gopkg.in/yaml.v2" + "gopkg.in/coryb/yaml.v2" "io/ioutil" "net/http" "net/http/cookiejar" @@ -250,10 +250,7 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m } } - editing := true - if val, ok := c.opts["edit"].(bool); ok && !val { - editing = false - } + editing := c.getOptBool("edit", true) tmpFileNameOrig := fmt.Sprintf("%s.orig", tmpFileName) copyFile(tmpFileName, tmpFileNameOrig) @@ -275,6 +272,7 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m } return err } + diff := exec.Command("diff", "-q", tmpFileNameOrig, tmpFileName) // if err == nil then diff found no changes if err := diff.Run(); err == nil { diff --git a/jira/cli/templates.go b/jira/cli/templates.go index 81dee12..5b19d2d 100644 --- a/jira/cli/templates.go +++ b/jira/cli/templates.go @@ -54,7 +54,7 @@ const default_edit_template = `# issue: {{ .key }} update: comment: - add: - body: | + body: |~ {{ or .overrides.comment "" | indent 10 }} fields: summary: {{ or .overrides.summary .fields.summary }} @@ -71,7 +71,7 @@ fields: - name: {{ .overrides.watcher}}{{end}} priority: # Values: {{ range .meta.fields.priority.allowedValues }}{{.name}}, {{end}} name: {{ or .overrides.priority .fields.priority.name }} - description: | + description: |~ {{ or .overrides.description (or .fields.description "") | indent 4 }} # comments: # {{ range .fields.comment.comments }} - | # {{.author.name}} at {{.created}} @@ -94,7 +94,7 @@ const default_create_template = `fields: name: {{ or .overrides.priority "unassigned" }} components: # Values: {{ range .meta.fields.components.allowedValues }}{{.name}}, {{end}}{{ range split "," (or .overrides.components "")}} - name: {{ . }}{{end}} - description: | + description: |~ {{ or .overrides.description "" | indent 4 }} assignee: name: {{ or .overrides.assignee "" }} @@ -106,14 +106,14 @@ const default_create_template = `fields: - name: ` -const default_comment_template = `body: | +const default_comment_template = `body: |~ {{ or .overrides.comment "" | indent 2 }} ` const default_transition_template = `update: comment: - add: - body: | + body: |~ {{ or .overrides.comment "" | indent 10 }} fields:{{if .meta.fields.assignee}} assignee: diff --git a/jira/cli/util.go b/jira/cli/util.go index 2a7d5bc..86a53de 100644 --- a/jira/cli/util.go +++ b/jira/cli/util.go @@ -125,15 +125,26 @@ func runTemplate(templateContent string, data interface{}, out io.Writer) error } }, "indent": func(spaces int, content string) string { - indent := make([]byte, spaces+1, spaces+1) + indent := make([]rune, spaces+1, spaces+1) indent[0] = '\n' for i := 1; i < spaces+1; i += 1 { indent[i] = ' ' } - return strings.Replace(content, "\n", string(indent), -1) + + lineSeps := []rune{'\n', '\u0085', '\u2028', '\u2029'} + for _, sep := range lineSeps { + indent[0] = sep + content = strings.Replace(content, string(sep), string(indent), -1) + } + return content + }, "comment": func(content string) string { - return strings.Replace(content, "\n", "\n# ", -1) + lineSeps := []rune{'\n', '\u0085', '\u2028', '\u2029'} + for _, sep := range lineSeps { + content = strings.Replace(content, string(sep), string([]rune{sep, '#', ' '}), -1) + } + return content }, "color": func(color string) string { return ansi.ColorCode(color) diff --git a/jira/main.go b/jira/main.go index 35b3611..0d10a80 100644 --- a/jira/main.go +++ b/jira/main.go @@ -6,7 +6,7 @@ import ( "github.com/Netflix-Skunkworks/go-jira/jira/cli" "github.com/coryb/optigo" "github.com/op/go-logging" - "gopkg.in/yaml.v2" + "gopkg.in/coryb/yaml.v2" "io/ioutil" "os" "os/exec"