-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileProcessor.go
47 lines (35 loc) · 1.05 KB
/
FileProcessor.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
package src
import (
"fmt"
"io/ioutil"
"os"
"github.com/mitchellh/go-homedir"
)
const LogDirectory = ".worktime/"
const LogPath = "worktime.log"
type FileProcessor struct {
ErrorHandler *ErrorHandler
}
func (fileProcessor *FileProcessor) OpenFile() *os.File {
logDirectory := fileProcessor.getLogDirectory()
logPath := fileProcessor.GetFilePath()
var _, err = os.Stat(logPath)
if os.IsNotExist(err) {
fmt.Println("Log file doesn't exist. Creating new one at", logPath)
}
os.MkdirAll(logDirectory, 0777)
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
fileProcessor.ErrorHandler.Check(err)
return file
}
func (fileProcessor *FileProcessor) getLogDirectory() string {
homeDirectory, _ := homedir.Dir()
return homeDirectory + "/" + LogDirectory
}
func (fileProcessor *FileProcessor) GetFilePath() string {
return fileProcessor.getLogDirectory() + LogPath
}
func (fileProcessor *FileProcessor) ClearLogFile() {
err := ioutil.WriteFile(fileProcessor.GetFilePath(), []byte(""), 0644)
fileProcessor.ErrorHandler.Check(err)
}