// hookGofmt runs a gofmt check on the local files matching the files in the
// git staging area.
// An error is returned if something went wrong or if some of the files need
// gofmting. In the latter case, the instruction is printed.
549757
func (c *hookCmd) hookGofmt() error {
	if os.Getenv("GIT_GOFMT_HOOK") == "off" {
		printf("gofmt disabled by $GIT_GOFMT_HOOK=off\n")
		return nil
	}

	files, err := c.runGofmt()
	if err != nil {
		printf("gofmt hook reported errors:\n\t%v\n", strings.Replace(strings.TrimSpace(err.Error()), "\n", "\n\t", -1))
		return errors.New("gofmt errors")
	}
	if len(files) == 0 {
		return nil
	}
	printf("You need to format with gofmt:\n\tgofmt -w %s\n",
		strings.Join(files, " "))
	return errors.New("gofmt required")
}
868014
func ByGoarm(s string) Filter {
	return func(a Artifact) bool {
		return a.Goarm == s
	}
}
546119
func (ui *UI) DisplayOK() {
	ui.terminalLock.Lock()
	defer ui.terminalLock.Unlock()

	fmt.Fprintf(ui.Out, "%s\n\n", ui.modifyColor(ui.TranslateText("OK"), color.New(color.FgGreen, color.Bold)))
}
566272
func (bs *BuildSummary) GitilesCommit() *buildbucketpb.GitilesCommit {
	for _, bset := range bs.BuildSet {
		if gc, ok := protoutil.ParseBuildSet(bset).(*buildbucketpb.GitilesCommit); ok {
			return gc
		}
	}
	return nil
}
468683
func GoFmt(buf string) string {
	formatted, err := format.Source([]byte(buf))
	if err != nil {
		panic(fmt.Errorf("%s\nOriginal code:\n%s", err.Error(), buf))
	}
	return string(formatted)
}
1293754
func ReadGodelConfigFromFile(cfgFile string) (GodelConfig, error) {
	if _, err := os.Stat(cfgFile); os.IsNotExist(err) {
		return GodelConfig{}, nil
	}

	var godelCfg GodelConfig
	bytes, err := ioutil.ReadFile(cfgFile)
	if err != nil {
		return GodelConfig{}, errors.Wrapf(err, "failed to read file %s", cfgFile)
	}
	upgradedBytes, err := UpgradeConfig(bytes)
	if err != nil {
		return GodelConfig{}, errors.Wrapf(err, "failed to upgrade configuration")
	}
	if err := yaml.Unmarshal(upgradedBytes, &godelCfg); err != nil {
		return GodelConfig{}, errors.Wrapf(err, "failed to unmarshal gödel config YAML")
	}
	return godelCfg, nil
}
1294626
func detectCustomGOPATH(ctx context.Context, fs ctxvfs.FileSystem) (gopaths []string) {
	// If we detect any .sorucegraph/config.json GOPATHs then they take
	// absolute precedence and override all others.
	if paths := detectSourcegraphGOPATH(ctx, fs); len(paths) > 0 {
		return paths
	}

	// Check .vscode/config.json and .envrc files, giving them equal precedence.
	if paths := detectVSCodeGOPATH(ctx, fs); len(paths) > 0 {
		gopaths = append(gopaths, paths...)
	}
	if paths := detectEnvRCGOPATH(ctx, fs); len(paths) > 0 {
		gopaths = append(gopaths, paths...)
	}
	return
}
282371
func Fmt() error {
	if !isGoLatest() {
		return nil
	}
	pkgs, err := hugoPackages()
	if err != nil {
		return err
	}
	failed := false
	first := true
	for _, pkg := range pkgs {
		files, err := filepath.Glob(filepath.Join(pkg, "*.go"))
		if err != nil {
			return nil
		}
		for _, f := range files {
			// gofmt doesn't exit with non-zero when it finds unformatted code
			// so we have to explicitly look for output, and if we find any, we
			// should fail this target.
			s, err := sh.Output("gofmt", "-l", f)
			if err != nil {
				fmt.Printf("ERROR: running gofmt on %q: %v\n", f, err)
				failed = true
			}
			if s != "" {
				if first {
					fmt.Println("The following files are not gofmt'ed:")
					first = false
				}
				failed = true
				fmt.Println(s)
			}
		}
	}
	if failed {
		return errors.New("improperly formatted go files")
	}
	return nil
}
798903
func (f *flowBuf) putGoto(pos token.Pos, target string, b *block) {
	f.gotos[pos] = newFlowBlock(target, b)
}
666332
func SaveGopmfile(gf *goconfig.ConfigFile, fileName string) error {
	if err := goconfig.SaveConfigFile(gf, fileName); err != nil {
		return fmt.Errorf("Fail to save gopmfile: %v", err)
	}
	return nil
}
355511
func golua_callgofunction(gostateindex uintptr, fid uint) int {
	L1 := getGoState(gostateindex)
	if fid < 0 {
		panic(&LuaError{0, "Requested execution of an unknown function", L1.StackTrace()})
	}
	f := L1.registry[fid].(LuaGoFunction)
	return f(L1)
}
4562
func (h *HandlerT) StartGoTrace(file string) error {
	h.mu.Lock()
	defer h.mu.Unlock()
	if h.traceW != nil {
		return errors.New("trace already in progress")
	}
	f, err := os.Create(expandHome(file))
	if err != nil {
		return err
	}
	if err := trace.Start(f); err != nil {
		f.Close()
		return err
	}
	h.traceW = f
	h.traceFile = file
	log.Info("Go tracing started", "dump", h.traceFile)
	return nil
}
821802
func resolveGoModFile(dir string) (*os.File, string, error) {
	goModPath := filepath.Join(dir, "go.mod")
	f, err := os.Open(goModPath)
	if err != nil {
		if os.IsNotExist(err) && dir != filepath.Dir(dir) {
			return resolveGoModFile(filepath.Dir(dir))
		}
		return nil, "", err
	}
	return f, dir, nil
}
170426
func (cl *CoLog) AddHook(hook Hook) {
	cl.mu.Lock()
	defer cl.mu.Unlock()

	for _, l := range hook.Levels() {
		cl.hooks[l] = append(cl.hooks[l], hook)
	}
}
929373
func GoFmt(files ...string) *exec.Cmd {
	if len(files) == 0 {
		files = []string{"."}
	}
	c := "gofmt"
	_, err := exec.LookPath("goimports")
	if err == nil {
		c = "goimports"
	}
	_, err = exec.LookPath("gofmt")
	if err != nil {
		return exec.Command("echo", "could not find gofmt or goimports")
	}
	args := []string{"-w"}
	args = append(args, files...)
	return exec.Command(c, args...)
}
835039
func runGoFmt(filePath string) error {
	args := []string{
		"-w",
		filePath,
	}

	if out, err := exec.Command("gofmt", args...).CombinedOutput(); err != nil {
		log.Errorf("Error running go fmt on '%s' failed:\n%s", filePath, string(out))
		return errors.New("go fmt failed")
	}
	return nil
}
1298254
func loadGopkgLock(rawToml []byte) pinnedPkgs {
	lock := struct {
		Projects []struct {
			Name     string `toml:"name"`
			Revision string `toml:"revision"`
			// There are other fields, but we don't use them
		} `toml:"projects"`
		// There are other fields, but we don't use them
	}{}
	err := toml.Unmarshal(rawToml, &lock)
	if err != nil {
		return nil
	}

	pkgs := make(pinnedPkgs, 0, len(lock.Projects))
	for _, p := range lock.Projects {
		pkgs = append(pkgs, pinnedPkg{Pkg: p.Name + "/", Rev: p.Revision})
	}
	sort.Sort(pkgs)
	return pkgs
}