github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/src/cmd/api/run.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build ignore
     6  
     7  // The run program is invoked via the dist tool.
     8  // To invoke manually: go tool dist test -run api --no-rebuild
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"log"
    14  	"os"
    15  	"os/exec"
    16  	"path/filepath"
    17  	"runtime"
    18  	"strings"
    19  )
    20  
    21  func goCmd() string {
    22  	var exeSuffix string
    23  	if runtime.GOOS == "windows" {
    24  		exeSuffix = ".exe"
    25  	}
    26  	path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
    27  	if _, err := os.Stat(path); err == nil {
    28  		return path
    29  	}
    30  	return "go"
    31  }
    32  
    33  var goroot string
    34  
    35  func main() {
    36  	log.SetFlags(0)
    37  	goroot = os.Getenv("GOROOT") // should be set by run.{bash,bat}
    38  	if goroot == "" {
    39  		log.Fatal("No $GOROOT set.")
    40  	}
    41  
    42  	apiDir := filepath.Join(goroot, "api")
    43  	out, err := exec.Command(goCmd(), "tool", "api",
    44  		"-c", findAPIDirFiles(apiDir),
    45  		"-next", filepath.Join(apiDir, "next.txt"),
    46  		"-except", filepath.Join(apiDir, "except.txt")).CombinedOutput()
    47  	if err != nil {
    48  		log.Fatalf("Error running API checker: %v\n%s", err, out)
    49  	}
    50  	fmt.Print(string(out))
    51  }
    52  
    53  // findAPIDirFiles returns a comma-separated list of Go API files
    54  // (go1.txt, go1.1.txt, etc.) located in apiDir.
    55  func findAPIDirFiles(apiDir string) string {
    56  	dir, err := os.Open(apiDir)
    57  	if err != nil {
    58  		log.Fatal(err)
    59  	}
    60  	defer dir.Close()
    61  	fs, err := dir.Readdirnames(-1)
    62  	if err != nil {
    63  		log.Fatal(err)
    64  	}
    65  	var apiFiles []string
    66  	for _, fn := range fs {
    67  		if strings.HasPrefix(fn, "go1") {
    68  			apiFiles = append(apiFiles, filepath.Join(apiDir, fn))
    69  		}
    70  	}
    71  	return strings.Join(apiFiles, ",")
    72  }