-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wagon_identity.go
124 lines (97 loc) · 3.59 KB
/
wagon_identity.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Copyright © 2022 Macaroni OS Linux
See AUTHORS and LICENSE for the license details and contributors.
*/
package repository
import (
"fmt"
"io/ioutil"
"github.com/geaaru/luet/pkg/config"
artifact "github.com/geaaru/luet/pkg/v2/compiler/types/artifact"
compression "github.com/geaaru/luet/pkg/v2/compiler/types/compression"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
type WagonDocument struct {
FileName string `json:"filename" yaml:"filename"`
CompressionType compression.Implementation `json:"compressiontype,omitempty" yaml:"compressiontype,omitempty"`
Checksums artifact.Checksums `json:"checksums,omitempty" yaml:"checksums,omitempty"`
}
type WagonIdentity struct {
*config.LuetRepository `yaml:",inline" json:",inline"`
IdentityFile string `yaml:-" json:"-"`
RepositoryFiles map[string]*WagonDocument `yaml:"repo_files,omitempty" json:"repo_files,omitempty"`
}
func NewWagonIdentify(l *config.LuetRepository) *WagonIdentity {
return &WagonIdentity{
LuetRepository: l,
}
}
func (w *WagonIdentity) Valid() bool {
_, hasMeta := w.RepositoryFiles[REPOFILE_META_KEY]
if !hasMeta {
return false
}
_, hasTree := w.RepositoryFiles[REPOFILE_TREE_KEY]
if !hasTree {
return false
}
return true
}
func (w *WagonIdentity) GetTreePath() string { return w.LuetRepository.TreePath }
func (w *WagonIdentity) GetMetaPath() string { return w.LuetRepository.MetaPath }
func (w *WagonIdentity) GetName() string { return w.LuetRepository.Name }
func (w *WagonIdentity) GetLastUpdate() string { return w.LuetRepository.LastUpdate }
func (w *WagonIdentity) GetRevision() int { return w.LuetRepository.Revision }
func (w *WagonIdentity) GetType() string { return w.LuetRepository.Type }
func (w *WagonIdentity) GetVerify() bool { return w.LuetRepository.Verify }
func (w *WagonIdentity) GetUrls() []string { return w.LuetRepository.Urls }
func (w *WagonIdentity) SetLastUpdate(u string) { w.LuetRepository.LastUpdate = u }
func (w *WagonIdentity) SetType(p string) { w.LuetRepository.Type = p }
func (w *WagonIdentity) SetVerify(p bool) { w.LuetRepository.Verify = p }
func (w *WagonIdentity) GetAuthentication() map[string]string {
return w.LuetRepository.Authentication
}
func (w *WagonIdentity) IncrementRevision() {
w.LuetRepository.Revision++
}
func (w *WagonIdentity) Load(f string) error {
//previousName := w.LuetRepository.Name
data, err := ioutil.ReadFile(f)
if err != nil {
return errors.Wrap(err, "Error on reading file "+f)
}
err = yaml.Unmarshal(data, w)
if err != nil {
return err
}
w.IdentityFile = f
return nil
}
func (w *WagonIdentity) Is2Update(newIdentity *WagonIdentity) bool {
ans := true
if w.GetRevision() == newIdentity.GetRevision() &&
w.GetLastUpdate() == newIdentity.GetLastUpdate() {
ans = false
}
return ans
}
func (w *WagonIdentity) DownloadDocument(c Client, key string) (*artifact.PackageArtifact, error) {
docFile, ok := w.RepositoryFiles[key]
if !ok {
return nil, errors.New(fmt.Sprintf("key %s not present in the repository", key))
}
downloadedFile, err := c.DownloadFile(docFile.GetFileName())
if err != nil {
return nil, errors.Wrap(err, "While downloading "+docFile.GetFileName())
}
docArtifact := artifact.NewPackageArtifact(downloadedFile)
docArtifact.CachePath = downloadedFile
docArtifact.Checksums = docFile.GetChecksums()
docArtifact.CompressionType = docFile.GetCompressionType()
err = docArtifact.Verify()
if err != nil {
return nil, errors.Wrap(err, "file integrity check failure for repository "+w.GetName())
}
return docArtifact, nil
}