Module: CardanoUp::Utils

Defined in:
lib/cardano-up/utils.rb

Overview

Utility methods

Class Method Summary collapse

Class Method Details

.cmd(cmd, display_result: false) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/cardano-up/utils.rb', line 6

def self.cmd(cmd, display_result: false)
  cmd.gsub(/\s+/, ' ')
  res = `#{cmd}`
  puts cmd if display_result
  puts res if display_result
  res.gsub("\n", ' ').strip
end

.config_urls(env) ⇒ Object

Raises:

  • CardanoUp::EnvNotSupportedError



96
97
98
99
100
101
102
103
104
105
# File 'lib/cardano-up/utils.rb', line 96

def self.config_urls(env)
  raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include?(env)

  base_url = configs_base_url(env)
  configs = []
  CardanoUp::CONFIG_FILES.each do |file|
    configs << "#{base_url}#{file}"
  end
  configs
end

.configs_base_url(env) ⇒ Object

Latest Cardano configs

Raises:

  • CardanoUp::EnvNotSupportedError



89
90
91
92
93
# File 'lib/cardano-up/utils.rb', line 89

def self.configs_base_url(env)
  raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include?(env)

  "#{CardanoUp::CONFIGS_BASE_URL}/#{env}/"
end

.from_json(file) ⇒ Object



42
43
44
# File 'lib/cardano-up/utils.rb', line 42

def self.from_json(file)
  JSON.parse(File.read(file), { symbolize_names: true })
end

.get_binary_url(release = 'latest') ⇒ Object

Latest binary url for latest release or particular tag, from master or pr num.

Parameters:

  • release (String) (defaults to: 'latest')
    • ‘latest’ | /^v20.2-.2-.2/

Raises:

  • CardanoUp::VersionNotSupportedError



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cardano-up/utils.rb', line 71

def self.get_binary_url(release = 'latest')
  raise CardanoUp::VersionNotSupportedError, release unless release == 'latest' || release =~ /^v20.{2}-.{2}-.{2}/

  tag = release == 'latest' ? latest_tag : release
  if linux?
    file = "cardano-wallet-#{tag}-linux64.tar.gz"
  elsif mac?
    file = "cardano-wallet-#{tag}-macos-intel.tar.gz"
  elsif win?
    file = "cardano-wallet-#{tag}-win64.zip"
  end

  "#{CardanoUp::BINS_BASE_URL}/releases/download/#{tag}/#{file}"
end

.latest_tagObject



62
63
64
65
# File 'lib/cardano-up/utils.rb', line 62

def self.latest_tag
  HTTParty.get("#{CardanoUp::BINS_BASE_URL}/releases/latest",
               headers: { 'Accept' => 'application/json' })['tag_name']
end

.linux?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/cardano-up/utils.rb', line 54

def self.linux?
  RUBY_PLATFORM =~ /linux/
end

.mac?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/cardano-up/utils.rb', line 58

def self.mac?
  RUBY_PLATFORM =~ /darwin/
end

.port_used?(port) ⇒ Boolean

Check if port is already used

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cardano-up/utils.rb', line 26

def self.port_used?(port)
  begin
    Timeout.timeout(1) do
      s = TCPSocket.new('localhost', port)
      s.close
      return true
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
      return false
    end
  rescue Timeout::Error
    # do nothing
  end

  false
end

.screen?Boolean

Check if screen command is available

Returns:

  • (Boolean)


15
16
17
# File 'lib/cardano-up/utils.rb', line 15

def self.screen?
  cmd('which screen').chomp.length.positive?
end

.to_json(file, hash) ⇒ Object



46
47
48
# File 'lib/cardano-up/utils.rb', line 46

def self.to_json(file, hash)
  File.write(file, JSON.pretty_generate(hash))
end

.wget(url, file = nil) ⇒ Object



19
20
21
22
23
# File 'lib/cardano-up/utils.rb', line 19

def self.wget(url, file = nil)
  file ||= File.basename(url)
  resp = HTTParty.get(url)
  File.binwrite(file, resp.body)
end

.win?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cardano-up/utils.rb', line 50

def self.win?
  RUBY_PLATFORM =~ /cygwin|mswin|mingw|bccwin|wince|emx/
end