2
2
package goext
3
3
4
4
import (
5
- "encoding/json"
6
- "errors"
7
5
"fmt"
8
6
"os"
9
- "sort"
10
7
"strconv"
11
- "strings"
12
8
)
13
9
14
- // AddIf adds the given values to a slice if the condition is fulfilled.
15
- func AddIf [T any ](slice []T , cond bool , values ... T ) []T {
16
- if cond {
17
- return append (slice , values ... )
18
- }
19
- return slice
20
- }
21
-
22
- // AppendIfMissing appends the given element if it is missing in the slice.
23
- func AppendIfMissing [T comparable ](slice []T , value T ) []T {
24
- for _ , ele := range slice {
25
- if ele == value {
26
- return slice
27
- }
28
- }
29
- return append (slice , value )
30
- }
31
-
32
10
// Ternary adds the missing ternary operator.
33
11
func Ternary [T any ](cond bool , vtrue , vfalse T ) T {
34
12
if cond {
@@ -43,17 +21,6 @@ func Printfln(format string, a ...any) (n int, err error) {
43
21
return fmt .Println (text )
44
22
}
45
23
46
- // RemoveEmpty removes all empty strings from an array.
47
- func RemoveEmpty (s []string ) []string {
48
- var r []string
49
- for _ , str := range s {
50
- if str != "" {
51
- r = append (r , str )
52
- }
53
- }
54
- return r
55
- }
56
-
57
24
// RunInDirectory runs a given function inside the passed directory as working directory.
58
25
// It resets to the previous directory when finished (or an error occured).
59
26
func RunInDirectory (path string , f func () error ) (err error ) {
@@ -95,99 +62,8 @@ func Noop() error {
95
62
return nil
96
63
}
97
64
98
- // WriteJsonToFile writes the given object into a file.
99
- func WriteJsonToFile (object any , outputFilePath string , indented bool ) error {
100
- var data []byte
101
- var err error
102
- if indented {
103
- data , err = json .MarshalIndent (object , "" , " " )
104
- } else {
105
- data , err = json .Marshal (object )
106
- }
107
- if err != nil {
108
- return err
109
- }
110
- if err := os .WriteFile (outputFilePath , data , os .ModePerm ); err != nil {
111
- return err
112
- }
113
- return nil
114
- }
115
-
116
- // EnvExists checks if the given environment variable exists or not.
117
- func EnvExists (key string ) bool {
118
- if _ , ok := os .LookupEnv (key ); ok {
119
- return true
120
- }
121
- return false
122
- }
123
-
124
- // GetEnvOrDefault returns the value if the environment variable exists or the default otherwise.
125
- func GetEnvOrDefault (key string , defaultValue string ) (string , bool ) {
126
- if _ , ok := os .LookupEnv (key ); ok {
127
- return key , true
128
- }
129
- return defaultValue , false
130
- }
131
-
132
- // FileExists checks if a file exists (and it is not a directory).
133
- func FileExists (filePath string ) (bool , error ) {
134
- info , err := os .Stat (filePath )
135
- if err == nil {
136
- return ! info .IsDir (), nil
137
- }
138
- if errors .Is (err , os .ErrNotExist ) {
139
- return false , nil
140
- }
141
- return false , err
142
- }
143
-
144
65
// Pass is a no-op function that can be used to set variables to used.
145
66
// Usefull during development but must be removed afterwards!
146
67
func Pass (i ... interface {}) {
147
68
// No-Op
148
69
}
149
-
150
- func TrimAllPrefix (s , prefix string ) string {
151
- for strings .HasPrefix (s , prefix ) {
152
- s = s [len (prefix ):]
153
- }
154
- return s
155
- }
156
-
157
- func TrimAllSuffix (s , suffix string ) string {
158
- for strings .HasSuffix (s , suffix ) {
159
- s = s [:len (s )- len (suffix )]
160
- }
161
- return s
162
- }
163
-
164
- func TrimNewlineSuffix (v string ) string {
165
- return TrimAllSuffix (TrimAllSuffix (v , "\r \n " ), "\n " )
166
- }
167
-
168
- // SplitByNewLine splits the given value by newlines.
169
- func SplitByNewLine (value string ) []string {
170
- return strings .Split (strings .ReplaceAll (value , "\r \n " , "\n " ), "\n " )
171
- }
172
-
173
- // ProcessMapSorted calls the given function on all entries of the map, sorted by their key value (by string).
174
- func ProcessMapSorted [TK comparable , TV any ](m map [TK ]TV , pf func (TK , TV ) error ) error {
175
- // Get all keys
176
- keys := make ([]TK , 0 , len (m ))
177
- for k := range m {
178
- keys = append (keys , k )
179
- }
180
-
181
- // Sort the keys alphanumeric
182
- sort .Slice (keys , func (i , j int ) bool {
183
- return fmt .Sprint (keys [i ]) < fmt .Sprint (keys [j ])
184
- })
185
-
186
- // Call the method for each function
187
- for _ , k := range keys {
188
- if err := pf (k , m [k ]); err != nil {
189
- return err
190
- }
191
- }
192
- return nil
193
- }
0 commit comments