Class: Bibliothecary::Parsers::Go
- Inherits:
-
Object
- Object
- Bibliothecary::Parsers::Go
- Includes:
- Analyser
- Defined in:
- lib/bibliothecary/parsers/go.rb
Constant Summary collapse
- GPM_REGEXP =
/^(.+)\s+(.+)$/- GOMOD_COMMENT_REGEXP =
/(\/\/(.*))/- GOMOD_REPLACEMENT_SEPARATOR_REGEXP =
/\s=>\s/- GOMOD_DEP_REGEXP =
the “ =>” negative character class is to make sure we don’t capture the delimiter for “replace” deps
/(?<name>\S+)\s?(?<requirement>[^\s=>]+)?\s*(?<indirect>\/\/\s+indirect)?/- GOMOD_SINGLELINE_DEP_REGEXP =
/^(?<category>require|exclude|replace|retract)\s+#{GOMOD_DEP_REGEXP}.*$/- GOMOD_MULTILINE_DEP_REGEXP =
/^#{GOMOD_DEP_REGEXP}.*$/- GOMOD_MULTILINE_START_REGEXP =
/^(?<category>require|exclude|replace|retract)\s+\(/- GOMOD_MULTILINE_END_REGEXP =
/^\)/- GOSUM_REGEXP =
/^(.+)\s+(.+)\s+(.+)$/
Class Method Summary collapse
-
.go_mod_category_relative_dep(category:, line:, match:, source: nil) ⇒ Object
Returns our standard-ish dep Hash based on the category of dep matched (“require”, “replace”, etc.).
- .map_dependencies(manifest, attr_name, dep_attr_name, version_attr_name, type, source = nil) ⇒ Object
- .mapping ⇒ Object
- .parse_dep_lockfile(file_contents, options: {}) ⇒ Object
- .parse_dep_toml(file_contents, options: {}) ⇒ Object
- .parse_gb_manifest(file_contents, options: {}) ⇒ Object
- .parse_glide_lockfile(file_contents, options: {}) ⇒ Object
- .parse_glide_yaml(file_contents, options: {}) ⇒ Object
- .parse_go_mod(file_contents, options: {}) ⇒ Object
- .parse_go_mod_categorized_deps(file_contents, source) ⇒ Object
- .parse_go_resolved(file_contents, options: {}) ⇒ Object
- .parse_go_sum(file_contents, options: {}) ⇒ Object
- .parse_godep_json(file_contents, options: {}) ⇒ Object
- .parse_govendor(file_contents, options: {}) ⇒ Object
- .parse_gpm(file_contents, options: {}) ⇒ Object
Methods included from Analyser
create_analysis, create_error_analysis, included
Class Method Details
.go_mod_category_relative_dep(category:, line:, match:, source: nil) ⇒ Object
Returns our standard-ish dep Hash based on the category of dep matched (“require”, “replace”, etc.)
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/bibliothecary/parsers/go.rb', line 241 def self.go_mod_category_relative_dep(category:, line:, match:, source: nil) case category when "replace" replacement_dep = line.split(GOMOD_REPLACEMENT_SEPARATOR_REGEXP, 2).last replacement_match = replacement_dep.match(GOMOD_DEP_REGEXP) Dependency.new( platform: platform_name, original_name: match[:name], original_requirement: match[:requirement], name: replacement_match[:name], requirement: replacement_match[:requirement], type: "runtime", direct: !match[:indirect], source: source ) when "retract" Dependency.new( platform: platform_name, name: match[:name], requirement: match[:requirement], type: "runtime", deprecated: true, direct: !match[:indirect], source: source ) else Dependency.new( platform: platform_name, name: match[:name], requirement: match[:requirement], type: "runtime", direct: !match[:indirect], source: source ) end end |
.map_dependencies(manifest, attr_name, dep_attr_name, version_attr_name, type, source = nil) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/bibliothecary/parsers/go.rb', line 228 def self.map_dependencies(manifest, attr_name, dep_attr_name, version_attr_name, type, source = nil) manifest.fetch(attr_name, []).map do |dependency| Dependency.new( platform: platform_name, name: dependency[dep_attr_name], requirement: dependency[version_attr_name], type: type, source: source ) end end |
.mapping ⇒ Object
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 |
# File 'lib/bibliothecary/parsers/go.rb', line 21 def self.mapping { # Go Modules (recommended) match_filename("go.mod") => { kind: "manifest", parser: :parse_go_mod, }, match_filename("go.sum") => { kind: "lockfile", parser: :parse_go_sum, }, # Glide (unmaintained: https://github.com/Masterminds/glide#go-modules) match_filename("glide.yaml") => { kind: "manifest", parser: :parse_glide_yaml, }, match_filename("glide.lock") => { kind: "lockfile", parser: :parse_glide_lockfile, }, # Godep (unmaintained: https://github.com/tools/godep) match_filename("Godeps/Godeps.json") => { kind: "manifest", parser: :parse_godep_json, }, match_filename("Godeps", case_insensitive: true) => { kind: "manifest", parser: :parse_gpm, }, # Govendor (unmaintained: https://github.com/kardianos/govendor) match_filename("vendor/manifest") => { kind: "manifest", parser: :parse_gb_manifest, }, match_filename("vendor/vendor.json") => { kind: "manifest", parser: :parse_govendor, }, # Go dep (deprecated: https://github.com/golang/dep#dep) match_filename("Gopkg.toml") => { kind: "manifest", parser: :parse_dep_toml, }, match_filename("Gopkg.lock") => { kind: "lockfile", parser: :parse_dep_lockfile, }, match_filename("go-resolved-dependencies.json") => { kind: "lockfile", parser: :parse_go_resolved, }, } end |
.parse_dep_lockfile(file_contents, options: {}) ⇒ Object
134 135 136 137 138 |
# File 'lib/bibliothecary/parsers/go.rb', line 134 def self.parse_dep_lockfile(file_contents, options: {}) manifest = Tomlrb.parse file_contents dependencies = map_dependencies(manifest, "projects", "name", "revision", "runtime", .fetch(:filename, nil)) ParserResult.new(dependencies: dependencies) end |
.parse_dep_toml(file_contents, options: {}) ⇒ Object
128 129 130 131 132 |
# File 'lib/bibliothecary/parsers/go.rb', line 128 def self.parse_dep_toml(file_contents, options: {}) manifest = Tomlrb.parse file_contents dependencies = map_dependencies(manifest, "constraint", "name", "version", "runtime", .fetch(:filename, nil)) ParserResult.new(dependencies: dependencies) end |
.parse_gb_manifest(file_contents, options: {}) ⇒ Object
122 123 124 125 126 |
# File 'lib/bibliothecary/parsers/go.rb', line 122 def self.parse_gb_manifest(file_contents, options: {}) manifest = JSON.parse file_contents dependencies = map_dependencies(manifest, "dependencies", "importpath", "revision", "runtime", .fetch(:filename, nil)) ParserResult.new(dependencies: dependencies) end |
.parse_glide_lockfile(file_contents, options: {}) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/bibliothecary/parsers/go.rb', line 115 def self.parse_glide_lockfile(file_contents, options: {}) # glide.lock files contain an "updated" Time field, but Ruby 3.2+ requires us to safelist that class manifest = YAML.load file_contents, permitted_classes: [Time] dependencies = map_dependencies(manifest, "imports", "name", "version", "runtime", .fetch(:filename, nil)) ParserResult.new(dependencies: dependencies) end |
.parse_glide_yaml(file_contents, options: {}) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/bibliothecary/parsers/go.rb', line 108 def self.parse_glide_yaml(file_contents, options: {}) manifest = YAML.load file_contents dependencies = map_dependencies(manifest, "import", "package", "version", "runtime", .fetch(:filename, nil)) + map_dependencies(manifest, "devImports", "package", "version", "development", .fetch(:filename, nil)) ParserResult.new(dependencies: dependencies) end |
.parse_go_mod(file_contents, options: {}) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/bibliothecary/parsers/go.rb', line 140 def self.parse_go_mod(file_contents, options: {}) categorized_deps = parse_go_mod_categorized_deps(file_contents, .fetch(:filename, nil)) dependencies = categorized_deps["require"] .map do |dep| # NOTE: A "replace" directive doesn't add the dep to the module graph unless the original dep is also in a "require" directive, # so we need to track down replacements here and use those instead of the originals, if present. # # NOTE: The "replace" directive doesn't actually change the version reported from Go (e.g. "go mod graph"), it only changes # the *source code*. So by replacing the deps here, we're giving more honest results than you'd get when asking go # about the versions used. replaced_dep = categorized_deps["replace"] .find do |replacement_dep| replacement_dep.original_name == dep.name && (replacement_dep.original_requirement == "*" || replacement_dep.original_requirement == dep.requirement) end replaced_dep || dep end ParserResult.new(dependencies: dependencies) end |
.parse_go_mod_categorized_deps(file_contents, source) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/bibliothecary/parsers/go.rb', line 162 def self.parse_go_mod_categorized_deps(file_contents, source) current_multiline_category = nil # docs: https://go.dev/ref/mod#go-mod-file-require categorized_deps = { "require" => [], "exclude" => [], # these deps are not necessarily used by the module "replace" => [], # these deps are not necessarily used by the module "retract" => [], # TODO: these are not parsed correctly right now, but they shouldn't be returned in list of deps anyway. } file_contents .lines .map(&:strip) .grep_v(/^#{GOMOD_COMMENT_REGEXP}/) # ignore comment lines .each do |line| if line.match(GOMOD_MULTILINE_END_REGEXP) # detect the end of a multiline current_multiline_category = nil elsif (match = line.match(GOMOD_MULTILINE_START_REGEXP)) # or, detect the start of a multiline current_multiline_category = match[1] elsif (match = line.match(GOMOD_SINGLELINE_DEP_REGEXP)) # or, detect a singleline dep categorized_deps[match[:category]] << go_mod_category_relative_dep(category: match[:category], line: line, match: match, source: source) elsif current_multiline_category && (match = line.match(GOMOD_MULTILINE_DEP_REGEXP)) # otherwise, parse the multiline dep categorized_deps[current_multiline_category] << go_mod_category_relative_dep(category: current_multiline_category, line: line, match: match, source: source) end end categorized_deps end |
.parse_go_resolved(file_contents, options: {}) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/bibliothecary/parsers/go.rb', line 206 def self.parse_go_resolved(file_contents, options: {}) dependencies = JSON.parse(file_contents) .reject { |dep| dep["Main"] == "true" } .map do |dep| if dep["Replace"].is_a?(String) && dep["Replace"] != "<nil>" && dep["Replace"] != "" # NOTE: The "replace" directive doesn't actually change the version reported from Go (e.g. "go mod graph"), it only changes # the *source code*. So by replacing the deps here, we're giving more honest results than you'd get when asking go # about the versions used. name, requirement = dep["Replace"].split(" ", 2) requirement = "*" if requirement.to_s.strip == "" Dependency.new( platform: platform_name, name: name, requirement: requirement, original_name: dep["Path"], original_requirement: dep["Version"], type: dep.fetch("Scope", "runtime"), source: .fetch(:filename, nil) ) else Dependency.new( platform: platform_name, name: dep["Path"], requirement: dep["Version"], type: dep.fetch("Scope", "runtime"), source: .fetch(:filename, nil) ) end end ParserResult.new(dependencies: dependencies) end |
.parse_go_sum(file_contents, options: {}) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/bibliothecary/parsers/go.rb', line 189 def self.parse_go_sum(file_contents, options: {}) deps = [] file_contents.lines.map(&:strip).each do |line| next unless (match = line.match(GOSUM_REGEXP)) deps << Dependency.new( name: match[1].strip, requirement: match[2].strip.split("/").first, type: "runtime", source: .fetch(:filename, nil), platform: platform_name ) end dependencies = deps.uniq ParserResult.new(dependencies: dependencies) end |
.parse_godep_json(file_contents, options: {}) ⇒ Object
79 80 81 82 83 |
# File 'lib/bibliothecary/parsers/go.rb', line 79 def self.parse_godep_json(file_contents, options: {}) manifest = JSON.parse file_contents dependencies = map_dependencies(manifest, "Deps", "ImportPath", "Rev", "runtime", .fetch(:filename, nil)) ParserResult.new(dependencies: dependencies) end |
.parse_govendor(file_contents, options: {}) ⇒ Object
102 103 104 105 106 |
# File 'lib/bibliothecary/parsers/go.rb', line 102 def self.parse_govendor(file_contents, options: {}) manifest = JSON.parse file_contents dependencies = map_dependencies(manifest, "package", "path", "revision", "runtime", .fetch(:filename, nil)) ParserResult.new(dependencies: dependencies) end |
.parse_gpm(file_contents, options: {}) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bibliothecary/parsers/go.rb', line 85 def self.parse_gpm(file_contents, options: {}) deps = [] file_contents.split("\n").each do |line| match = line.gsub(/(\#(.*))/, "").match(GPM_REGEXP) next unless match deps << Dependency.new( name: match[1].strip, requirement: match[2].strip, type: "runtime", source: .fetch(:filename, nil), platform: platform_name ) end ParserResult.new(dependencies: deps) end |