/
delete.go
46 lines (36 loc) · 980 Bytes
/
delete.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
package repo
import (
"context"
"errors"
"github.com/multiverse-vcs/go-multiverse/internal/p2p"
"github.com/multiverse-vcs/go-multiverse/pkg/object"
)
// DeleteArgs contains the args.
type DeleteArgs struct {
// Name is the repository name.
Name string `json:"name"`
}
// DeleteReply contains the reply
type DeleteReply struct{}
// Delete delete an existing repository.
func (s *Service) Delete(args *DeleteArgs, reply *DeleteReply) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
key, err := p2p.DecodeKey(s.Config.PrivateKey)
if err != nil {
return err
}
author := s.Config.Author
if _, ok := author.Repositories[args.Name]; !ok {
return errors.New("repository does not exist")
}
delete(author.Repositories, args.Name)
if err := s.Config.Write(); err != nil {
return err
}
authorID, err := object.AddAuthor(ctx, s.Peer.DAG, author)
if err != nil {
return err
}
return s.Namesys.Publish(ctx, key, authorID)
}