Skip to content

Commit d7373fd

Browse files
committed
Add author struct. Add peer config.
1 parent 64ebaea commit d7373fd

24 files changed

+458
-446
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

+5-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/ipfs/go-ds-badger2"
1010
"github.com/multiverse-vcs/go-multiverse/data"
11-
"github.com/multiverse-vcs/go-multiverse/key"
1211
"github.com/multiverse-vcs/go-multiverse/peer"
1312
"github.com/multiverse-vcs/go-multiverse/rpc"
1413
"github.com/multiverse-vcs/go-multiverse/web"
@@ -49,28 +48,19 @@ func daemonAction(c *cli.Context) error {
4948
}
5049

5150
dpath := filepath.Join(root, "datastore")
52-
dstore, err := badger.NewDatastore(dpath, &badger.DefaultOptions)
53-
if err != nil {
54-
return err
55-
}
56-
57-
kpath := filepath.Join(root, "keystore")
58-
kstore, err := key.NewKeystore(kpath)
59-
if err != nil {
60-
return err
61-
}
51+
dopts := badger.DefaultOptions
6252

63-
key, err := kstore.DefaultKey()
53+
dstore, err := badger.NewDatastore(dpath, &dopts)
6454
if err != nil {
6555
return err
6656
}
6757

68-
client, err := peer.New(c.Context, dstore, key)
58+
config, err := peer.LoadConfig(root)
6959
if err != nil {
7060
return err
7161
}
7262

73-
peerId, err := client.PeerID()
63+
client, err := peer.New(c.Context, dstore, config)
7464
if err != nil {
7565
return err
7666
}
@@ -80,7 +70,7 @@ func daemonAction(c *cli.Context) error {
8070
go rpc.ListenAndServe(client, store)
8171

8272
fmt.Printf(daemonBanner)
83-
fmt.Printf("Peer ID: %s\n", peerId.Pretty())
73+
fmt.Printf("Peer ID: %s\n", client.PeerID().Pretty())
8474
fmt.Printf("Web URL: %s\n", web.BindAddr)
8575

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

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

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

data/data.go

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

2424
func init() {
2525
cbornode.RegisterCborType(timeAtlasEntry)
26+
cbornode.RegisterCborType(Author{})
2627
cbornode.RegisterCborType(Commit{})
2728
cbornode.RegisterCborType(Repository{})
2829
}

data/repository.go

+2-24
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ import (
88
"github.com/ipfs/go-ipfs-pinner"
99
"github.com/ipfs/go-ipld-cbor"
1010
ipld "github.com/ipfs/go-ipld-format"
11-
"github.com/libp2p/go-libp2p-core/peer"
1211
"github.com/multiformats/go-multihash"
1312
)
1413

1514
// Repository contains all versions of a project.
1615
type Repository struct {
17-
// Author is the peer id of the author.
18-
Author peer.ID `json:"author"`
1916
// Name is the human friendly name of the repo.
2017
Name string `json:"name"`
18+
// DefaultBranch is the base branch of the repo.
19+
DefaultBranch string `json:"default_branch"`
2120
// Description describes the project.
2221
Description string `json:"description"`
2322
// Branches is a map of names to commit CIDs.
@@ -95,24 +94,3 @@ func NewRepository(name string) *Repository {
9594
Metadata: make(map[string]string),
9695
}
9796
}
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-
}

key/key.go

-87
This file was deleted.

p2p/key.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package p2p
2+
3+
import (
4+
"github.com/libp2p/go-libp2p-core/crypto"
5+
)
6+
7+
// DefaultKeyType is the default private key type.
8+
const DefaultKeyType = crypto.Ed25519
9+
10+
// GenerateKey returns a new private key.
11+
func GenerateKey() (crypto.PrivKey, error) {
12+
priv, _, err := crypto.GenerateKeyPair(DefaultKeyType, -1)
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
return priv, nil
18+
}
19+
20+
// EncodeKey returns a base64 encoded version of the key.
21+
func EncodeKey(priv crypto.PrivKey) (string, error) {
22+
data, err := crypto.MarshalPrivateKey(priv)
23+
if err != nil {
24+
return "", err
25+
}
26+
27+
return crypto.ConfigEncodeKey(data), nil
28+
}
29+
30+
// DecodeKey returns a key from a base64 encoded version.
31+
func DecodeKey(encoded string) (crypto.PrivKey, error) {
32+
data, err := crypto.ConfigDecodeKey(encoded)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
return crypto.UnmarshalPrivateKey(data)
38+
}

0 commit comments

Comments
 (0)