Module: PuppetX::Rustup::Util

Defined in:
lib/puppet_x/rustup/util.rb

Overview

Utility functions for rustup

Class Method Summary collapse

Class Method Details

.download(url, basename = '') ⇒ Object

Make a download available as a ‘Tempfile` in a block.

download('https://example.com/test.sh', ['test', '.sh']) do |file|
  puts "#{file.path} will be deleted after the block ends."
end


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet_x/rustup/util.rb', line 30

def self.download(url, basename = '')
  file = Tempfile.new(basename)
  begin
    Puppet.debug { "Downloading #{url.inspect} into #{file.path.inspect}" }
    PuppetX::Rustup::Util.download_into(url, file)
    file.flush

    yield(file)
  ensure
    Puppet.debug { "Deleting #{file.path.inspect}" }
    file.close
    file.unlink
  end
end

.download_into(url, output) ⇒ Object

Download a URL into a stream.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/puppet_x/rustup/util.rb', line 8

def self.download_into(url, output)
  client = Puppet.runtime[:http]
  client.get(url, options: { include_system_store: true }) do |response|
    unless response.success?
      message = response.body.empty? ? response.reason : response.body
      raise Net::HTTPError.new(
        "Error #{response.code} on SERVER: #{message}",
        Puppet::HTTP::ResponseConverter.to_ruby_response(response),
      )
    end

    response.read_body do |chunk|
      output.print(chunk)
    end
  end
end

.nil_or_non_empty_string?(input) ⇒ Boolean

Check that input is nil or a non-empty string.

Returns:

  • (Boolean)


70
71
72
# File 'lib/puppet_x/rustup/util.rb', line 70

def self.nil_or_non_empty_string?(input)
  input.nil? || non_empty_string?(input)
end

.non_empty_string?(input) ⇒ Boolean

Check that input is a non-empty string.

Returns:

  • (Boolean)


65
66
67
# File 'lib/puppet_x/rustup/util.rb', line 65

def self.non_empty_string?(input)
  input.is_a?(String) && !input.empty?
end

.remove_file_line(path, line_to_remove) ⇒ Object

Remove a line from a file.

This does the removal in place to avoid problems with symlinks, hard links, and preserving metadata (like modes and ACLs). This means the removal is not atomic.

This only writes to the file if changes are made.

line_to_remove is a string that matches the line to remove, not including newlines.



55
56
57
58
59
60
61
62
# File 'lib/puppet_x/rustup/util.rb', line 55

def self.remove_file_line(path, line_to_remove)
  matcher = Regexp.escape(line_to_remove)
  contents = IO.read(path)
  if contents.gsub!(%r{^#{matcher}(\n|\r\n|\r|\Z)}, '')
    # Substitutions were made
    IO.write(path, contents)
  end
end