github.com/phillinzzz/newBsc@v1.1.6/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 "go/parser" 24 "go/token" 25 "io" 26 "io/ioutil" 27 "log" 28 "os" 29 "os/exec" 30 "path" 31 "path/filepath" 32 "strings" 33 "text/template" 34 ) 35 36 var DryRunFlag = flag.Bool("n", false, "dry run, don't execute commands") 37 38 // MustRun executes the given command and exits the host process for 39 // any error. 40 func MustRun(cmd *exec.Cmd) { 41 fmt.Println(">>>", strings.Join(cmd.Args, " ")) 42 if !*DryRunFlag { 43 cmd.Stderr = os.Stderr 44 cmd.Stdout = os.Stdout 45 if err := cmd.Run(); err != nil { 46 log.Fatal(err) 47 } 48 } 49 } 50 51 func MustRunCommand(cmd string, args ...string) { 52 MustRun(exec.Command(cmd, args...)) 53 } 54 55 var warnedAboutGit bool 56 57 // RunGit runs a git subcommand and returns its output. 58 // The command must complete successfully. 59 func RunGit(args ...string) string { 60 cmd := exec.Command("git", args...) 61 var stdout, stderr bytes.Buffer 62 cmd.Stdout, cmd.Stderr = &stdout, &stderr 63 if err := cmd.Run(); err != nil { 64 if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound { 65 if !warnedAboutGit { 66 log.Println("Warning: can't find 'git' in PATH") 67 warnedAboutGit = true 68 } 69 return "" 70 } 71 log.Fatal(strings.Join(cmd.Args, " "), ": ", err, "\n", stderr.String()) 72 } 73 return strings.TrimSpace(stdout.String()) 74 } 75 76 // readGitFile returns content of file in .git directory. 77 func readGitFile(file string) string { 78 content, err := ioutil.ReadFile(path.Join(".git", file)) 79 if err != nil { 80 return "" 81 } 82 return strings.TrimSpace(string(content)) 83 } 84 85 // Render renders the given template file into outputFile. 86 func Render(templateFile, outputFile string, outputPerm os.FileMode, x interface{}) { 87 tpl := template.Must(template.ParseFiles(templateFile)) 88 render(tpl, outputFile, outputPerm, x) 89 } 90 91 // RenderString renders the given template string into outputFile. 92 func RenderString(templateContent, outputFile string, outputPerm os.FileMode, x interface{}) { 93 tpl := template.Must(template.New("").Parse(templateContent)) 94 render(tpl, outputFile, outputPerm, x) 95 } 96 97 func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x interface{}) { 98 if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil { 99 log.Fatal(err) 100 } 101 out, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_EXCL, outputPerm) 102 if err != nil { 103 log.Fatal(err) 104 } 105 if err := tpl.Execute(out, x); err != nil { 106 log.Fatal(err) 107 } 108 if err := out.Close(); err != nil { 109 log.Fatal(err) 110 } 111 } 112 113 // UploadSFTP uploads files to a remote host using the sftp command line tool. 114 // The destination host may be specified either as [user@]host[: or as a URI in 115 // the form sftp://[user@]host[:port]. 116 func UploadSFTP(identityFile, host, dir string, files []string) error { 117 sftp := exec.Command("sftp") 118 sftp.Stdout = nil 119 sftp.Stderr = os.Stderr 120 if identityFile != "" { 121 sftp.Args = append(sftp.Args, "-i", identityFile) 122 } 123 sftp.Args = append(sftp.Args, host) 124 fmt.Println(">>>", strings.Join(sftp.Args, " ")) 125 if *DryRunFlag { 126 return nil 127 } 128 129 stdin, err := sftp.StdinPipe() 130 if err != nil { 131 return fmt.Errorf("can't create stdin pipe for sftp: %v", err) 132 } 133 if err := sftp.Start(); err != nil { 134 return err 135 } 136 in := io.MultiWriter(stdin, os.Stdout) 137 for _, f := range files { 138 fmt.Fprintln(in, "put", f, path.Join(dir, filepath.Base(f))) 139 } 140 stdin.Close() 141 return sftp.Wait() 142 } 143 144 // FindMainPackages finds all 'main' packages in the given directory and returns their 145 // package paths. 146 func FindMainPackages(dir string) []string { 147 var commands []string 148 cmds, err := ioutil.ReadDir(dir) 149 if err != nil { 150 log.Fatal(err) 151 } 152 for _, cmd := range cmds { 153 pkgdir := filepath.Join(dir, cmd.Name()) 154 pkgs, err := parser.ParseDir(token.NewFileSet(), pkgdir, nil, parser.PackageClauseOnly) 155 if err != nil { 156 log.Fatal(err) 157 } 158 for name := range pkgs { 159 if name == "main" { 160 path := "./" + filepath.ToSlash(pkgdir) 161 commands = append(commands, path) 162 break 163 } 164 } 165 } 166 return commands 167 }