Class: Licensed::Sources::Go

Inherits:
Source
  • Object
show all
Includes:
ContentVersioning
Defined in:
lib/licensed/sources/go.rb

Constant Summary

Constants included from ContentVersioning

ContentVersioning::CONTENTS, ContentVersioning::GIT

Instance Attribute Summary

Attributes inherited from Source

#config

Instance Method Summary collapse

Methods included from ContentVersioning

#contents_hash, #contents_version, #git_version, #version_strategy

Methods inherited from Source

#dependencies, #ignored?, inherited, #initialize, type

Constructor Details

This class inherits a constructor from Licensed::Sources::Source

Instance Method Details

#contents_version_argumentsObject

Determines the arguments to pass to contents_version based on which version strategy is selected

Returns an array of arguments to pass to contents version



116
117
118
119
120
121
122
# File 'lib/licensed/sources/go.rb', line 116

def contents_version_arguments
  if version_strategy == Licensed::Sources::ContentVersioning::GIT
    ["."]
  else
    Dir["*"]
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/licensed/sources/go.rb', line 11

def enabled?
  Licensed::Shell.tool_available?("go") && go_source?
end

#enumerate_dependenciesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/licensed/sources/go.rb', line 15

def enumerate_dependencies
  with_configured_gopath do
    packages.map do |package|
      import_path = non_vendored_import_path(package["ImportPath"])
      error = package.dig("Error", "Err") if package["Error"]
      package_dir = package["Dir"]

      Dependency.new(
        name: import_path,
        version: package_version(package),
        path: package_dir,
        search_root: search_root(package_dir),
        errors: Array(error),
        metadata: {
          "type"        => Go.type,
          "summary"     => package["Doc"],
          "homepage"    => homepage(import_path)
        }
      )
    end
  end
end

#go_list_depsObject

Returns the list of dependencies as returned by “go list -json -deps” available in go 1.11



62
63
64
65
66
67
68
69
# File 'lib/licensed/sources/go.rb', line 62

def go_list_deps
  # the CLI command returns packages in a pretty-printed JSON format but
  # not separated by commas. this gsub adds commas after all non-indented
  # "}" that close root level objects.
  # (?!\z) uses negative lookahead to not match the final "}"
  deps = package_info_command("-deps").gsub(/^}(?!\z)$/m, "},")
  JSON.parse("[#{deps}]")
end

#go_source?Boolean

Returns whether go source is found

Returns:

  • (Boolean)


185
186
187
# File 'lib/licensed/sources/go.rb', line 185

def go_source?
  with_configured_gopath { Licensed::Shell.success?("go", "doc") }
end

#go_std_package?(package) ⇒ Boolean

Returns whether the given package import path belongs to the go std library or not

package - package to check as part of the go standard library

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/licensed/sources/go.rb', line 75

