forked from berty/go-orbit-db
/
create_db_manifest.go
48 lines (40 loc) · 1.33 KB
/
create_db_manifest.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
package utils
import (
"context"
"fmt"
"path"
cid "github.com/ipfs/go-cid"
cbornode "github.com/ipfs/go-ipld-cbor"
coreiface "github.com/ipfs/kubo/core/coreiface"
"github.com/polydawn/refmt/obj/atlas"
"github.com/thusharprakash/go-ipfs-log/io"
)
// Manifest defines a database manifest describing its type and access controller
type Manifest struct {
Name string
Type string
AccessController string
}
// CreateDBManifest creates a new database manifest and saves it on IPFS
func CreateDBManifest(ctx context.Context, ipfs coreiface.CoreAPI, name string, dbType string, accessControllerAddress string) (cid.Cid, error) {
manifest := &Manifest{
Name: name,
Type: dbType,
AccessController: path.Join("/ipfs", accessControllerAddress),
}
c, err := io.WriteCBOR(ctx, ipfs, manifest, nil)
if err != nil {
return cid.Cid{}, fmt.Errorf("unable to write cbor data: %w", err)
}
return c, err
}
// AtlasManifest defines how a manifest is serialized
var AtlasManifest = atlas.BuildEntry(Manifest{}).
StructMap().
AddField("Name", atlas.StructMapEntry{SerialName: "name"}).
AddField("Type", atlas.StructMapEntry{SerialName: "type"}).
AddField("AccessController", atlas.StructMapEntry{SerialName: "access_controller"}).
Complete()
func init() {
cbornode.RegisterCborType(AtlasManifest)
}