Skip to content

Commit 8341cf7

Browse files
committed
Merge branch 'feature-authors'
2 parents a744dc3 + 5262607 commit 8341cf7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1141
-1055
lines changed

cmd/multi/clone.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var cloneCommand = &cli.Command{
2323
Name: "branch",
2424
Aliases: []string{"b"},
2525
Usage: "Branch name",
26-
Value: "default",
26+
Value: "main",
2727
},
2828
&cli.IntFlag{
2929
Name: "limit",

cmd/multi/config.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
)
1313

1414
const (
15+
// DefaultBranch is the name of the default branch.
16+
DefaultBranch = "main"
1517
// ConfigFile is the name of the config file.
1618
ConfigFile = "multi.json"
1719
// IgnoreFile is the name of the ignore file.
@@ -38,7 +40,7 @@ type Config struct {
3840
// NewConfig returns a config with default settings.
3941
func NewConfig(root string) *Config {
4042
return &Config{
41-
Branch: "default",
43+
Branch: DefaultBranch,
4244
Path: filepath.Join(root, ConfigFile),
4345
Root: root,
4446
}

cmd/multi/daemon.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"path/filepath"
88

99
"github.com/ipfs/go-ds-badger2"
10-
"github.com/multiverse-vcs/go-multiverse/data"
11-
"github.com/multiverse-vcs/go-multiverse/key"
1210
"github.com/multiverse-vcs/go-multiverse/peer"
1311
"github.com/multiverse-vcs/go-multiverse/rpc"
1412
"github.com/multiverse-vcs/go-multiverse/web"
@@ -49,38 +47,33 @@ func daemonAction(c *cli.Context) error {
4947
}
5048

5149
dpath := filepath.Join(root, "datastore")
52-
dstore, err := badger.NewDatastore(dpath, &badger.DefaultOptions)
53-
if err != nil {
54-
return err
55-
}
50+
dopts := badger.DefaultOptions
5651

57-
kpath := filepath.Join(root, "keystore")
58-
kstore, err := key.NewKeystore(kpath)
52+
dstore, err := badger.NewDatastore(dpath, &dopts)
5953
if err != nil {
6054
return err
6155
}
6256

63-
key, err := kstore.DefaultKey()
57+
config, err := peer.OpenConfig(root)
6458
if err != nil {
6559
return err
6660
}
6761

68-
client, err := peer.New(c.Context, dstore, key)
62+
node, err := peer.New(c.Context, dstore, config)
6963
if err != nil {
7064
return err
7165
}
7266

73-
peerId, err := client.PeerID()
74-
if err != nil {
67+
// ensure any changes made offline will be published
68+
if err := node.Authors().Publish(c.Context); err != nil {
7569
return err
7670
}
7771

78-
store := data.NewStore(dstore)
79-
go web.ListenAndServe(client, store)
80-
go rpc.ListenAndServe(client, store)
72+
go web.ListenAndServe(node)
73+
go rpc.ListenAndServe(node)
8174

8275
fmt.Printf(daemonBanner)
83-
fmt.Printf("Peer ID: %s\n", peerId.Pretty())
76+
fmt.Printf("Peer ID: %s\n", node.PeerID().Pretty())
8477
fmt.Printf("Web URL: %s\n", web.BindAddr)
8578

8679
quit := make(chan os.Signal, 1)

cmd/multi/import.go

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"errors"
5-
"os"
64
"path/filepath"
75

86
"github.com/multiverse-vcs/go-multiverse/rpc"
@@ -39,17 +37,6 @@ func importAction(c *cli.Context) error {
3937
cli.ShowSubcommandHelpAndExit(c, 1)
4038
}
4139

42-
name := c.Args().Get(0)
43-
44-
cwd, err := os.Getwd()
45-
if err != nil {
46-
return err
47-
}
48-
49-
if _, err := FindConfig(cwd); err == nil {
50-
return errors.New("repo already exists")
51-
}
52-
5340
dir, err := filepath.Abs(c.String("dir"))
5441
if err != nil {
5542
return err
@@ -61,7 +48,7 @@ func importAction(c *cli.Context) error {
6148
}
6249

6350
args := rpc.ImportArgs{
64-
Name: name,
51+
Name: c.Args().Get(0),
6552
Type: c.String("type"),
6653
URL: c.String("url"),
6754
Dir: dir,
@@ -72,7 +59,5 @@ func importAction(c *cli.Context) error {
7259
return err
7360
}
7461

75-
config := NewConfig(cwd)
76-
config.Name = name
77-
return config.Save()
62+
return nil
7863
}

cmd/multi/init.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@ var initCommand = &cli.Command{
1313
Name: "init",
1414
Usage: "Create a repo",
1515
ArgsUsage: "<name>",
16+
Flags: []cli.Flag{
17+
&cli.StringFlag{
18+
Name: "branch",
19+
Aliases: []string{"b"},
20+
Usage: "Default branch",
21+
Value: DefaultBranch,
22+
},
23+
},
1624
}
1725

1826
func initAction(c *cli.Context) error {
1927
if c.NArg() < 1 {
2028
cli.ShowSubcommandHelpAndExit(c, 1)
2129
}
2230

23-
name := c.Args().Get(0)
24-
2531
cwd, err := os.Getwd()
2632
if err != nil {
2733
return err
@@ -37,7 +43,8 @@ func initAction(c *cli.Context) error {
3743
}
3844

3945
args := rpc.InitArgs{
40-
Name: name,
46+
Name: c.Args().Get(0),
47+
Branch: c.String("branch"),
4148
}
4249

4350
var reply rpc.InitReply
@@ -46,6 +53,7 @@ func initAction(c *cli.Context) error {
4653
}
4754

4855
config := NewConfig(cwd)
49-
config.Name = name
56+
config.Name = args.Name
57+
config.Branch = args.Branch
5058
return config.Save()
5159
}

data/author.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package data
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
7+
"github.com/ipfs/go-cid"
8+
cbornode "github.com/ipfs/go-ipld-cbor"
9+
ipld "github.com/ipfs/go-ipld-format"
10+
"github.com/multiformats/go-multihash"
11+
)
12+
13+
// Author contains info about a user.
14+
type Author struct {
15+
// Repositories is a map of repositories.
16+
Repositories map[string]cid.Cid `json:"repositories"`
17+
// Metadata contains additional data.
18+
Metadata map[string]string `json:"metadata"`
19+
}
20+
21+
// GetAuthor returns the author with the given CID.
22+
func GetAuthor(ctx context.Context, dag ipld.DAGService, id cid.Cid) (*Author, error) {
23+
node, err := dag.Get(ctx, id)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
return AuthorFromCBOR(node.RawData())
29+
}
30+
31+
// AddAuthor adds a author to the given dag.
32+
func AddAuthor(ctx context.Context, dag ipld.DAGService, author *Author) (cid.Cid, error) {
33+
node, err := cbornode.WrapObject(author, multihash.SHA2_256, -1)
34+
if err != nil {
35+
return cid.Cid{}, err
36+
}
37+
38+
if err := dag.Add(ctx, node); err != nil {
39+
return cid.Cid{}, err
40+
}
41+
42+
return node.Cid(), nil
43+
}
44+
45+
// AuthorFromJSON decodes a author from json.
46+
func AuthorFromJSON(data []byte) (*Author, error) {
47+
var author Author
48+
if err := json.Unmarshal(data, &author); err != nil {
49+
return nil, err
50+
}
51+
52+
return &author, nil
53+
}
54+
55+
// AuthorFromCBOR decodes a author from an ipld node.
56+
func AuthorFromCBOR(data []byte) (*Author, error) {
57+
var author Author
58+
if err := cbornode.DecodeInto(data, &author); err != nil {
59+
return nil, err
60+
}
61+
62+
return &author, nil
63+
}
64+
65+
// NewAuthor returns a new author.
66+
func NewAuthor() *Author {
67+
return &Author{
68+
Repositories: make(map[string]cid.Cid),
69+
Metadata: make(map[string]string),
70+
}
71+
}

data/commit.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77

88
"github.com/ipfs/go-cid"
9-
"github.com/ipfs/go-ipld-cbor"
9+
cbornode "github.com/ipfs/go-ipld-cbor"
1010
ipld "github.com/ipfs/go-ipld-format"
1111
"github.com/multiformats/go-multihash"
1212
)
@@ -49,7 +49,7 @@ func AddCommit(ctx context.Context, dag ipld.DAGService, commit *Commit) (cid.Ci
4949
return node.Cid(), nil
5050
}
5151

52-
// CommitFromJON decodes a commit from json.
52+
// CommitFromJSON decodes a commit from json.
5353
func CommitFromJSON(data []byte) (*Commit, error) {
5454
var commit Commit
5555
if err := json.Unmarshal(data, &commit); err != nil {

data/data.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var timeAtlasEntry = atlas.BuildEntry(time.Time{}).
2323

2424
func init() {
2525
cbornode.RegisterCborType(timeAtlasEntry)
26+
cbornode.RegisterCborType(Author{})
2627
cbornode.RegisterCborType(Commit{})
28+
cbornode.RegisterCborType(Record{})
2729
cbornode.RegisterCborType(Repository{})
2830
}

data/record.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package data
2+
3+
import (
4+
cbornode "github.com/ipfs/go-ipld-cbor"
5+
)
6+
7+
// Record contains named record info.
8+
type Record struct {
9+
// Payload contains the record data.
10+
Payload []byte
11+
// Sequence is a version identifier.
12+
Sequence uint64
13+
// Signature is a signature of the payload.
14+
Signature []byte
15+
}
16+
17+
// RecordFromCBOR decodes a record from an ipld node.
18+
func RecordFromCBOR(data []byte) (*Record, error) {
19+
var rec Record
20+
if err := cbornode.DecodeInto(data, &rec); err != nil {
21+
return nil, err
22+
}
23+
24+
return &rec, nil
25+
}
26+
27+
// NewRecord returns a record containing the given payload.
28+
func NewRecord(payload []byte, sequence uint64, signature []byte) *Record {
29+
return &Record{
30+
Payload: payload,
31+
Sequence: sequence,
32+
Signature: signature,
33+
}
34+
}

data/repository.go

+4-46
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@ import (
55
"encoding/json"
66

77
"github.com/ipfs/go-cid"
8-
"github.com/ipfs/go-ipfs-pinner"
9-
"github.com/ipfs/go-ipld-cbor"
8+
cbornode "github.com/ipfs/go-ipld-cbor"
109
ipld "github.com/ipfs/go-ipld-format"
11-
"github.com/libp2p/go-libp2p-core/peer"
1210
"github.com/multiformats/go-multihash"
1311
)
1412

1513
// Repository contains all versions of a project.
1614
type Repository struct {
17-
// Author is the peer id of the author.
18-
Author peer.ID `json:"author"`
19-
// Name is the human friendly name of the repo.
20-
Name string `json:"name"`
21-
// Description describes the project.
22-
Description string `json:"description"`
15+
// DefaultBranch is the base branch of the repo.
16+
DefaultBranch string `json:"default_branch"`
2317
// Branches is a map of names to commit CIDs.
2418
Branches map[string]cid.Cid `json:"branches"`
2519
// Tags is a map of names to commit CIDs.
@@ -52,20 +46,6 @@ func AddRepository(ctx context.Context, dag ipld.DAGService, repo *Repository) (
5246
return node.Cid(), nil
5347
}
5448

55-
// PinRepository pins a repo using the given pinner.
56-
func PinRepository(ctx context.Context, pinner pin.Pinner, repo *Repository) (cid.Cid, error) {
57-
node, err := cbornode.WrapObject(repo, multihash.SHA2_256, -1)
58-
if err != nil {
59-
return cid.Cid{}, err
60-
}
61-
62-
if err := pinner.Pin(ctx, node, true); err != nil {
63-
return cid.Cid{}, err
64-
}
65-
66-
return node.Cid(), nil
67-
}
68-
6949
// RepositoryFromJSON decodes a repo from json.
7050
func RepositoryFromJSON(data []byte) (*Repository, error) {
7151
var repo Repository
@@ -87,32 +67,10 @@ func RepositoryFromCBOR(data []byte) (*Repository, error) {
8767
}
8868

8969
// NewRepository returns a new repo.
90-
func NewRepository(name string) *Repository {
70+
func NewRepository() *Repository {
9171
return &Repository{
92-
Name: name,
9372
Branches: make(map[string]cid.Cid),
9473
Tags: make(map[string]cid.Cid),
9574
Metadata: make(map[string]string),
9675
}
9776
}
98-
99-
// DefaultBranch returns the default branch of the repo.
100-
func (r *Repository) DefaultBranch() string {
101-
for branch := range r.Branches {
102-
return branch
103-
}
104-
return ""
105-
}
106-
107-
// Ref returns the cid of the given ref.
108-
func (r *Repository) Ref(ref string) (cid.Cid, error) {
109-
if id, ok := r.Branches[ref]; ok {
110-
return id, nil
111-
}
112-
113-
if id, ok := r.Tags[ref]; ok {
114-
return id, nil
115-
}
116-
117-
return cid.Parse(ref)
118-
}

0 commit comments

Comments
 (0)