forked from nano-interactive/go-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
205 lines (162 loc) · 4.1 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package utils
import (
"crypto/rand"
"encoding/base64"
"io/fs"
"net/http"
"os"
"path/filepath"
"unicode"
"unsafe"
)
//nolint:gosec
/*#nosec G103*/
func UnsafeBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
//nolint:gosec
/*#nosec G103*/
func UnsafeString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}
// Returns bool value of HTTP status code if its failure or success
func IsSuccess(status int) bool {
return status >= http.StatusOK && status < http.StatusMultipleChoices
}
// Provides a way of testing if type is integer
// Returns bool value depending if its integer or not
func IsInt(s string) bool {
if len(s) > 0 && s[0] == '0' {
return false
}
for _, c := range s {
if !unicode.IsDigit(c) {
return false
}
}
return true
}
// Returns ENV string
func Getenv(env string, def ...string) string {
item, exists := os.LookupEnv(env)
if !exists && len(def) > 0 {
return def[0]
}
return item
}
// Returns random string of given length
func RandomString(n int32) string {
buffer := make([]byte, n)
_, err := rand.Read(buffer)
if err != nil {
return ""
}
return base64.RawURLEncoding.EncodeToString(buffer)
}
// Returns absolute path string for a given directory and error if directory doesent exist
func GetAbsolutePath(path string) (string, error) {
var err error
if !filepath.IsAbs(path) {
path, err = filepath.Abs(path)
if err != nil {
return "", err
}
return path, nil
}
return path, err
}
// Provides a way of creating a directory from a path
// Returns created directory path and error if fails
func CreateDirectoryFromFile(path string, perm fs.FileMode) (string, error) {
p, err := GetAbsolutePath(path)
if err != nil {
return "", err
}
directory := filepath.Dir(p)
if err := os.MkdirAll(directory, perm); err != nil {
return "", err
}
return p, nil
}
// Creates file for given directory with flags and permissions for directory and file
// Returns file instance and error if it fails
func CreateFile(path string, flags int, dirMode, mode fs.FileMode) (file *os.File, err error) {
path, err = CreateDirectoryFromFile(path, dirMode|fs.ModeDir)
if err != nil {
return nil, err
}
if _, err = os.Stat(path); err != nil {
if !os.IsNotExist(err) {
return nil, err
}
//#nosec G304
file, err = os.Create(path)
if err != nil {
return nil, err
}
if err = file.Chmod(mode); err != nil {
return nil, err
}
if err = file.Close(); err != nil {
return nil, err
}
}
//#nosec G304
file, err = os.OpenFile(path, flags, mode)
return
}
// Creates write only appendable file with permission 0o744 for directory and file for given path
func CreateLogFile(path string) (file *os.File, err error) {
file, err = CreateFile(path, os.O_WRONLY|os.O_APPEND, 0o744, fs.FileMode(0o744)|os.ModeAppend)
return
}
// Provides a way of checking if file exists. Returns bool
func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// Provides a way of creating directory with permissions for a given path
// Returns string path of created directory and error if fails
func CreateDirectory(path string, perm fs.FileMode) (string, error) {
p, err := GetAbsolutePath(path)
if err != nil {
return "", err
}
if err := os.MkdirAll(p, perm); err != nil {
return "", err
}
return p, nil
}
// Provides a way of copying bytes into new byte slice
// Returns byte slice
func CopyBytes(input []byte) []byte {
c := make([]byte, len(input))
copy(c, input)
return c
}
var pixel []byte = nil
const image = "R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=="
func init() {
if pixel == nil {
var err error
pixel, err = base64.StdEncoding.DecodeString(image)
if err != nil {
panic(err)
}
}
}
func GetBrokenImageBytes() []byte {
return pixel
}
// Returns unique slice back
func UniqueSliceElements[T comparable](inputSlice []T) []T {
uniqueSlice := make([]T, 0, len(inputSlice))
seen := make(map[T]bool, len(inputSlice))
for _, element := range inputSlice {
if !seen[element] {
uniqueSlice = append(uniqueSlice, element)
seen[element] = true
}
}
return uniqueSlice
}