-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
59 lines (54 loc) · 1.35 KB
/
helper.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
package helper
import (
"errors"
"github.com/go-akka/configuration"
log "github.com/sirupsen/logrus"
"io/fs"
"os"
"path/filepath"
)
func PathExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
} else if errors.Is(err, os.ErrNotExist) {
return false
} else {
log.Panic("Couldn't fetch stats for "+path, err)
return false
}
}
func WriteConfig(config *configuration.Config, path string) {
path = path + "/.latex"
err := os.WriteFile(path, []byte(ConfigHeader+"\n"+config.String()), 0644)
if err != nil {
log.Panic("Couldn't write config to "+path, err)
}
}
func GetConfig(path string) (config *configuration.Config, er error) {
mainConfig := path + "/.latex"
log.Println("Opening config file for reading. Path:" + mainConfig)
c, err := os.ReadFile(mainConfig)
if err != nil {
log.Error("Couldn't read the main config file. ", err)
return nil, err
}
config = configuration.ParseString(string(c))
return
}
func ListFilesByExtension(path string, extension string) []string {
var listOfFiles []string
err := filepath.WalkDir(path, func(s string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
if filepath.Ext(d.Name()) == extension {
listOfFiles = append(listOfFiles, s)
}
return nil
})
if err != nil {
log.Error("Couldn't list files in "+path+". ", err)
return nil
}
return listOfFiles
}