Class: Dependabot::GoModules::FileUpdater::GoModUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/go_modules/file_updater/go_mod_updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(dependencies:, go_mod:, go_sum:, credentials:) ⇒ GoModUpdater

Returns a new instance of GoModUpdater.



11
12
13
14
15
16
# File 'lib/dependabot/go_modules/file_updater/go_mod_updater.rb', line 11

def initialize(dependencies:, go_mod:, go_sum:, credentials:)
  @dependencies = dependencies
  @go_mod = go_mod
  @go_sum = go_sum
  @credentials = credentials
end

Instance Method Details

#updated_go_mod_contentObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dependabot/go_modules/file_updater/go_mod_updater.rb', line 18

def updated_go_mod_content
  @updated_go_mod_content ||=
    SharedHelpers.in_a_temporary_directory do
      SharedHelpers.with_git_configured(credentials: credentials) do
        File.write("go.mod", go_mod.content)

        deps = dependencies.map do |dep|
          {
            name: dep.name,
            version: "v" + dep.version.sub(/^v/i, ""),
            indirect: dep.requirements.empty?
          }
        end

        SharedHelpers.run_helper_subprocess(
          command: "GO111MODULE=on #{NativeHelpers.helper_path}",
          function: "updateDependencyFile",
          args: { dependencies: deps }
        )
      end
    end
end

#updated_go_sum_contentObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dependabot/go_modules/file_updater/go_mod_updater.rb', line 41

def updated_go_sum_content
  return nil unless go_sum

  # This needs to be run separately so we don't nest subprocess calls
  updated_go_mod_content

  @updated_go_sum_content ||=
    SharedHelpers.in_a_temporary_directory do
      SharedHelpers.with_git_configured(credentials: credentials) do
        File.write("go.mod", updated_go_mod_content)
        File.write("go.sum", go_sum.content)
        File.write("main.go", dummy_main_go)

        `GO111MODULE=on go get -d`
        unless $CHILD_STATUS.success?
          raise Dependabot::DependencyFileNotParseable, go_sum.path
        end

        File.read("go.sum")
      end
    end
end