This repository was archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclient.go
More file actions
69 lines (60 loc) · 1.89 KB
/
client.go
File metadata and controls
69 lines (60 loc) · 1.89 KB
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
package tuf
import (
"io"
"github.com/pkg/errors"
)
// Client is a TUF client.
type Client struct {
// Client wraps the private repoMan type which contains the actual
// methods for working with TUF repositories. In the future it might
// be worthwile to export the repoMan type as Client instead, but
// wrapping it reduces the amount of present refactoring work.
manager *repoMan
}
func NewClient(settings *Settings) (*Client, error) {
if settings.MaxResponseSize == 0 {
settings.MaxResponseSize = defaultMaxResponseSize
}
// check to see if Notary server is available
notary, err := newNotaryRepo(settings)
if err != nil {
return nil, errors.Wrap(err, "creating notary client")
}
err = notary.ping()
if err != nil {
return nil, errors.Wrap(err, "pinging notary server failed")
}
localRepo, err := newLocalRepo(settings.LocalRepoPath)
if err != nil {
return nil, errors.New("creating local tuf role repo")
}
// store intermediate state until all validation succeeds, then write
// changed roles to non-volitile storage
manager := newRepoMan(localRepo, notary, settings, notary.client)
return &Client{manager: manager}, nil
}
func (c *Client) Update() (files map[string]FileIntegrityMeta, latest bool, err error) {
latest, err = c.manager.refresh()
if err != nil {
return nil, latest, errors.Wrap(err, "refreshing state")
}
if err := c.manager.save(getTag()); err != nil {
return nil, latest, errors.Wrap(err, "unable to save tuf repo state")
}
files = c.manager.getLocalTargets()
return files, latest, nil
}
func (c *Client) Download(targetName string, destination io.Writer) error {
files := c.manager.getLocalTargets()
fim, ok := files[targetName]
if !ok {
return errNoSuchTarget
}
if err := c.manager.downloadTarget(targetName, &fim, destination); err != nil {
return errors.Wrap(err, "downloading target")
}
return nil
}
func (c *Client) Stop() {
c.manager.Stop()
}