1
0
mirror of https://github.com/golang/go synced 2024-11-21 23:24:41 -07:00

cmd/go: change Printer.Output -> Printer.Printf for consistency

Currently, the Printer interface has `Output`, which acts like Print
and `Errorf`, which acts like Printf. It's confusing that the
formatting style is tied to whether it's regular output or an error.

Fix this by replacing Output with Printf, so both use Printf-style
formatting.

Change-Id: I4c76f941e956f2599c5620b455bf41e21636b44e
Reviewed-on: https://go-review.googlesource.com/c/go/+/627795
Reviewed-by: Russ Cox <rsc@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Austin Clements 2024-11-13 19:50:44 -05:00
parent f9a95b1bdc
commit 44d4b69942
5 changed files with 21 additions and 21 deletions

View File

@ -17,15 +17,15 @@ import (
// A Printer reports output about a Package. // A Printer reports output about a Package.
type Printer interface { type Printer interface {
// Output reports output from building pkg. The arguments are of the form // Printf reports output from building pkg. The arguments are of the form
// expected by fmt.Print. // expected by [fmt.Printf].
// //
// pkg may be nil if this output is not associated with the build of a // pkg may be nil if this output is not associated with the build of a
// particular package. // particular package.
// //
// The caller is responsible for checking if printing output is appropriate, // The caller is responsible for checking if printing output is appropriate,
// for example by checking cfg.BuildN or cfg.BuildV. // for example by checking cfg.BuildN or cfg.BuildV.
Output(pkg *Package, args ...any) Printf(pkg *Package, format string, args ...any)
// Errorf prints output in the form of `log.Errorf` and reports that // Errorf prints output in the form of `log.Errorf` and reports that
// building pkg failed. // building pkg failed.
@ -68,8 +68,8 @@ type TextPrinter struct {
Writer io.Writer Writer io.Writer
} }
func (p *TextPrinter) Output(_ *Package, args ...any) { func (p *TextPrinter) Printf(_ *Package, format string, args ...any) {
fmt.Fprint(p.Writer, args...) fmt.Fprintf(p.Writer, format, args...)
} }
func (p *TextPrinter) Errorf(_ *Package, format string, args ...any) { func (p *TextPrinter) Errorf(_ *Package, format string, args ...any) {
@ -92,10 +92,10 @@ type jsonBuildEvent struct {
Output string `json:",omitempty"` // Non-empty if Action == “build-output” Output string `json:",omitempty"` // Non-empty if Action == “build-output”
} }
func (p *JSONPrinter) Output(pkg *Package, args ...any) { func (p *JSONPrinter) Printf(pkg *Package, format string, args ...any) {
ev := &jsonBuildEvent{ ev := &jsonBuildEvent{
Action: "build-output", Action: "build-output",
Output: fmt.Sprint(args...), Output: fmt.Sprintf(format, args...),
} }
if ev.Output == "" { if ev.Output == "" {
// There's no point in emitting a completely empty output event. // There's no point in emitting a completely empty output event.
@ -112,7 +112,7 @@ func (p *JSONPrinter) Errorf(pkg *Package, format string, args ...any) {
// For clarity, emit each line as a separate output event. // For clarity, emit each line as a separate output event.
for len(s) > 0 { for len(s) > 0 {
i := strings.IndexByte(s, '\n') i := strings.IndexByte(s, '\n')
p.Output(pkg, s[:i+1]) p.Printf(pkg, "%s", s[:i+1])
s = s[i+1:] s = s[i+1:]
} }
ev := &jsonBuildEvent{ ev := &jsonBuildEvent{

View File

@ -298,7 +298,7 @@ func NewBuilder(workDir string) *Builder {
b.backgroundSh = NewShell(b.WorkDir, nil) b.backgroundSh = NewShell(b.WorkDir, nil)
if printWorkDir { if printWorkDir {
b.BackgroundShell().Print("WORK=", b.WorkDir, "\n") b.BackgroundShell().Printf("WORK=%s\n", b.WorkDir)
} }
if err := CheckGOOSARCHPair(cfg.Goos, cfg.Goarch); err != nil { if err := CheckGOOSARCHPair(cfg.Goos, cfg.Goarch); err != nil {

View File

@ -618,7 +618,7 @@ func showStdout(b *Builder, c cache.Cache, a *Action, key string) error {
sh.ShowCmd("", "%s # internal", joinUnambiguously(str.StringList("cat", c.OutputFile(stdoutEntry.OutputID)))) sh.ShowCmd("", "%s # internal", joinUnambiguously(str.StringList("cat", c.OutputFile(stdoutEntry.OutputID))))
} }
if !cfg.BuildN { if !cfg.BuildN {
sh.Print(string(stdout)) sh.Printf("%s", stdout)
} }
} }
return nil return nil
@ -626,7 +626,7 @@ func showStdout(b *Builder, c cache.Cache, a *Action, key string) error {
// flushOutput flushes the output being queued in a. // flushOutput flushes the output being queued in a.
func (b *Builder) flushOutput(a *Action) { func (b *Builder) flushOutput(a *Action) {
b.Shell(a).Print(string(a.output)) b.Shell(a).Printf("%s", a.output)
a.output = nil a.output = nil
} }

View File

@ -520,11 +520,11 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
// different sections of the bootstrap script have to // different sections of the bootstrap script have to
// be merged, the banners give patch something // be merged, the banners give patch something
// to use to find its context. // to use to find its context.
sh.Print("\n#\n# " + p.ImportPath + "\n#\n\n") sh.Printf("\n#\n# %s\n#\n\n", p.ImportPath)
} }
if cfg.BuildV { if cfg.BuildV {
sh.Print(p.ImportPath + "\n") sh.Printf("%s\n", p.ImportPath)
} }
if p.Error != nil { if p.Error != nil {

View File

@ -71,16 +71,16 @@ func (sh *Shell) pkg() *load.Package {
return sh.action.Package return sh.action.Package
} }
// Print emits a to this Shell's output stream, formatting it like fmt.Print. // Printf emits a to this Shell's output stream, formatting it like fmt.Printf.
// It is safe to call concurrently. // It is safe to call concurrently.
func (sh *Shell) Print(a ...any) { func (sh *Shell) Printf(format string, a ...any) {
sh.printLock.Lock() sh.printLock.Lock()
defer sh.printLock.Unlock() defer sh.printLock.Unlock()
sh.printer.Output(sh.pkg(), a...) sh.printer.Printf(sh.pkg(), format, a...)
} }
func (sh *Shell) printLocked(a ...any) { func (sh *Shell) printfLocked(format string, a ...any) {
sh.printer.Output(sh.pkg(), a...) sh.printer.Printf(sh.pkg(), format, a...)
} }
// Errorf reports an error on sh's package and sets the process exit status to 1. // Errorf reports an error on sh's package and sets the process exit status to 1.
@ -371,7 +371,7 @@ func (sh *Shell) ShowCmd(dir string, format string, args ...any) {
if dir != "" && dir != "/" { if dir != "" && dir != "/" {
if dir != sh.scriptDir { if dir != sh.scriptDir {
// Show changing to dir and update the current directory. // Show changing to dir and update the current directory.
sh.printLocked(sh.fmtCmd("", "cd %s\n", dir)) sh.printfLocked("%s", sh.fmtCmd("", "cd %s\n", dir))
sh.scriptDir = dir sh.scriptDir = dir
} }
// Replace scriptDir is our working directory. Replace it // Replace scriptDir is our working directory. Replace it
@ -383,7 +383,7 @@ func (sh *Shell) ShowCmd(dir string, format string, args ...any) {
cmd = strings.ReplaceAll(" "+cmd, " "+dir, dot)[1:] cmd = strings.ReplaceAll(" "+cmd, " "+dir, dot)[1:]
} }
sh.printLocked(cmd + "\n") sh.printfLocked("%s\n", cmd)
} }
// reportCmd reports the output and exit status of a command. The cmdOut and // reportCmd reports the output and exit status of a command. The cmdOut and
@ -522,7 +522,7 @@ func (sh *Shell) reportCmd(desc, dir string, cmdOut []byte, cmdErr error) error
a.output = append(a.output, err.Error()...) a.output = append(a.output, err.Error()...)
} else { } else {
// Write directly to the Builder output. // Write directly to the Builder output.
sh.Print(err.Error()) sh.Printf("%s", err)
} }
return nil return nil
} }