vendor/github.com/ysmood/leakless/pkg/utils/utils.go

139 lines · 2613 bytes

  1package utils
  2
  3import (
  4	"crypto/rand"
  5	"encoding/json"
  6	"io/ioutil"
  7	"os"
  8	"os/exec"
  9	"path/filepath"
 10	"time"
 11)
 12
 13// E if the last arg is error, panic it
 14func E(args ...interface{}) []interface{} {
 15	err, ok := args[len(args)-1].(error)
 16	if ok {
 17		panic(err)
 18	}
 19	return args
 20}
 21
 22// RandBytes generate random bytes with specified byte length
 23func RandBytes(len int) []byte {
 24	b := make([]byte, len)
 25	_, _ = rand.Read(b)
 26	return b
 27}
 28
 29// MkdirOptions ...
 30type MkdirOptions struct {
 31	Perm os.FileMode
 32}
 33
 34// Mkdir makes dir recursively
 35func Mkdir(path string, options *MkdirOptions) error {
 36	if options == nil {
 37		options = &MkdirOptions{
 38			Perm: 0775,
 39		}
 40	}
 41
 42	return os.MkdirAll(path, options.Perm)
 43}
 44
 45// OutputFileOptions ...
 46type OutputFileOptions struct {
 47	DirPerm    os.FileMode
 48	FilePerm   os.FileMode
 49	JSONPrefix string
 50	JSONIndent string
 51}
 52
 53// OutputFile auto creates file if not exists, it will try to detect the data type and
 54// auto output binary, string or json
 55func OutputFile(p string, data interface{}, options *OutputFileOptions) error {
 56	if options == nil {
 57		options = &OutputFileOptions{0775, 0664, "", "    "}
 58	}
 59
 60	dir := filepath.Dir(p)
 61	_ = Mkdir(dir, &MkdirOptions{Perm: options.DirPerm})
 62
 63	var bin []byte
 64
 65	switch t := data.(type) {
 66	case []byte:
 67		bin = t
 68	case string:
 69		bin = []byte(t)
 70	default:
 71		var err error
 72		bin, err = json.MarshalIndent(data, options.JSONPrefix, options.JSONIndent)
 73
 74		if err != nil {
 75			return err
 76		}
 77	}
 78
 79	return ioutil.WriteFile(p, bin, options.FilePerm)
 80}
 81
 82// FileExists checks if file exists, only for file, not for dir
 83func FileExists(path string) bool {
 84	info, err := os.Stat(path)
 85
 86	if err != nil {
 87		return false
 88	}
 89
 90	if info.IsDir() {
 91		return false
 92	}
 93
 94	return true
 95}
 96
 97// Sleep the goroutine for specified seconds, such as 2.3 seconds
 98func Sleep(seconds float64) {
 99	d := time.Duration(seconds * float64(time.Second))
100	time.Sleep(d)
101}
102
103// ReadFile reads file as bytes
104func ReadFile(p string) ([]byte, error) {
105	return ioutil.ReadFile(p)
106}
107
108// ReadJSON reads file as json
109func ReadJSON(p string, data interface{}) error {
110	bin, err := ReadFile(p)
111
112	if err != nil {
113		return err
114	}
115
116	return json.Unmarshal(bin, data)
117}
118
119// MustToJSONBytes encode data to json bytes
120func MustToJSONBytes(data interface{}) []byte {
121	bytes, err := json.Marshal(data)
122	E(err)
123	return bytes
124}
125
126// MustToJSON encode data to json string
127func MustToJSON(data interface{}) string {
128	return string(MustToJSONBytes(data))
129}
130
131// Exec cmd
132func Exec(cmd, dir string, args ...string) {
133	c := exec.Command(cmd, args...)
134	c.Dir = dir
135	c.Stdout = os.Stdout
136	c.Stderr = os.Stderr
137
138	E(c.Run())
139}