summaryrefslogtreecommitdiff
path: root/lib/license/finder/ext/go_modules.rb
blob: 2ef3aa25243bd02cc9d43858366b0cc2b18a7832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module LicenseFinder
  class GoModules
    def prepare
      shell.execute([:go, :mod, :tidy, '-v', '&&', :go, :mod, :vendor, '-v'])
    end

    def active?
      Dir[project_path.join('go.sum')].any?
    end

    def current_packages
      stdout, _stderr, status = shell.execute([
        :go, :list, '-m', '-mod=vendor',
        '-f', "'{{.Path}},{{.Version}},{{.Dir}}'", :all
      ])
      return [] unless status.success?

      stdout.lines.map do |line|
        name, version, dir = line.chomp.split(',')
        next if Pathname(dir).cleanpath == absolute_project_path

        Package.new(name, version, install_path: dir)
      end.compact
    end

    private

    def shell
      @shell ||= ::License::Management::Shell.new
    end

    def absolute_project_path
      @absolute_project_path ||= Pathname(project_path).cleanpath
    end
  end
end