2015-10-21 12:15:19 -07:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2017-01-27 20:37:04 -08:00
|
|
|
"io"
|
2015-10-21 12:15:19 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-10-11 12:42:54 +04:00
|
|
|
"os/exec"
|
2015-10-21 12:15:19 -07:00
|
|
|
"os/signal"
|
|
|
|
"strings"
|
2017-10-04 00:13:58 -04:00
|
|
|
"syscall"
|
2015-10-21 12:15:19 -07:00
|
|
|
)
|
|
|
|
|
2017-10-25 11:01:52 +04:00
|
|
|
var gopath string
|
|
|
|
|
|
|
|
// GoPath returns GOPATH env variable value. If it is not set, this function
|
|
|
|
// will try to call `go env GOPATH` subcommand.
|
|
|
|
func GoPath() string {
|
|
|
|
if gopath != "" {
|
|
|
|
return gopath
|
|
|
|
}
|
2015-10-21 12:15:19 -07:00
|
|
|
|
2017-10-11 12:42:54 +04:00
|
|
|
path := os.Getenv("GOPATH")
|
|
|
|
if len(path) == 0 {
|
|
|
|
goCmd := exec.Command("go", "env", "GOPATH")
|
|
|
|
out, err := goCmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to determine gopath: %v", err))
|
|
|
|
}
|
|
|
|
path = string(out)
|
|
|
|
}
|
2017-10-25 11:01:52 +04:00
|
|
|
gopath = path
|
2017-10-11 12:42:54 +04:00
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2017-11-03 23:51:39 -05:00
|
|
|
// TrapSignal catches the SIGTERM and executes cb function. After that it exits
|
|
|
|
// with code 1.
|
2015-10-21 12:15:19 -07:00
|
|
|
func TrapSignal(cb func()) {
|
|
|
|
c := make(chan os.Signal, 1)
|
2017-10-04 00:13:58 -04:00
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
2015-10-21 12:15:19 -07:00
|
|
|
go func() {
|
|
|
|
for sig := range c {
|
|
|
|
fmt.Printf("captured %v, exiting...\n", sig)
|
|
|
|
if cb != nil {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
2017-11-03 23:51:39 -05:00
|
|
|
// Kill the running process by sending itself SIGTERM.
|
2017-10-27 11:01:40 -04:00
|
|
|
func Kill() error {
|
2017-11-03 23:51:39 -05:00
|
|
|
p, err := os.FindProcess(os.Getpid())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.Signal(syscall.SIGTERM)
|
2017-10-27 11:01:40 -04:00
|
|
|
}
|
|
|
|
|
2015-10-21 12:15:19 -07:00
|
|
|
func Exit(s string) {
|
|
|
|
fmt.Printf(s + "\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-12-03 23:44:24 -08:00
|
|
|
func EnsureDir(dir string, mode os.FileMode) error {
|
2015-10-21 12:15:19 -07:00
|
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
2015-12-03 23:44:24 -08:00
|
|
|
err := os.MkdirAll(dir, mode)
|
2015-10-21 12:15:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not create directory %v. %v", dir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-27 20:37:04 -08:00
|
|
|
func IsDirEmpty(name string) (bool, error) {
|
|
|
|
f, err := os.Open(name)
|
|
|
|
if err != nil {
|
2017-08-04 02:03:46 -06:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
// Otherwise perhaps a permission
|
|
|
|
// error or some other error.
|
|
|
|
return false, err
|
2017-01-27 20:37:04 -08:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Readdirnames(1) // Or f.Readdir(1)
|
|
|
|
if err == io.EOF {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, err // Either not empty or error, suits both cases
|
|
|
|
}
|
|
|
|
|
2015-10-21 12:15:19 -07:00
|
|
|
func FileExists(filePath string) bool {
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadFile(filePath string) ([]byte, error) {
|
|
|
|
return ioutil.ReadFile(filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MustReadFile(filePath string) []byte {
|
|
|
|
fileBytes, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
2018-08-10 00:25:57 -05:00
|
|
|
Exit(fmt.Sprintf("MustReadFile failed: %v", err))
|
2015-10-21 12:15:19 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fileBytes
|
|
|
|
}
|
|
|
|
|
2015-12-03 23:56:50 -08:00
|
|
|
func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
|
2017-09-22 11:42:29 -04:00
|
|
|
return ioutil.WriteFile(filePath, contents, mode)
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
|
|
|
|
2015-12-03 23:56:50 -08:00
|
|
|
func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
|
|
|
|
err := WriteFile(filePath, contents, mode)
|
2015-10-21 12:15:19 -07:00
|
|
|
if err != nil {
|
2018-08-10 00:25:57 -05:00
|
|
|
Exit(fmt.Sprintf("MustWriteFile failed: %v", err))
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 16:15:57 -07:00
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
2015-10-21 12:15:19 -07:00
|
|
|
func Prompt(prompt string, defaultValue string) (string, error) {
|
|
|
|
fmt.Print(prompt)
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return defaultValue, err
|
|
|
|
}
|
2018-04-03 12:23:28 +02:00
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if line == "" {
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
return line, nil
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|