func Format(req *FormatRequest, rep *FormatReply) error {
	for _, f := range req.Files {
		if f.Language == "" {
			return fmt.Errorf("file %q has empty language", f.Name)
		}
	}
	for language, fs := range splitByLang(req.Files) {
		var buf bytes.Buffer
		entry := Formatters[language]
		log.Println("init", Formatters)
		out, err := entry.Formatter.Format(fs, &buf)
		if err != nil {
			return err
		}
		if len(out) > 0 && out[0].Message == "" {
			out[0].Message = buf.String()
		}
		rep.Files = append(rep.Files, out...)
	}
	return nil
}
Format formats all the files in the request for which a formatter exists.
package gerritlinter
import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"sort"
	"strings"
)
type Formatter interface {
	Format(in []File, outSink io.Writer) (out []FormattedFile, err error)
}
type FormatterConfig struct {
	Regex *regexp.Regexp
	Query string
	Formatter Formatter
}
var Formatters = map[string]*FormatterConfig{
	"commitmsg": {
		Regex:     regexp.MustCompile(`^/COMMIT_MSG$`),
		Formatter: &commitMsgFormatter{},
	},
}
func init() {
	if exe, err := os.Executable(); err == nil {
		os.Setenv("PATH", filepath.Dir(exe)+":"+os.Getenv("PATH"))
	}
	gjf, err := exec.LookPath("google-java-format.jar")
	if err == nil {
		Formatters["java"] = &FormatterConfig{
			Regex: regexp.MustCompile(`\.java$`),
			Query: "ext:java",
			Formatter: &toolFormatter{
				bin:  "java",
				args: []string{"-jar", gjf, "-i"},
			},
		}
	} else {
		log.Printf("LookPath google-java-format: %v PATH=%s", err, os.Getenv("PATH"))
	}
	bzl, err := exec.LookPath("buildifier")
	if err == nil {
		Formatters["bzl"] = &FormatterConfig{
			Regex: regexp.MustCompile(`(\.bzl|/BUILD|^BUILD)$`),
			Query: "(ext:bzl OR file:BUILD OR file:WORKSPACE)",
			Formatter: &toolFormatter{
				bin:  bzl,
				args: []string{"-mode=fix"},
			},
		}
	} else {
		log.Printf("LookPath buildifier: %v, PATH=%s", err, os.Getenv("PATH"))
	}
	gofmt, err := exec.LookPath("gofmt")
	if err == nil {
		Formatters["go"] = &FormatterConfig{
			Regex: regexp.MustCompile(`\.go$`),
			Query: "ext:go",
			Formatter: &toolFormatter{
				bin:  gofmt,
				args: []string{"-w"},
			},
		}
	} else {
		log.Printf("LookPath gofmt: %v, PATH=%s", err, os.Getenv("PATH"))
	}
}
func IsSupported(lang string) bool {
	_, ok := Formatters[lang]
	return ok
}
func SupportedLanguages() []string {
	var r []string
	for l := range Formatters {
		r = append(r, l)
	}
	sort.Strings(r)
	return r
}
func splitByLang(in []File) map[string][]File {
	res := map[string][]File{}
	for _, f := range in {
		res[f.Language] = append(res[f.Language], f)
	}
	return res
}