tendermint/libs/common/os.go

141 lines
2.8 KiB
Go
Raw Normal View History

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"
"os/exec"
2015-10-21 12:15:19 -07:00
"os/signal"
"strings"
2017-10-03 21:13:58 -07:00
"syscall"
2015-10-21 12:15:19 -07: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
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)
}
gopath = path
return path
}
2017-11-03 21:51:39 -07: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-03 21:13:58 -07: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 21:51:39 -07:00
// Kill the running process by sending itself SIGTERM.
2017-10-27 08:01:40 -07:00
func Kill() error {
2017-11-03 21:51:39 -07:00
p, err := os.FindProcess(os.Getpid())
if err != nil {
return err
}
return p.Signal(syscall.SIGTERM)
2017-10-27 08:01:40 -07: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 {
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 {
Exit(Fmt("MustReadFile failed: %v", err))
return nil
}
return fileBytes
}
2015-12-03 23:56:50 -08:00
func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
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 {
Exit(Fmt("MustWriteFile failed: %v", err))
}
}
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
}
line = strings.TrimSpace(line)
if line == "" {
return defaultValue, nil
}
return line, nil
2015-10-21 12:15:19 -07:00
}