// A Printer reports output about a Package.
type Printer interface {
- // Output reports output from building pkg. The arguments are of the form
- // expected by fmt.Print.
+ // Printf reports output from building pkg. The arguments are of the form
+ // expected by [fmt.Printf].
//
// pkg may be nil if this output is not associated with the build of a
// particular package.
//
// The caller is responsible for checking if printing output is appropriate,
// 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
// building pkg failed.
Writer io.Writer
}
-func (p *TextPrinter) Output(_ *Package, args ...any) {
- fmt.Fprint(p.Writer, args...)
+func (p *TextPrinter) Printf(_ *Package, format string, args ...any) {
+ fmt.Fprintf(p.Writer, format, args...)
}
func (p *TextPrinter) Errorf(_ *Package, format string, args ...any) {
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{
Action: "build-output",
- Output: fmt.Sprint(args...),
+ Output: fmt.Sprintf(format, args...),
}
if ev.Output == "" {
// There's no point in emitting a completely empty output event.
// For clarity, emit each line as a separate output event.
for len(s) > 0 {
i := strings.IndexByte(s, '\n')
- p.Output(pkg, s[:i+1])
+ p.Printf(pkg, "%s", s[:i+1])
s = s[i+1:]
}
ev := &jsonBuildEvent{
b.backgroundSh = NewShell(b.WorkDir, nil)
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 {
sh.ShowCmd("", "%s # internal", joinUnambiguously(str.StringList("cat", c.OutputFile(stdoutEntry.OutputID))))
}
if !cfg.BuildN {
- sh.Print(string(stdout))
+ sh.Printf("%s", stdout)
}
}
return nil
// flushOutput flushes the output being queued in a.
func (b *Builder) flushOutput(a *Action) {
- b.Shell(a).Print(string(a.output))
+ b.Shell(a).Printf("%s", a.output)
a.output = nil
}
// different sections of the bootstrap script have to
// be merged, the banners give patch something
// 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 {
- sh.Print(p.ImportPath + "\n")
+ sh.Printf("%s\n", p.ImportPath)
}
if p.Error != nil {
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.
-func (sh *Shell) Print(a ...any) {
+func (sh *Shell) Printf(format string, a ...any) {
sh.printLock.Lock()
defer sh.printLock.Unlock()
- sh.printer.Output(sh.pkg(), a...)
+ sh.printer.Printf(sh.pkg(), format, a...)
}
-func (sh *Shell) printLocked(a ...any) {
- sh.printer.Output(sh.pkg(), a...)
+func (sh *Shell) printfLocked(format string, a ...any) {
+ sh.printer.Printf(sh.pkg(), format, a...)
}
// Errorf reports an error on sh's package and sets the process exit status to 1.
if dir != "" && dir != "/" {
if dir != sh.scriptDir {
// 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
}
// Replace scriptDir is our working directory. Replace it
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
a.output = append(a.output, err.Error()...)
} else {
// Write directly to the Builder output.
- sh.Print(err.Error())
+ sh.Printf("%s", err)
}
return nil
}