Don't choke on literal `%` output from `--format` use

In general, avoid using Printf without a static format string.
This commit is contained in:
Mislav Marohnić 2018-12-20 17:55:46 +01:00
Родитель 3e6fcc1f8a
Коммит f8feb335bc
5 изменённых файлов: 35 добавлений и 6 удалений

Просмотреть файл

@ -298,7 +298,7 @@ func listIssues(cmd *Command, args *Args) {
colorize := ui.IsTerminal(os.Stdout) colorize := ui.IsTerminal(os.Stdout)
for _, issue := range issues { for _, issue := range issues {
ui.Printf(formatIssue(issue, flagIssueFormat, colorize)) ui.Print(formatIssue(issue, flagIssueFormat, colorize))
} }
} }
@ -455,7 +455,7 @@ func showIssue(cmd *Command, args *Args) {
colorize := ui.IsTerminal(os.Stdout) colorize := ui.IsTerminal(os.Stdout)
if flagShowIssueFormat != "" { if flagShowIssueFormat != "" {
ui.Printf(formatIssue(*issue, flagShowIssueFormat, colorize)) ui.Print(formatIssue(*issue, flagShowIssueFormat, colorize))
return return
} }
@ -585,7 +585,7 @@ func listLabels(cmd *Command, args *Args) {
utils.Check(err) utils.Check(err)
for _, label := range labels { for _, label := range labels {
ui.Printf(formatLabel(label, flagLabelsColorize)) ui.Print(formatLabel(label, flagLabelsColorize))
} }
} }

Просмотреть файл

@ -220,7 +220,7 @@ func listPulls(cmd *Command, args *Args) {
colorize := ui.IsTerminal(os.Stdout) colorize := ui.IsTerminal(os.Stdout)
for _, pr := range pulls { for _, pr := range pulls {
ui.Printf(formatPullRequest(pr, flagPullRequestFormat, colorize)) ui.Print(formatPullRequest(pr, flagPullRequestFormat, colorize))
} }
} }

Просмотреть файл

@ -250,7 +250,7 @@ func listReleases(cmd *Command, args *Args) {
colorize := ui.IsTerminal(os.Stdout) colorize := ui.IsTerminal(os.Stdout)
for _, release := range releases { for _, release := range releases {
ui.Printf(formatRelease(release, flagReleaseFormat, colorize)) ui.Print(formatRelease(release, flagReleaseFormat, colorize))
} }
} }
@ -338,7 +338,7 @@ func showRelease(cmd *Command, args *Args) {
colorize := ui.IsTerminal(os.Stdout) colorize := ui.IsTerminal(os.Stdout)
if flagShowReleaseFormat != "" { if flagShowReleaseFormat != "" {
ui.Printf(formatRelease(*release, flagShowReleaseFormat, colorize)) ui.Print(formatRelease(*release, flagShowReleaseFormat, colorize))
return return
} }

Просмотреть файл

@ -614,6 +614,26 @@ Feature: hub issue
I want this feature\n I want this feature\n
""" """
Scenario: Format with literal % characters
Given the GitHub API server:
"""
get('/repos/github/hub/issues/102') {
json \
:number => 102,
:state => "open",
:title => "Feature request % hub",
:user => { :login => "alexfornuto" }
}
get('/repos/github/hub/issues/102/comments') {
json []
}
"""
When I successfully run `hub issue show 102 --format='%t%%t%%n%n'`
Then the output should contain exactly:
"""
Feature request % hub%t%n\n
"""
Scenario: Did not supply an issue number Scenario: Did not supply an issue number
When I run `hub issue show` When I run `hub issue show`
Then the exit status should be 1 Then the exit status should be 1

Просмотреть файл

@ -10,6 +10,7 @@ import (
) )
type UI interface { type UI interface {
Print(a ...interface{}) (n int, err error)
Printf(format string, a ...interface{}) (n int, err error) Printf(format string, a ...interface{}) (n int, err error)
Println(a ...interface{}) (n int, err error) Println(a ...interface{}) (n int, err error)
Errorf(format string, a ...interface{}) (n int, err error) Errorf(format string, a ...interface{}) (n int, err error)
@ -22,6 +23,10 @@ var (
Default UI = Console{Stdout: Stdout, Stderr: Stderr} Default UI = Console{Stdout: Stdout, Stderr: Stderr}
) )
func Print(a ...interface{}) (n int, err error) {
return Default.Print(a...)
}
func Printf(format string, a ...interface{}) (n int, err error) { func Printf(format string, a ...interface{}) (n int, err error) {
return Default.Printf(format, a...) return Default.Printf(format, a...)
} }
@ -47,6 +52,10 @@ type Console struct {
Stderr io.Writer Stderr io.Writer
} }
func (c Console) Print(a ...interface{}) (n int, err error) {
return fmt.Fprint(c.Stdout, a...)
}
func (c Console) Printf(format string, a ...interface{}) (n int, err error) { func (c Console) Printf(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(c.Stdout, format, a...) return fmt.Fprintf(c.Stdout, format, a...)
} }