-
Notifications
You must be signed in to change notification settings - Fork 0
/
handleFile.go
60 lines (50 loc) · 1.32 KB
/
handleFile.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
package main
import (
"io"
"log/slog"
"os"
"path"
"path/filepath"
"gopkg.in/ini.v1"
)
type FileHandler struct{}
func NewFileHandler() FileHandler {
return FileHandler{}
}
func (F *FileHandler) OpenDezzFile(filePath string) (io.ReadWriteCloser, error) {
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
slog.Error("[ERROR]: Cannot Open the file", "[DETAILS]: ", err.Error())
return nil, err
}
return file, err
}
func (F *FileHandler) OpenDezzReadFile(filePath string) (io.ReadCloser, error) {
file, err := os.OpenFile(filePath, os.O_RDONLY, os.ModePerm)
if err != nil {
slog.Error("[ERROR]: Cannot Open the file", "[DETAILS]: ", err.Error())
return nil, err
}
return file, err
}
func (F *FileHandler) GetExecutablePath() (string, error) {
exPath, err := os.Executable()
if err != nil {
slog.Error("Unable to get executable path", "Details", err.Error())
return "", err
}
return exPath, nil
}
func (F *FileHandler) OpenConfigFile(configPath string) (*ini.File, error) {
return ini.Load(configPath)
}
func (F *FileHandler) GetBashScriptPath() string {
fh := NewFileHandler()
exPath, err := fh.GetExecutablePath()
if err != nil {
slog.Error("Executable path not found", "Details", err.Error())
os.Exit(1)
}
exPath = path.Join(filepath.Dir(exPath), "cdbash.sh")
return exPath
}