/
init.go
35 lines (28 loc) · 958 Bytes
/
init.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
// Initialize the timesheet.
package timesheet
import (
"fmt"
"os"
"github.com/JosephLai241/shift/utils"
)
// Initialize the directories in which the timesheet will be stored.
// Returns a string denoting the path to the timesheet directory.
func InitializeDirectories() string {
cwd := utils.GetCWD()
timesheetDirectory := fmt.Sprintf("%s/shifts/%s", cwd, utils.CurrentYear)
os.MkdirAll(timesheetDirectory, os.ModePerm)
return timesheetDirectory
}
// If the file does not already exist, create a new timesheet with column headers.
// Returns *os.File for writing.
func InitializeTimesheet(timesheetPath string) *os.File {
var file *os.File
if _, err := os.Stat(timesheetPath); err == nil {
file, _ = os.OpenFile(timesheetPath, os.O_RDWR, 0755)
utils.CheckError("Could not open timesheet", err)
} else if os.IsNotExist(err) {
file, err = os.Create(timesheetPath)
utils.CheckError("Could not create timesheet", err)
}
return file
}