This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
72 lines (58 loc) · 1.55 KB
/
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
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
package repo
import (
config "github.com/ipfs/go-ipfs-config"
"github.com/spf13/afero"
)
const specFn = "datastore_spec"
// Init initializes a new FSRepo at the given path with the provided config.
// TODO add support for custom datastores.
func Init(fs afero.Fs, repoPath string, conf *config.Config) error {
// packageLock must be held to ensure that the repo is not initialized more
// than once.
packageLock.Lock()
defer packageLock.Unlock()
if isInitializedUnsynced(fs, repoPath) {
return nil
}
if err := initConfig(fs, repoPath, conf); err != nil {
return err
}
if err := initSpec(fs, repoPath, conf.Datastore.Spec); err != nil {
return err
}
if err := WriteRepoVersion(fs, repoPath, RepoVersion); err != nil {
return err
}
return nil
}
func initConfig(fs afero.Fs, path string, conf *config.Config) error {
if configIsInitialized(fs, 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 := WriteConfigFile(fs, configFilename, conf); err != nil {
return err
}
return nil
}
func initSpec(fs afero.Fs, path string, conf map[string]interface{}) error {
fn, err := config.Path(path, specFn)
if err != nil {
return err
}
if FileExists(fs, fn) {
return nil
}
dsc, err := AnyDatastoreConfig(conf)
if err != nil {
return err
}
bytes := dsc.DiskSpec().Bytes()
return afero.WriteFile(fs, fn, bytes, 0600)
}