github.com/supragya/TendermintConnector@v0.0.0-20210619045051-113e32b84fb1/_deprecated_chains/cosmos/libs/common/os.go (about) 1 package common 2 3 import ( 4 "bufio" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "os/exec" 10 "os/signal" 11 "strings" 12 "syscall" 13 ) 14 15 var gopath string 16 17 // GoPath returns GOPATH env variable value. If it is not set, this function 18 // will try to call `go env GOPATH` subcommand. 19 func GoPath() string { 20 if gopath != "" { 21 return gopath 22 } 23 24 path := os.Getenv("GOPATH") 25 if len(path) == 0 { 26 goCmd := exec.Command("go", "env", "GOPATH") 27 out, err := goCmd.Output() 28 if err != nil { 29 panic(fmt.Sprintf("failed to determine gopath: %v", err)) 30 } 31 path = string(out) 32 } 33 gopath = path 34 return path 35 } 36 37 // TrapSignal catches the SIGTERM and executes cb function. After that it exits 38 // with code 1. 39 func TrapSignal(cb func()) { 40 c := make(chan os.Signal, 1) 41 signal.Notify(c, os.Interrupt, syscall.SIGTERM) 42 go func() { 43 for sig := range c { 44 fmt.Printf("captured %v, exiting...\n", sig) 45 if cb != nil { 46 cb() 47 } 48 os.Exit(1) 49 } 50 }() 51 select {} 52 } 53 54 // Kill the running process by sending itself SIGTERM. 55 func Kill() error { 56 p, err := os.FindProcess(os.Getpid()) 57 if err != nil { 58 return err 59 } 60 return p.Signal(syscall.SIGTERM) 61 } 62 63 func Exit(s string) { 64 fmt.Printf(s + "\n") 65 os.Exit(1) 66 } 67 68 func EnsureDir(dir string, mode os.FileMode) error { 69 if _, err := os.Stat(dir); os.IsNotExist(err) { 70 err := os.MkdirAll(dir, mode) 71 if err != nil { 72 return fmt.Errorf("Could not create directory %v. %v", dir, err) 73 } 74 } 75 return nil 76 } 77 78 func IsDirEmpty(name string) (bool, error) { 79 f, err := os.Open(name) 80 if err != nil { 81 if os.IsNotExist(err) { 82 return true, err 83 } 84 // Otherwise perhaps a permission 85 // error or some other error. 86 return false, err 87 } 88 defer f.Close() 89 90 _, err = f.Readdirnames(1) // Or f.Readdir(1) 91 if err == io.EOF { 92 return true, nil 93 } 94 return false, err // Either not empty or error, suits both cases 95 } 96 97 func FileExists(filePath string) bool { 98 _, err := os.Stat(filePath) 99 return !os.IsNotExist(err) 100 } 101 102 func ReadFile(filePath string) ([]byte, error) { 103 return ioutil.ReadFile(filePath) 104 } 105 106 func MustReadFile(filePath string) []byte { 107 fileBytes, err := ioutil.ReadFile(filePath) 108 if err != nil { 109 Exit(fmt.Sprintf("MustReadFile failed: %v", err)) 110 return nil 111 } 112 return fileBytes 113 } 114 115 func WriteFile(filePath string, contents []byte, mode os.FileMode) error { 116 return ioutil.WriteFile(filePath, contents, mode) 117 } 118 119 func MustWriteFile(filePath string, contents []byte, mode os.FileMode) { 120 err := WriteFile(filePath, contents, mode) 121 if err != nil { 122 Exit(fmt.Sprintf("MustWriteFile failed: %v", err)) 123 } 124 } 125 126 //-------------------------------------------------------------------------------- 127 128 func Prompt(prompt string, defaultValue string) (string, error) { 129 fmt.Print(prompt) 130 reader := bufio.NewReader(os.Stdin) 131 line, err := reader.ReadString('\n') 132 if err != nil { 133 return defaultValue, err 134 } 135 line = strings.TrimSpace(line) 136 if line == "" { 137 return defaultValue, nil 138 } 139 return line, nil 140 }