/
main.go
86 lines (71 loc) · 1.7 KB
/
main.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
86
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
config "github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/plugin/loader"
"github.com/ipfs/kubo/repo/fsrepo"
)
func setupPlugins(externalPluginsPath string) error {
// Load any external plugins if available on externalPluginsPath
plugins, err := loader.NewPluginLoader(filepath.Join(externalPluginsPath, "plugins"))
if err != nil {
return fmt.Errorf("error loading plugins: %s", err)
}
// Load preloaded and external plugins
if err := plugins.Initialize(); err != nil {
return fmt.Errorf("error initializing plugins: %s", err)
}
if err := plugins.Inject(); err != nil {
return fmt.Errorf("error initializing plugins: %s", err)
}
return nil
}
var rootKey = datastore.NewKey("/local/filesroot")
func main() {
ctx := context.Background()
defaultPath, err := config.PathRoot()
if err != nil {
log.Fatal(err)
}
if err := setupPlugins(defaultPath); err != nil {
log.Fatal(err)
}
repo, err := fsrepo.Open(defaultPath)
if err != nil {
log.Fatal(err)
}
defer repo.Close()
ds := repo.Datastore()
root, err := ds.Get(ctx, rootKey)
if err == datastore.ErrNotFound {
fmt.Println("empty MFS root")
} else if err != nil {
log.Fatal(err)
} else {
c, err := cid.Cast(root)
if err != nil {
log.Fatal(err)
}
fmt.Printf("MFS Root: %s\n", c)
}
if len(os.Args) < 2 {
log.Println("No new MFS root provided")
return
}
fmt.Println("Updating MFS root")
newRoot, err := cid.Parse(os.Args[1])
if err != nil {
log.Fatal(err)
}
err = ds.Put(ctx, rootKey, newRoot.Bytes())
if err != nil {
log.Fatal(err)
}
fmt.Printf("MFS root updated to %s\n", newRoot)
}