def go_std_package?(package)
  return false unless package
  return true if package["Standard"]

  import_path = package["ImportPath"]
  return false unless import_path

  # modify the import path to look like the import path `go list` returns for vendored std packages
  std_vendor_import_path = import_path.sub(%r{^#{root_package["ImportPath"]}/vendor/golang.org}, "vendor/golang_org")
  go_std_packages.include?(import_path) || go_std_packages.include?(std_vendor_import_path)
end

#go_std_packagesObject

Returns a list of go standard packages



190
191
192
# File 'lib/licensed/sources/go.rb', line 190

def go_std_packages
  @std_packages ||= Licensed::Shell.execute("go", "list", "std").lines.map(&:strip)
end

#go_versionObject

Returns the current version of go available, as a Gem::Version



213
214
215
216
217
218
219
# File 'lib/licensed/sources/go.rb', line 213

def go_version
  @go_version ||= begin
    full_version = Licensed::Shell.execute("go", "version").strip
    version_string = full_version.gsub(%r{.*go(\d+\.\d+(\.\d+)?).*}, "\\1")
    Gem::Version.new(version_string)
  end
end

#gopathObject

Returns a GOPATH value from either a configuration value or ENV, with the configuration value preferred over the ENV var



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/licensed/sources/go.rb', line 196

def gopath
  return @gopath if defined?(@gopath)

  path = @config.dig("go", "GOPATH")
  @gopath = if path.nil? || path.empty?
              ENV["GOPATH"]
            else
              root = begin
                       @config.root
                     rescue Licensed::Shell::Error
                       Pathname.pwd
                     end
              File.expand_path(path, root)
            end
end

#homepage(import_path) ⇒ Object

Returns the homepage for a package import_path. Assumes that the import path itself is a url domain and path



126
127
128
129
130
131
132
# File 'lib/licensed/sources/go.rb', line 126

def homepage(import_path)
  return unless import_path

  # hacky but generally works due to go packages looking like
  # "github.com/..." or "golang.org/..."
  "https://#{import_path}"
end

#local_package?(package) ⇒ Boolean

Returns whether the package is local to the current project

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/licensed/sources/go.rb', line 88

def local_package?(package)
  return false unless package && package["ImportPath"]
  import_path = package["ImportPath"]
  import_path.start_with?(root_package["ImportPath"]) && !vendored_path?(import_path)
end

#non_vendored_import_path(import_path) ⇒ Object

Returns the import path parameter without the vendor component

import_path - Package import path with vendor component



159
160
161
162
163
# File 'lib/licensed/sources/go.rb', line 159

def non_vendored_import_path(import_path)
  return unless import_path
  return import_path unless vendored_path?(import_path)
  import_path.split("vendor/")[1]
end

#package_info(import_path) ⇒ Object

Returns a hash of information about the package with a given import path

import_path - Go package import path



168
169
170
# File 'lib/licensed/sources/go.rb', line 168

def package_info(import_path)
  JSON.parse(package_info_command(import_path))
end

#package_info_command(*args) ⇒ Object

Returns package information as a JSON string

args - additional arguments to ‘go list`, e.g. Go package import path



175
176
177
# File 'lib/licensed/sources/go.rb', line 175

def package_info_command(*args)
  Licensed::Shell.execute("go", "list", "-e", "-json", *Array(args)).strip
end

#package_version(package) ⇒ Object

Returns the version for a given package

package - package to get version of



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/licensed/sources/go.rb', line 97

def package_version(package)
  # use module version if it exists
  go_mod = package["Module"]
  return go_mod["Version"] if go_mod

  package_directory = package["Dir"]
  return unless package_directory

  # find most recent git SHA for a package, or nil if SHA is
  # not available
  Dir.chdir package_directory do
    contents_version *contents_version_arguments
  end
end

#packagesObject

Returns an array of dependency package import paths



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/licensed/sources/go.rb', line 39

def packages
  dependency_packages = if go_version < Gem::Version.new("1.11.0")
    root_package_deps
  else
    go_list_deps
  end

  # don't include go std packages
  # don't include packages under the root project that aren't vendored
  dependency_packages
    .reject { |pkg| go_std_package?(pkg) }
    .reject { |pkg| local_package?(pkg) }
end

#root_packageObject

Returns the info for the package under test



180
181
182
# File 'lib/licensed/sources/go.rb', line 180

def root_package
  @root_package ||= package_info(".")
end

#root_package_depsObject

Returns non-ignored packages found from the root packages “Deps” property



54
55
56
57
58
# File 'lib/licensed/sources/go.rb', line 54

def root_package_deps
  # check for ignored packages to avoid raising errors calling `go list`
  # when ignored package is not found
  Array(root_package["Deps"]).map { |name| package_info(name) }
end

#search_root(package_dir) ⇒ Object

Returns the root directory to search for a package license

package - package object obtained from package_info



137
138
139
140
141
142
143
144
145
146
# File 'lib/licensed/sources/go.rb', line 137

def search_root(package_dir)
  return nil if package_dir.nil? || package_dir.empty?

  # search root choices:
  # 1. vendor folder if package is vendored
  # 2. GOPATH
  # 3. nil (no search up directory hierarchy)
  return package_dir.match("^(.*/vendor)/.*$")[1] if vendored_path?(package_dir)
  gopath
end

#vendored_path?(path) ⇒ Boolean

Returns whether a package is vendored or not based on the package import_path

path - Package path to test

Returns:

  • (Boolean)


152
153
154
# File 'lib/licensed/sources/go.rb', line 152

def vendored_path?(path)
  path && path.include?("vendor/")
end