Class: Licensed::Source::Go

Inherits:
Object
  • Object
show all
Defined in:
lib/licensed/source/go.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Go

Returns a new instance of Go.



8
9
10
# File 'lib/licensed/source/go.rb', line 8

def initialize(config)
  @config = config
end

Instance Method Details

#dependenciesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/licensed/source/go.rb', line 20

def dependencies
  @dependencies ||= packages.map do |package_name|
    package = package_info(package_name)
    import_path = non_vendored_import_path(package_name)

    if package.empty?
      next if @config.ignored?("type" => type, "name" => package_name)
      raise "couldn't find package for #{import_path}"
    end

    package_dir = package["Dir"]
    Dependency.new(package_dir, {
      "type"        => type,
      "name"        => import_path,
      "summary"     => package["Doc"],
      "homepage"    => homepage(import_path),
      "search_root" => search_root(package_dir),
      "version"     => Licensed::Git.version(package_dir)
    })
  end.compact
end

#enabled?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/licensed/source/go.rb', line 16

def enabled?
  @config.enabled?(type) && go_source?
end

#go_source?Boolean

Returns whether go source is found

Returns:

  • (Boolean)


116
117
118
# File 'lib/licensed/source/go.rb', line 116

def go_source?
  @go_source ||= Licensed::Shell.success?("go", "doc")
end

#go_std_packagesObject

Returns a list of go standard packages



121
122
123
# File 'lib/licensed/source/go.rb', line 121

def go_std_packages
  @std_packages ||= Licensed::Shell.execute("go", "list", "std").lines.map(&:strip)
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



44
45
46
47
48
49
50
# File 'lib/licensed/source/go.rb', line 44

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

#non_vendored_import_path(import_path) ⇒ Object

Returns the import path parameter without the vendor component

import_path - Package import path with vendor component



87
88
89
90
91
# File 'lib/licensed/source/go.rb', line 87

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(package = nil) ⇒ Object

Returns package information, or {} if package isn’t found

package - Go package import path



96
97
98
99
100
# File 'lib/licensed/source/go.rb', line 96

def package_info(package = nil)
  info = package_info_command(package)
  return {} if info.empty?
  JSON.parse(info)
end

#package_info_command(package) ⇒ Object

Returns package information as a JSON string

package - Go package import path



105
106
107
108
# File 'lib/licensed/source/go.rb', line 105

def package_info_command(package)
  package ||= ""
  Licensed::Shell.execute("go", "list", "-json", package)
end

#packagesObject

Returns an array of dependency package import paths



53
54
55
56
57
58
59
60
61
62
# File 'lib/licensed/source/go.rb', line 53

def packages
  return [] unless root_package["Deps"]

  # don't include go std packages
  # don't include packages under the root project that aren't vendored
  root_package["Deps"]
    .uniq
    .select { |d| !go_std_packages.include?(d) }
    .select { |d| !d.start_with?(root_package["ImportPath"]) || vendored_path?(d) }
end

#root_packageObject

Returns the info for the package under test



111
112
113
# File 'lib/licensed/source/go.rb', line 111

def root_package
  @root_package ||= package_info
end

#search_root(package_dir) ⇒ Object

Returns the root directory to search for a package license

package - package object obtained from package_info



67
68
69
70
71
72
73
74
# File 'lib/licensed/source/go.rb', line 67

def search_root(package_dir)
  # 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)
  ENV.fetch("GOPATH", nil)
end

#typeObject



12
13
14
# File 'lib/licensed/source/go.rb', line 12

def type
  "go"
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)


80
81
82
# File 'lib/licensed/source/go.rb', line 80

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