-
Notifications
You must be signed in to change notification settings - Fork 6
/
repo.go
85 lines (73 loc) · 1.79 KB
/
repo.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
80
81
82
83
84
85
package mobile
import (
"fmt"
"os"
"path/filepath"
config "github.com/ipfs/kubo/config"
serialize "github.com/ipfs/kubo/config/serialize"
)
func initConfig(path string, conf *config.Config) error {
if configIsInitialized(path) {
return nil
}
configFilename, err := config.Filename(path, "")
if err != nil {
return err
}
// initialization is the one time when it's okay to write to the config
// without reading the config from disk and merging any user-provided keys
// that may exist.
if err := serialize.WriteConfigFile(configFilename, conf); err != nil {
return err
}
return nil
}
func checkWritable(dir string) error {
_, err := os.Stat(dir)
if err == nil {
// dir exists, make sure we can write to it
testfile := filepath.Join(dir, "test")
fi, err := os.Create(testfile)
if err != nil {
if os.IsPermission(err) {
return fmt.Errorf("%s is not writeable by the current user", dir)
}
return fmt.Errorf("unexpected error while checking writeablility of repo root: %s", err)
}
fi.Close()
return os.Remove(testfile)
}
if os.IsNotExist(err) {
// dir doesn't exist, check that we can create it
return os.Mkdir(dir, 0775)
}
if os.IsPermission(err) {
return fmt.Errorf("cannot write to %s, incorrect permissions", err)
}
return err
}
func configIsInitialized(path string) bool {
configFilename, err := config.Filename(path, "")
if err != nil {
return false
}
if fileExists(configFilename) {
return true
}
return false
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func openConfig(path string) (*config.Config, error) {
configFilename, err := config.Filename(path, "")
if err != nil {
return nil, err
}
conf, err := serialize.Load(configFilename)
if err != nil {
return nil, err
}
return conf, err
}