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



110
111
112
113
114
115
116
117
118
119
# File 'lib/cardano-up/utils.rb', line 110

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



103
104
105
106
107
# File 'lib/cardano-up/utils.rb', line 103

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



37
38
39
# File 'lib/cardano-up/utils.rb', line 37

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/ | ‘master’ | ‘3341’

Raises:

  • CardanoUp::VersionNotSupportedError



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cardano-up/utils.rb', line 66

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

  url = ''
  if 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
    url = "#{CardanoUp::BINS_BASE_URL}/releases/download/#{tag}/#{file}"
  else
    if linux?
      os = 'linux.musl.cardano-wallet-linux64'
    elsif mac?
      os = 'macos.intel.cardano-wallet-macos-intel'
    elsif win?
      os = 'linux.windows.cardano-wallet-win64'
    end

    url = if release == 'master'
            "#{CardanoUp::HYDRA_BASE_URL}/#{os}/latest/download-by-type/file/binary-dist"
          else
            "#{CardanoUp::HYDRA_BASE_URL}-pr-#{release}/#{os}/latest/download-by-type/file/binary-dist"
          end
  end
  url
end

.latest_tagObject



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

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

.linux?Boolean

Returns:

  • (Boolean)


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

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

.mac?Boolean

Returns:

  • (Boolean)


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

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

.port_used?(port) ⇒ Boolean

Check if port is already used

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cardano-up/utils.rb', line 21

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

.to_json(file, hash) ⇒ Object



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

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

.wget(url, file = nil) ⇒ Object



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

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

.win?Boolean

Returns:

  • (Boolean)


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

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