Class: Licensed::Source::Go

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Go

Returns a new instance of Go.



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

def initialize(config)
  @config = config
end

Class Method Details

.typeObject



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

def self.type
  "go"
end

Instance Method Details

#dependenciesObject



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

def dependencies
  @dependencies ||= with_configured_gopath do
    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" => Go.type, "name" => package_name)
        raise "couldn't find package for #{import_path}"
      end

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

#enabled?Boolean

Returns:

  • (Boolean)


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

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

#go_source?Boolean

Returns whether go source is found

Returns:

  • (Boolean)


138
139
140
# File 'lib/licensed/source/go.rb', line 138

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

#go_std_packagesObject

Returns a list of go standard packages



143
144
145
# File 'lib/licensed/source/go.rb', line 143

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

#gopathObject

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/licensed/source/go.rb', line 149

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



59
60
61
62
63
64
65
# File 'lib/licensed/source/go.rb', line 59

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



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

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



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

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



127
128
129
130
# File 'lib/licensed/source/go.rb', line 127

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

#package_version(package_directory) ⇒ Object

Returns the most recent git SHA for a package, or nil if SHA is not available

package_directory - package location



49
50
51
52
53
54
55
# File 'lib/licensed/source/go.rb', line 49

def package_version(package_directory)
  return unless package_directory

  Dir.chdir package_directory do
    Licensed::Git.version(".")
  end
end

#packagesObject

Returns an array of dependency package import paths



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/licensed/source/go.rb', line 68

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) }
    .select do |d|
    # this removes the packages listed in `go list std` as "vendor/golang_org/*" but are vendored
    # as "vendor/golang.org/*"
      go_std_packages.none? do |std_pkg|
        std_pkg.sub(%r{^vendor/golang_org/}, "#{root_package["ImportPath"]}/vendor/golang.org/") == d
      end
    end
end

#root_packageObject

Returns the info for the package under test



133
134
135
# File 'lib/licensed/source/go.rb', line 133

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



89
90
91
92
93
94
95
96
# File 'lib/licensed/source/go.rb', line 89

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)
  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)


102
103
104
# File 'lib/licensed/source/go.rb', line 102

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