/
local.go
123 lines (103 loc) · 3.31 KB
/
local.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
package provider
import (
"github.com/analogj/go-util/utils"
"github.com/package-url/packageurl-go"
fErrors "github.com/packagrio/fetchr/pkg/errors"
"github.com/packagrio/fetchr/pkg/models"
"log"
"os"
"path/filepath"
)
type LocalProvider struct {
*BaseProvider
Name string
}
func DefaultLocalProvider() (*LocalProvider, error) {
return NewLocalProvider("default")
}
func NewLocalProvider(providerName string) (*LocalProvider, error) {
p := LocalProvider{
Name: providerName,
}
//this must be "default"
p.Name = providerName
return &p, p.ValidateAuth()
}
func (p *LocalProvider) Close() {}
func (p *LocalProvider) ValidateAuth() error {
return nil
}
func (p *LocalProvider) IsAuthenticatedProvider() bool {
return false
}
func (p *LocalProvider) IsSupportedArtifactType(actionType string, artifactType string) bool {
if actionType == ActionSetMetadata {
return false
}
return artifactType == "local"
}
func (p *LocalProvider) ArtifactSearch(artifactPurl *packageurl.PackageURL) ([]*models.QueryResult, error) {
if !p.IsSupportedArtifactType(ActionSearch, artifactPurl.Type) {
return nil, fErrors.NewProviderUnsupportedActionError(ActionSearch, artifactPurl.Type)
}
artifactPath := artifactPurl.Subpath
//check if destination is a path or a file.
if artifactPath[len(artifactPath)-1:] == "/" {
log.Printf("source is a directory, listing recursively ")
queryResults := []*models.QueryResult{}
err := filepath.Walk(artifactPath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
newArtifactPurl := NewLocalPackageUrl(path)
//todo calculate checksums and store as qualifiers or in the queryResults
queryResults = append(queryResults, &models.QueryResult{
ArtifactPurl: newArtifactPurl.String(),
Size: info.Size(),
})
}
return nil
})
if err != nil {
return nil, err
}
return queryResults, nil
} else {
log.Print("source is a file, searching")
if utils.FileExists(artifactPath) {
return []*models.QueryResult{{ArtifactPurl: artifactPurl.String()}}, nil
} else {
return nil, fErrors.NewFileValidationError("file does not exist at path", artifactPath)
}
}
}
func (p *LocalProvider) ArtifactDownload(downloadFolderPath string, artifactPurl *packageurl.PackageURL) ([]string, []*packageurl.PackageURL, error) {
if !p.IsSupportedArtifactType(ActionDownload, artifactPurl.Type) {
return nil, nil, fErrors.NewProviderUnsupportedActionError(ActionSearch, artifactPurl.Type)
}
return nil, nil, nil
}
func (p *LocalProvider) ArtifactUpload(artifactCachePath string, sourceArtifactPurl *packageurl.PackageURL, destArtifactPurl *packageurl.PackageURL) error {
if !p.IsSupportedArtifactType(ActionUpload, destArtifactPurl.Type) {
return fErrors.NewProviderUnsupportedActionError(ActionSearch, destArtifactPurl.Type)
}
return nil
}
func (p *LocalProvider) ArtifactSetMetadata(metadata map[string]string, destArtifactPurl *packageurl.PackageURL) error {
if !p.IsSupportedArtifactType(ActionSetMetadata, destArtifactPurl.Type) {
return fErrors.NewProviderUnsupportedActionError(ActionSetMetadata, destArtifactPurl.Type)
}
return nil
}
func NewLocalPackageUrl(localPath string) *packageurl.PackageURL {
return packageurl.NewPackageURL(
"local",
"",
"",
"",
nil,
localPath,
)
}