forked from dakyskye/dxhd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
79 lines (65 loc) · 1.13 KB
/
config.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
)
func getDefaultConfigPath() (file, directory string, err error) {
configDirPath, err := os.UserConfigDir()
if err != nil {
return
}
directory = filepath.Join(configDirPath, "dxhd")
file = filepath.Join(directory, "dxhd.sh")
return
}
func isPathToConfigValid(path string) (isValid bool, err error) {
stat, err := os.Stat(path)
if err != nil {
return
}
if !stat.Mode().IsRegular() {
err = errors.New(fmt.Sprintf("%s is not a regular file", path))
return
}
isValid = true
return
}
func createDefaultConfig() (err error) {
var (
file, directory string
)
file, directory, err = getDefaultConfigPath()
if err != nil {
return
}
_, err = os.Stat(directory)
if err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(directory, 0744)
if err != nil {
return
}
} else {
return
}
}
_, err = os.Stat(file)
if os.IsNotExist(err) {
var f *os.File
f, err = os.Create(file)
if err != nil {
return
}
_, err = f.WriteString("#!/bin/sh\n")
if err != nil {
return
}
err = f.Close()
if err != nil {
return
}
}
return
}