-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmanifests.go
More file actions
149 lines (130 loc) · 4.08 KB
/
Copy pathmanifests.go
File metadata and controls
149 lines (130 loc) · 4.08 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Package manifests parses dependency manifest and lockfile formats across package ecosystems.
//
// It supports 40+ ecosystems including npm, gem, pypi, cargo, maven, and more.
// Each ecosystem uses its PURL type as the identifier.
//
// Basic usage:
//
// result, err := manifests.Parse("package.json", content)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Ecosystem: %s, Kind: %s\n", result.Ecosystem, result.Kind)
// for _, dep := range result.Dependencies {
// fmt.Printf(" %s %s\n", dep.Name, dep.Version)
// }
package manifests
import (
"github.com/git-pkgs/manifests/internal/core"
"github.com/git-pkgs/purl"
)
// Re-export types from internal/core for public API.
type (
Kind = core.Kind
Scope = core.Scope
Dependency = core.Dependency
)
// Re-export constants.
const (
Manifest Kind = core.Manifest
Lockfile Kind = core.Lockfile
Supplement Kind = core.Supplement
Runtime Scope = core.Runtime
Development Scope = core.Development
Test Scope = core.Test
Build Scope = core.Build
Optional Scope = core.Optional
)
// ParseResult contains the parsed dependencies from a manifest or lockfile.
type ParseResult struct {
Ecosystem string
Kind Kind
// Name is the package's own name as declared in the manifest, when
// the format has one. Empty for lockfiles and for formats that
// only list dependencies (Gemfile, requirements.txt, etc.).
Name string
// Version is the package's own version as declared in the
// manifest, when present.
Version string
Dependencies []Dependency
}
// Options configures Parse.
type Options struct {
// FSRoot, when non-empty, allows parsers that consult neighbouring
// files on disk (currently only pom.xml, for parent <relativePath>
// resolution) to do so within this directory. Paths outside it are
// refused. When empty, parsing is a pure function of content and no
// filesystem access occurs; this is the safe choice for untrusted
// input.
FSRoot string
}
// Parse parses a manifest or lockfile and returns its dependencies.
func Parse(filename string, content []byte, opts ...Options) (*ParseResult, error) {
var o Options
if len(opts) > 0 {
o = opts[0]
}
parser, eco, kind := core.IdentifyParser(filename)
if parser == nil {
return nil, &UnknownFileError{Filename: filename}
}
var res *core.Result
var err error
if fp, ok := parser.(core.FSRootParser); ok {
res, err = fp.ParseInRoot(filename, content, o.FSRoot)
} else {
res, err = parser.Parse(filename, content)
}
if err != nil {
return nil, err
}
if res == nil {
res = &core.Result{}
}
// Generate PURLs for all dependencies
for i := range res.Dependencies {
version := ""
if kind == Lockfile || kind == Supplement {
version = res.Dependencies[i].Version
}
res.Dependencies[i].PURL = makePURL(eco, res.Dependencies[i].Name, version, res.Dependencies[i].RegistryURL)
}
return &ParseResult{
Ecosystem: eco,
Kind: kind,
Name: res.Name,
Version: res.Version,
Dependencies: res.Dependencies,
}, nil
}
// makePURL creates a Package URL for a dependency.
func makePURL(ecosystem, name, version, registryURL string) string {
return purl.BuildPURLString(ecosystem, name, version, registryURL)
}
// Identify returns the ecosystem and kind for a filename without parsing.
func Identify(filename string) (ecosystem string, kind Kind, ok bool) {
_, eco, k := core.IdentifyParser(filename)
if eco == "" {
return "", "", false
}
return eco, k, true
}
// Match represents a file type match.
type Match = core.Match
// IdentifyAll returns all matching ecosystems for a filename.
func IdentifyAll(filename string) []Match {
return core.IdentifyAllParsers(filename)
}
// Ecosystems returns a list of all supported PURL ecosystem types.
func Ecosystems() []string {
return core.SupportedEcosystems()
}
// UnknownFileError is returned when a file type is not recognized.
type UnknownFileError struct {
Filename string
}
func (e *UnknownFileError) Error() string {
return "unknown manifest file: " + e.Filename
}
// ParseError is re-exported from internal/core.
type ParseError = core.ParseError