-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
63 lines (56 loc) · 1.26 KB
/
util.go
File metadata and controls
63 lines (56 loc) · 1.26 KB
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
61
62
63
package main
import (
"fmt"
"os"
"path/filepath"
)
func createFileList(files []os.FileInfo) []FileEntry {
size := len(files)
entryList := make([]FileEntry, size)
for i, v := range files {
entryList[i] = FileEntry{
Path: v.Name(),
Size: v.Size(),
Lastmod: v.ModTime(),
}
}
return entryList
}
func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func getDocumentDir() string {
documentDirName := "documents"
path, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
documentDir := filepath.Join(path, documentDirName)
return documentDir
}
func initDocumentDirectory(path string) error {
// TODO: Expand with permissions check.
fmt.Printf("INI Editor: Using document directory %s\n", path)
exists, err := fileExists(path)
if err != nil {
fmt.Printf("Failed to init document directory. Error: %s", err)
os.Exit(1)
}
if !exists {
fmt.Println("Document directory not found, attempting to create...")
err := os.Mkdir(path, os.ModePerm)
if err != nil {
fmt.Printf("Failed to created document directory. Error: %s", err)
}
}
fmt.Println("Successfully inited document directory.")
return nil
}