Skip to content

Commit

Permalink
Bibliothecary::Parsers::Go: pass "direct: true|false" back from go.mo…
Browse files Browse the repository at this point in the history
…d deps (librariesio#594)

* Pass "direct: true|false" back from go.mod dependencies.

* 9.1.0
  • Loading branch information
tiegz authored and andrew committed Jun 21, 2024
1 parent 2835467 commit 7a5845a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
9 changes: 6 additions & 3 deletions lib/bibliothecary/parsers/go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class Go
include Bibliothecary::Analyser

GPM_REGEXP = /^(.+)\s+(.+)$/
GOMOD_COMMENT_REGEXP = /(\/\/(.*))/
GOMOD_REPLACEMENT_SEPARATOR_REGEXP = /\s=>\s/
GOMOD_DEP_REGEXP = /(?<name>\S+)\s?(?<requirement>[^\s=>]+)?/ # the " =>" negative character class is to make sure we don't capture the delimiter for "replace" deps
GOMOD_DEP_REGEXP = /(?<name>\S+)\s?(?<requirement>[^\s=>]+)?\s*(?<indirect>\/\/\s+indirect)?/ # the " =>" negative character class is to make sure we don't capture the delimiter for "replace" deps
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 = /^\)/
GOMOD_COMMENT_REGEXP = /(\/\/(.*))/
GOSUM_REGEXP = /^(.+)\s+(.+)\s+(.+)$/

def self.mapping
Expand Down Expand Up @@ -158,8 +158,8 @@ def self.parse_go_mod_categorized_deps(file_contents)
}
file_contents
.lines
.map(&:strip)
.reject { |line| line =~ /^#{GOMOD_COMMENT_REGEXP}/ } # ignore comment lines
.map { |line| line.strip.gsub(GOMOD_COMMENT_REGEXP, "") } # strip out trailing comments
.each do |line|
if line.match(GOMOD_MULTILINE_END_REGEXP) # detect the end of a multiline
current_multiline_category = nil
Expand Down Expand Up @@ -227,19 +227,22 @@ def self.go_mod_category_relative_dep(category:, line:, match:)
name: replacement_match[:name],
requirement: replacement_match[:requirement] || "*",
type: "runtime",
direct: !match[:indirect],
}
when "retract"
{
name: match[:name],
requirement: match[:requirement] || "*",
type: "runtime",
deprecated: true,
direct: !match[:indirect],
}
else
{
name: match[:name],
requirement: match[:requirement] || "*",
type: "runtime",
direct: !match[:indirect],
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bibliothecary/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bibliothecary
VERSION = "9.0.0"
VERSION = "9.1.0"
end
18 changes: 14 additions & 4 deletions spec/parsers/go_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,38 @@
name: "github.com/go-check/check",
requirement: "v0.0.0-20180628173108-788fd7840127",
type: "runtime",
direct: false,
},
{
name: "github.com/gomodule/redigo",
requirement: "v2.0.0+incompatible",
type: "runtime",
direct: false,
},
{
name: "github.com/kr/pretty",
requirement: "v0.1.0",
type: "runtime",
direct: false,
},
{ name: "github.com/replicon/fast-archiver",
requirement: "v0.0.0-20121220195659-060bf9adec25",
type: "runtime",
direct: false,
},
{
name: "gopkg.in/yaml.v1",
requirement: "v1.0.0-20140924161607-9f9df34309c0",
type: "runtime"},
type: "runtime",
direct: true,
},
{
original_name: "golang.org/x/net",
original_requirement: "v1.2.3",
name: "example.com/fork/net",
requirement: "v1.4.5",
type: "runtime",
direct: true,
},
],
kind: "manifest",
Expand All @@ -51,9 +58,12 @@
platform: "go",
path: "go.mod",
dependencies: [
{ name: "github.com/go-check/check",
requirement: "v0.0.0-20180628173108-788fd7840127",
type: "runtime" },
{
name: "github.com/go-check/check",
requirement: "v0.0.0-20180628173108-788fd7840127",
type: "runtime",
direct: false,
},
],
kind: "manifest",
success: true,
Expand Down

0 comments on commit 7a5845a

Please sign in to comment.