github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/internal/build/util.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package build
    18  
    19  import (
    20  	"bytes"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"io/ioutil"
    25  	"log"
    26  	"os"
    27  	"os/exec"
    28  	"path/filepath"
    29  	"strings"
    30  	"text/template"
    31  )
    32  
    33  var DryRunFlag = flag.Bool("n", false, "dry run, don't execute commands")
    34  
    35  // MustRun executes the given command and exits the host process for
    36  // any error.
    37  func MustRun(cmd *exec.Cmd) {
    38  	fmt.Println(">>>", strings.Join(cmd.Args, " "))
    39  	if !*DryRunFlag {
    40  		cmd.Stderr = os.Stderr
    41  		cmd.Stdout = os.Stdout
    42  		if err := cmd.Run(); err != nil {
    43  			log.Fatal(err)
    44  		}
    45  	}
    46  }
    47  
    48  func MustRunCommand(cmd string, args ...string) {
    49  	MustRun(exec.Command(cmd, args...))
    50  }
    51  
    52  // GOPATH returns the value that the GOPATH environment
    53  // variable should be set to.
    54  func GOPATH() string {
    55  	path := filepath.SplitList(os.Getenv("GOPATH"))
    56  	if len(path) == 0 {
    57  		log.Fatal("GOPATH is not set")
    58  	}
    59  	// Ensure that our internal vendor folder is on GOPATH
    60  	vendor, _ := filepath.Abs(filepath.Join("build", "_vendor"))
    61  	for _, dir := range path {
    62  		if dir == vendor {
    63  			return strings.Join(path, string(filepath.ListSeparator))
    64  		}
    65  	}
    66  	newpath := append(path[:1], append([]string{vendor}, path[1:]...)...)
    67  	return strings.Join(newpath, string(filepath.ListSeparator))
    68  }
    69  
    70  // VERSION returns the content of the VERSION file.
    71  func VERSION() string {
    72  	version, err := ioutil.ReadFile("VERSION")
    73  	if err != nil {
    74  		log.Fatal(err)
    75  	}
    76  	return string(bytes.TrimSpace(version))
    77  }
    78  
    79  var warnedAboutGit bool
    80  
    81  // RunGit runs a git subcommand and returns its output.
    82  // The command must complete successfully.
    83  func RunGit(args ...string) string {
    84  	cmd := exec.Command("git", args...)
    85  	var stdout, stderr bytes.Buffer
    86  	cmd.Stdout, cmd.Stderr = &stdout, &stderr
    87  	if err := cmd.Run(); err == exec.ErrNotFound {
    88  		if !warnedAboutGit {
    89  			log.Println("Warning: can't find 'git' in PATH")
    90  			warnedAboutGit = true
    91  		}
    92  		return ""
    93  	} else if err != nil {
    94  		log.Fatal(strings.Join(cmd.Args, " "), ": ", err, "\n", stderr.String())
    95  	}
    96  	return strings.TrimSpace(stdout.String())
    97  }
    98  
    99  // Render renders the given template file into outputFile.
   100  func Render(templateFile, outputFile string, outputPerm os.FileMode, x interface{}) {
   101  	tpl := template.Must(template.ParseFiles(templateFile))
   102  	render(tpl, outputFile, outputPerm, x)
   103  }
   104  
   105  // RenderString renders the given template string into outputFile.
   106  func RenderString(templateContent, outputFile string, outputPerm os.FileMode, x interface{}) {
   107  	tpl := template.Must(template.New("").Parse(templateContent))
   108  	render(tpl, outputFile, outputPerm, x)
   109  }
   110  
   111  func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x interface{}) {
   112  	if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil {
   113  		log.Fatal(err)
   114  	}
   115  	out, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_EXCL, outputPerm)
   116  	if err != nil {
   117  		log.Fatal(err)
   118  	}
   119  	if err := tpl.Execute(out, x); err != nil {
   120  		log.Fatal(err)
   121  	}
   122  	if err := out.Close(); err != nil {
   123  		log.Fatal(err)
   124  	}
   125  }
   126  
   127  // CopyFile copies a file.
   128  func CopyFile(dst, src string, mode os.FileMode) {
   129  	if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
   130  		log.Fatal(err)
   131  	}
   132  	destFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
   133  	if err != nil {
   134  		log.Fatal(err)
   135  	}
   136  	defer destFile.Close()
   137  
   138  	srcFile, err := os.Open(src)
   139  	if err != nil {
   140  		log.Fatal(err)
   141  	}
   142  	defer srcFile.Close()
   143  
   144  	if _, err := io.Copy(destFile, srcFile); err != nil {
   145  		log.Fatal(err)
   146  	}
   147  }