cardano-up 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,41 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CardanoUp
4
+ # Thrown when service not exists in session
5
+ class SessionServiceNotUpError < StandardError
6
+ def initialize(session_name, env, service)
7
+ super("Service '#{service}' is not running on '#{env}' in session '#{session_name}'!")
8
+ end
9
+ end
10
+
11
+ # Thrown when env not exists in session
12
+ class SessionEnvNotUpError < StandardError
13
+ def initialize(session_name, env)
14
+ super("Nothing is running on '#{env}' in session '#{session_name}'!")
15
+ end
16
+ end
17
+
18
+ # Thrown when session not exists
19
+ class SessionNotExistsError < StandardError
20
+ def initialize(session_name)
21
+ super("Session '#{session_name}' does not exist!")
22
+ end
23
+ end
24
+
25
+ # Thrown when there is already a node running in the session
26
+ class SessionHasNodeError < StandardError
27
+ def initialize(session_name, network)
28
+ super("Session '#{session_name}' already has node running on '#{network}'!")
29
+ end
30
+ end
31
+
32
+ # Thrown when there is already a wallet running in the session
33
+ class SessionHasWalletError < StandardError
34
+ def initialize(session_name, network)
35
+ super("Session '#{session_name}' already has wallet running on '#{network}'!")
36
+ end
37
+ end
38
+
4
39
  # Thrown when env is not supported
5
40
  class EnvNotSupportedError < StandardError
6
41
  def initialize(env)
@@ -22,11 +57,25 @@ module CardanoUp
22
57
  end
23
58
  end
24
59
 
60
+ # Thrown when wallet port is not set
61
+ class WalletPortUsedError < StandardError
62
+ def initialize(port)
63
+ super("The port #{port} is already in use!")
64
+ end
65
+ end
66
+
25
67
  # Thrown when version of bundle to download is not supported
26
68
  class VersionNotSupportedError < StandardError
27
69
  def initialize(ver)
28
- super(["Not supported version: #{ver}. Supported are: 'latest', 'master',",
29
- "tag (e.g. 'v2022-08-16') or pr number ('3045')"].join(' '))
70
+ super(["Not supported version: #{ver}. Supported are: 'latest' or",
71
+ "tag (e.g. 'v2022-08-16')"].join(' '))
72
+ end
73
+ end
74
+
75
+ # Thrown when there is no screen on the system
76
+ class NoScreenError < StandardError
77
+ def initialize
78
+ super('There is no screen tool on the system!')
30
79
  end
31
80
  end
32
81
  end
@@ -0,0 +1,263 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardanoUp
4
+ ##
5
+ # Start/stop cardano-node and cardano-wallet on your system.
6
+ #
7
+ # For Linux and MacOS it will attempt to start separate screen sessions for
8
+ # wallet and node respectively, therefore 'screen' tool is required on your system.
9
+ # @see https://www.gnu.org/software/screen/
10
+ # If screen is not present you can install it using your package manager for instance:
11
+ # # MacOS:
12
+ # brew install screen
13
+ # # Linux:
14
+ # sudo apt-get install screen
15
+ #
16
+ # For Windows it will attepmt to install cardano-node and cardano-wallet as Windows services
17
+ # using 'nssm' tool.
18
+ # @see https://nssm.cc/
19
+ # Nssm can be installed via choco package manager:
20
+ # choco install nssm
21
+ module Launcher
22
+ # Create common set of variables for getting node and wallet up
23
+ # @param env [Hash] provide env and wallet_port,
24
+ # e.g. { env: 'mainnet', wallet_port: '8090', session_name: '0' }
25
+ # @raise CardanoUp::EnvNotSupportedError
26
+ # @raise CardanoUp::WalletPortError
27
+ def self.setup(opt = { env: 'mainnet', wallet_port: '8090', session_name: '0' })
28
+ env = opt[:env]
29
+ raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include? env
30
+
31
+ wallet_port = opt[:wallet_port]
32
+ raise CardanoUp::WalletPortError if wallet_port.nil? || wallet_port.empty?
33
+
34
+ session_name = opt[:session_name] || '0'
35
+
36
+ token_metadata_server = env == 'mainnet' ? CardanoUp::MAINNET_TOKEN_SERVER : CardanoUp::TESTNET_TOKEN_SERVER
37
+
38
+ CardanoUp.configure_default unless CardanoUp.configured?
39
+ configs = CardanoUp.config
40
+ bin_dir = configs[:bin_dir]
41
+ config_dir = File.join(configs[:config_dir], env)
42
+ log_dir = File.join(configs[:log_dir], session_name, env)
43
+ state_dir = File.join(configs[:state_dir], session_name, env)
44
+ wallet_db_dir = File.join(state_dir, 'wallet-db')
45
+ node_db_dir = File.join(state_dir, 'node-db')
46
+ [bin_dir, config_dir, log_dir, state_dir, wallet_db_dir, node_db_dir].each do |dir|
47
+ FileUtils.mkdir_p(dir)
48
+ end
49
+
50
+ node_socket = if CardanoUp::Utils.win?
51
+ "\\\\.\\pipe\\cardano-node-#{env}-#{session_name}"
52
+ else
53
+ File.join(state_dir, 'node.socket')
54
+ end
55
+ network = env == 'mainnet' ? '--mainnet' : "--testnet #{File.join(config_dir, 'byron-genesis.json')}"
56
+
57
+ {
58
+ env: env,
59
+ wallet_port: wallet_port,
60
+ token_metadata_server: token_metadata_server,
61
+ bin_dir: bin_dir,
62
+ config_dir: config_dir,
63
+ log_dir: log_dir,
64
+ state_dir: state_dir,
65
+ wallet_db_dir: wallet_db_dir,
66
+ node_db_dir: node_db_dir,
67
+ node_socket: node_socket,
68
+ network: network,
69
+ session_name: session_name
70
+ }
71
+ end
72
+
73
+ # @param configuration [Hash] output of setup
74
+ # @raise CardanoUp::SessionHasNodeError
75
+ # @raise CardanoUp::SessionHasWalletError
76
+ # @raise CardanoUp::NoScreenError
77
+ def self.node_up(configuration)
78
+ env = configuration[:env]
79
+ bin_dir = configuration[:bin_dir]
80
+ config_dir = configuration[:config_dir]
81
+ log_dir = configuration[:log_dir]
82
+ node_db_dir = configuration[:node_db_dir]
83
+ node_socket = configuration[:node_socket]
84
+ session_name = configuration[:session_name]
85
+
86
+ raise CardanoUp::NoScreenError if !CardanoUp::Utils.win? && !CardanoUp::Utils.screen?
87
+
88
+ exe = CardanoUp::Utils.win? ? '.exe' : ''
89
+ cardano_node = "#{File.join(bin_dir, 'cardano-node')}#{exe}"
90
+ version = CardanoUp::Utils.cmd "#{cardano_node} version"
91
+ node_cmd = ["#{cardano_node} run",
92
+ "--config #{File.join(config_dir, 'config.json')}",
93
+ "--topology #{File.join(config_dir, 'topology.json')}",
94
+ "--database-path #{node_db_dir}",
95
+ "--socket-path #{node_socket}"].join(' ')
96
+ node_service = if CardanoUp::Utils.win?
97
+ "cardano-node-#{env}-#{session_name}"
98
+ else
99
+ "NODE_#{env}_#{session_name}"
100
+ end
101
+ logfile = File.join(log_dir, 'node.log')
102
+ service_details = {
103
+ network: env,
104
+ node: {
105
+ service: node_service,
106
+ version: version,
107
+ log: logfile,
108
+ db_dir: node_db_dir,
109
+ socket_path: node_socket,
110
+ protocol_magic: get_protocol_magic(config_dir),
111
+ bin: node_cmd.split.first,
112
+ cmd: node_cmd
113
+ }
114
+ }
115
+ CardanoUp::Session.create_or_update(session_name, service_details)
116
+ if CardanoUp::Utils.win?
117
+ # Turn off p2p for Windows
118
+ # TODO: remove after https://github.com/input-output-hk/ouroboros-network/issues/3968 released
119
+ config_win = CardanoUp::Utils.from_json("#{config_dir}/config.json")
120
+ config_win[:EnableP2P] = false
121
+ CardanoUp::Utils.to_json("#{config_dir}/config.json", config_win)
122
+ topology = {
123
+ Producers: [
124
+ {
125
+ addr: "#{env}-node.world.dev.cardano.org",
126
+ port: 30_002,
127
+ valency: 2
128
+ }
129
+ ]
130
+ }
131
+ CardanoUp::Utils.to_json("#{config_dir}/topology.json", topology)
132
+
133
+ # create cardano-node.bat file
134
+ File.write("#{bin_dir}/cardano-node.bat", node_cmd)
135
+ install_node = "nssm install #{node_service} #{bin_dir}/cardano-node.bat"
136
+ log_stdout_node = "nssm set #{node_service} AppStdout #{logfile}"
137
+ log_stderr_node = "nssm set #{node_service} AppStderr #{logfile}"
138
+ start_node = "nssm start #{node_service}"
139
+
140
+ CardanoUp::Utils.cmd install_node
141
+ CardanoUp::Utils.cmd log_stdout_node
142
+ CardanoUp::Utils.cmd log_stderr_node
143
+ CardanoUp::Utils.cmd start_node
144
+ else
145
+ screen_cmd = "screen -dmS #{node_service} -L -Logfile #{logfile} #{node_cmd}"
146
+ CardanoUp::Utils.cmd screen_cmd
147
+ end
148
+ service_details
149
+ end
150
+
151
+ # @param configuration [Hash] output of setup
152
+ # @raise CardanoUp::NoScreenError
153
+ # @raise CardanoUp::WalletPortUsedError
154
+ def self.wallet_up(configuration)
155
+ if CardanoUp::Utils.port_used?(configuration[:wallet_port].to_i)
156
+ raise CardanoUp::WalletPortUsedError, configuration[:wallet_port]
157
+ end
158
+
159
+ raise CardanoUp::NoScreenError if !CardanoUp::Utils.win? && !CardanoUp::Utils.screen?
160
+
161
+ env = configuration[:env]
162
+ wallet_port = configuration[:wallet_port]
163
+ token_metadata_server = configuration[:token_metadata_server]
164
+ bin_dir = configuration[:bin_dir]
165
+ log_dir = configuration[:log_dir]
166
+ wallet_db_dir = configuration[:wallet_db_dir]
167
+ node_socket = configuration[:node_socket]
168
+ network = configuration[:network]
169
+ session_name = configuration[:session_name]
170
+
171
+ exe = CardanoUp::Utils.win? ? '.exe' : ''
172
+ cardano_wallet = "#{File.join(bin_dir, 'cardano-wallet')}#{exe}"
173
+ wallet_cmd = ["#{cardano_wallet} serve",
174
+ "--port #{wallet_port}",
175
+ "--node-socket #{node_socket}",
176
+ network.to_s,
177
+ "--database #{wallet_db_dir}",
178
+ "--token-metadata-server #{token_metadata_server}"].join(' ')
179
+ version = CardanoUp::Utils.cmd "#{bin_dir}/cardano-wallet#{exe} version"
180
+ wallet_service = if CardanoUp::Utils.win?
181
+ "cardano-wallet-#{env}-#{session_name}"
182
+ else
183
+ "WALLET_#{env}_#{session_name}"
184
+ end
185
+ logfile = File.join(log_dir, 'wallet.log')
186
+ service_details = {
187
+ network: env,
188
+ wallet: {
189
+ service: wallet_service,
190
+ version: version,
191
+ log: logfile,
192
+ db_dir: wallet_db_dir,
193
+ port: wallet_port.to_i,
194
+ url: "http://localhost:#{wallet_port}/v2",
195
+ bin: wallet_cmd.split.first,
196
+ cmd: wallet_cmd
197
+ }
198
+ }
199
+
200
+ CardanoUp::Session.create_or_update(session_name, service_details)
201
+ if CardanoUp::Utils.win?
202
+ # create cardano-wallet.bat file
203
+ File.write("#{bin_dir}/cardano-wallet.bat", wallet_cmd)
204
+ install_wallet = "nssm install #{wallet_service} #{bin_dir}/cardano-wallet.bat"
205
+ log_stdout_wallet = "nssm set #{wallet_service} AppStdout #{logfile}"
206
+ log_stderr_wallet = "nssm set #{wallet_service} AppStderr #{logfile}"
207
+ start_wallet = "nssm start #{wallet_service}"
208
+
209
+ CardanoUp::Utils.cmd install_wallet
210
+ CardanoUp::Utils.cmd log_stdout_wallet
211
+ CardanoUp::Utils.cmd log_stderr_wallet
212
+ CardanoUp::Utils.cmd start_wallet
213
+ else
214
+ CardanoUp::Utils.cmd "screen -dmS #{wallet_service} -L -Logfile #{logfile} #{wallet_cmd}"
215
+ end
216
+
217
+ service_details
218
+ end
219
+
220
+ # @raise CardanoUp::EnvNotSupportedError
221
+ # @raise CardanoUp::NoScreenError
222
+ def self.node_down(env, session_name = '0')
223
+ raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include? env
224
+
225
+ raise CardanoUp::NoScreenError if !CardanoUp::Utils.win? && !CardanoUp::Utils.screen?
226
+
227
+ if CardanoUp::Utils.win?
228
+ CardanoUp::Utils.cmd "nssm stop cardano-node-#{env}-#{session_name}"
229
+ CardanoUp::Utils.cmd "nssm remove cardano-node-#{env}-#{session_name} confirm"
230
+ else
231
+ CardanoUp::Utils.cmd "screen -S NODE_#{env}_#{session_name} -X at '0' stuff '^C'"
232
+ CardanoUp::Utils.cmd "screen -XS NODE_#{env}_#{session_name} quit"
233
+ end
234
+ CardanoUp::Session.remove(session_name, { network: env, service: 'node' })
235
+ end
236
+
237
+ # @raise CardanoUp::EnvNotSupportedError
238
+ # @raise CardanoUp::NoScreenError
239
+ def self.wallet_down(env, session_name = '0')
240
+ raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include? env
241
+
242
+ raise CardanoUp::NoScreenError if !CardanoUp::Utils.win? && !CardanoUp::Utils.screen?
243
+
244
+ if CardanoUp::Utils.win?
245
+ CardanoUp::Utils.cmd "nssm stop cardano-wallet-#{env}-#{session_name}"
246
+ CardanoUp::Utils.cmd "nssm remove cardano-wallet-#{env}-#{session_name} confirm"
247
+ else
248
+ CardanoUp::Utils.cmd "screen -S WALLET_#{env}_#{session_name} -X at '0' stuff '^C'"
249
+ CardanoUp::Utils.cmd "screen -XS WALLET_#{env}_#{session_name} quit"
250
+ end
251
+ CardanoUp::Session.remove(session_name, { network: env, service: 'wallet' })
252
+ end
253
+
254
+ ##
255
+ # Get protocol magic from config's byron-genesis.json
256
+ def get_protocol_magic(config)
257
+ byron_genesis = CardanoUp::Utils.from_json(File.join(config, 'byron-genesis.json'))
258
+ byron_genesis[:protocolConsts][:protocolMagic].to_i
259
+ end
260
+ module_function :get_protocol_magic
261
+ private_class_method :get_protocol_magic
262
+ end
263
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardanoUp
4
+ # Check status (ping) of running node and wallet services
5
+ module Ping
6
+ # @param session_name [String]
7
+ # @param env [String]
8
+ # @raise CardanoUp::EnvNotSupportedError
9
+ # @raise CardanoUp::SessionNotExistsError
10
+ # @raise CardanoUp::SessionServiceNotUpError
11
+ # @raise CardanoUp::SessionEnvNotUpError
12
+ def self.wallet(session_name, env)
13
+ CardanoUp::Configs.exist?(env)
14
+ CardanoUp::Session.network_or_raise?(session_name, env.to_sym)
15
+ CardanoUp::Session.wallet_or_raise?(session_name, env.to_sym)
16
+ session = CardanoUp::Session.get(session_name)
17
+
18
+ url = session[env.to_sym][:wallet][:url]
19
+ r = HTTParty.get("#{url}/network/information")
20
+ [r.parsed_response, r.code]
21
+ end
22
+
23
+ # @param session_name [String]
24
+ # @param env [String]
25
+ # @raise CardanoUp::EnvNotSupportedError
26
+ # @raise CardanoUp::SessionNotExistsError
27
+ # @raise CardanoUp::SessionServiceNotUpError
28
+ # @raise CardanoUp::SessionEnvNotUpError
29
+ def self.node(session_name, env)
30
+ CardanoUp::Configs.exist?(env)
31
+ CardanoUp::Session.network_or_raise?(session_name, env.to_sym)
32
+ CardanoUp::Session.node_or_raise?(session_name, env.to_sym)
33
+ session = CardanoUp::Session.get(session_name)
34
+ bin_dir = CardanoUp.config[:bin_dir]
35
+
36
+ ENV['CARDANO_NODE_SOCKET_PATH'] = session[env.to_sym][:node][:socket_path]
37
+ protocol_magic = session[env.to_sym][:node][:protocol_magic]
38
+ network_arg = env == 'mainnet' ? '--mainnet' : "--testnet-magic #{protocol_magic}"
39
+ exe = CardanoUp::Utils.win? ? '.exe' : ''
40
+ cmd = "#{File.join(bin_dir, 'cardano-cli')}#{exe} query tip #{network_arg}"
41
+ res = CardanoUp::Utils.cmd cmd
42
+ begin
43
+ r = JSON.parse(res)
44
+ c = 200
45
+ rescue JSON::ParserError
46
+ r = 'Not responding.'
47
+ c = 404
48
+ end
49
+ [r, c]
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardanoUp
4
+ # Basic sessions management
5
+ module Session
6
+ # Check session file exists
7
+ def self.exists?(session_name)
8
+ CardanoUp.configure_default unless CardanoUp.configured?
9
+ File.exist?(session_file_path(session_name))
10
+ end
11
+
12
+ # get session contents
13
+ def self.get(session_name)
14
+ raise CardanoUp::SessionNotExistsError, session_name unless exists?(session_name)
15
+
16
+ CardanoUp.configure_default unless CardanoUp.configured?
17
+ session = session_file_path(session_name)
18
+ CardanoUp::Utils.from_json(session)
19
+ end
20
+
21
+ def self.destroy!(session_name)
22
+ path = session_file_path(session_name)
23
+ FileUtils.rm_f(path)
24
+ end
25
+
26
+ def self.destroy_all!
27
+ list_all.each do |path|
28
+ FileUtils.rm_f(path)
29
+ end
30
+ end
31
+
32
+ def self.list_all
33
+ Dir[File.join(CardanoUp.base_dir, '.session*')]
34
+ end
35
+
36
+ # create or update session with details of launched service
37
+ # @param session_name [String] session name
38
+ # @param service_details [Hash] service details
39
+ # @raise ArgumentError
40
+ # @raise CardanoUp::SessionHasNodeError
41
+ # @raise CardanoUp::SessionHasWalletError
42
+ def self.create_or_update(session_name, service_details)
43
+ raise ArgumentError, 'service_details should be Hash' unless service_details.is_a?(Hash)
44
+ raise ArgumentError, 'service_details shoud have :network' if service_details[:network].nil?
45
+
46
+ service_network = service_details[:network].to_sym || :unknown
47
+
48
+ if !exists?(session_name) || empty?(session_name)
49
+ # if session is not set then just create new with just service_details
50
+ content = { service_network => service_details }
51
+ put_content(session_name, content)
52
+ elsif !network?(session_name, service_network)
53
+ # if session exists but has no network, add new network
54
+ existing_services = get(session_name)
55
+ new_service = { service_network => service_details }
56
+ put_content(session_name, existing_services.merge(new_service))
57
+ else
58
+ # if session has network and node already, you can add only wallet
59
+ if service_details.key?(:node) && node?(session_name, service_network)
60
+ raise CardanoUp::SessionHasNodeError.new(session_name, service_network)
61
+ end
62
+
63
+ # if session has network and wallet already, you can add only node
64
+ if service_details.key?(:wallet) && wallet?(session_name, service_network)
65
+ raise CardanoUp::SessionHasWalletError.new(session_name,
66
+ service_network)
67
+ end
68
+
69
+ existing_services = get(session_name)
70
+ updated_network = existing_services[service_network].merge(service_details)
71
+ existing_services[service_network] = updated_network
72
+ put_content(session_name, existing_services)
73
+ end
74
+ end
75
+
76
+ def self.node_or_raise?(session_name, env)
77
+ raise CardanoUp::SessionServiceNotUpError.new(session_name, env, :node) unless node?(session_name, env)
78
+
79
+ true
80
+ end
81
+
82
+ def self.wallet_or_raise?(session_name, env)
83
+ raise CardanoUp::SessionServiceNotUpError.new(session_name, env, :wallet) unless wallet?(session_name, env)
84
+
85
+ true
86
+ end
87
+
88
+ def self.network_or_raise?(session_name, env)
89
+ raise CardanoUp::SessionEnvNotUpError.new(session_name, env) unless network?(session_name, env)
90
+
91
+ true
92
+ end
93
+
94
+ # remove entry from session
95
+ # @param session_name [String] session name
96
+ # @param service_details [Hash] service details ({ network: 'preprod', service: 'wallet' })
97
+ def self.remove(session_name, service_details)
98
+ existing_services = get(session_name)
99
+ service = service_details[:service].to_sym
100
+ network = service_details[:network].to_sym
101
+ if existing_services && existing_services[network]
102
+ existing_services[network].delete(service)
103
+
104
+ unless existing_services[network].key?(:node) || existing_services[network].key?(:wallet)
105
+ existing_services.delete(network)
106
+ end
107
+ put_content(session_name, existing_services)
108
+ end
109
+
110
+ destroy!(session_name) if empty?(session_name)
111
+ end
112
+
113
+ def session_file_path(session_name)
114
+ File.join(CardanoUp.base_dir, ".session-#{session_name}.json")
115
+ end
116
+ module_function :session_file_path
117
+ private_class_method :session_file_path
118
+
119
+ def put_content(session_name, contents)
120
+ raise ArgumentError, 'contents should be Hash' unless contents.is_a?(Hash)
121
+
122
+ path = session_file_path(session_name)
123
+ File.write(path, JSON.pretty_generate(contents))
124
+ path
125
+ end
126
+ module_function :put_content
127
+ private_class_method :put_content
128
+
129
+ def empty?(session_name)
130
+ get(session_name) == {}
131
+ end
132
+ module_function :empty?
133
+ private_class_method :empty?
134
+
135
+ def network?(session_name, env)
136
+ get(session_name).key?(env.to_sym)
137
+ end
138
+ module_function :network?
139
+ private_class_method :network?
140
+
141
+ def node?(session_name, env)
142
+ get(session_name)[env.to_sym].key?(:node)
143
+ end
144
+ module_function :node?
145
+ private_class_method :node?
146
+
147
+ def wallet?(session_name, env)
148
+ get(session_name)[env.to_sym].key?(:wallet)
149
+ end
150
+ module_function :wallet?
151
+ private_class_method :wallet?
152
+ end
153
+ end
@@ -8,7 +8,12 @@ module CardanoUp
8
8
  res = `#{cmd}`
9
9
  puts cmd if display_result
10
10
  puts res if display_result
11
- res.gsub("\n", '')
11
+ res.gsub("\n", ' ').strip
12
+ end
13
+
14
+ # Check if screen command is available
15
+ def self.screen?
16
+ cmd('which screen').chomp.length.positive?
12
17
  end
13
18
 
14
19
  def self.wget(url, file = nil)
@@ -17,6 +22,31 @@ module CardanoUp
17
22
  File.binwrite(file, resp.body)
18
23
  end
19
24
 
25
+ # Check if port is already used
26
+ def self.port_used?(port)
27
+ begin
28
+ Timeout.timeout(1) do
29
+ s = TCPSocket.new('localhost', port)
30
+ s.close
31
+ return true
32
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
33
+ return false
34
+ end
35
+ rescue Timeout::Error
36
+ # do nothing
37
+ end
38
+
39
+ false
40
+ end
41
+
42
+ def self.from_json(file)
43
+ JSON.parse(File.read(file), { symbolize_names: true })
44
+ end
45
+
46
+ def self.to_json(file, hash)
47
+ File.write(file, JSON.pretty_generate(hash))
48
+ end
49
+
20
50
  def self.win?
21
51
  RUBY_PLATFORM =~ /cygwin|mswin|mingw|bccwin|wince|emx/
22
52
  end
@@ -36,40 +66,21 @@ module CardanoUp
36
66
 
37
67
  ##
38
68
  # Latest binary url for latest release or particular tag, from master or pr num.
39
- # @param release [String] - 'latest' | /^v20.{2}-.{2}-.{2}/ | 'master' | '3341'
69
+ # @param release [String] - 'latest' | /^v20.{2}-.{2}-.{2}/
40
70
  # @raise CardanoUp::VersionNotSupportedError
41
71
  def self.get_binary_url(release = 'latest')
42
- unless release == 'master' || release == 'latest' || release =~ /^v20.{2}-.{2}-.{2}/ || release =~ /^\d+$/
43
- raise CardanoUp::VersionNotSupportedError, release
44
- end
72
+ raise CardanoUp::VersionNotSupportedError, release unless release == 'latest' || release =~ /^v20.{2}-.{2}-.{2}/
45
73
 
46
- url = ''
47
- if release == 'latest' || release =~ /^v20.{2}-.{2}-.{2}/
48
- tag = release == 'latest' ? latest_tag : release
49
- if linux?
50
- file = "cardano-wallet-#{tag}-linux64.tar.gz"
51
- elsif mac?
52
- file = "cardano-wallet-#{tag}-macos-intel.tar.gz"
53
- elsif win?
54
- file = "cardano-wallet-#{tag}-win64.zip"
55
- end
56
- url = "#{CardanoUp::BINS_BASE_URL}/releases/download/#{tag}/#{file}"
57
- else
58
- if linux?
59
- os = 'linux.musl.cardano-wallet-linux64'
60
- elsif mac?
61
- os = 'macos.intel.cardano-wallet-macos-intel'
62
- elsif win?
63
- os = 'linux.windows.cardano-wallet-win64'
64
- end
65
-
66
- url = if release == 'master'
67
- "#{CardanoUp::HYDRA_BASE_URL}/#{os}/latest/download-by-type/file/binary-dist"
68
- else
69
- "#{CardanoUp::HYDRA_BASE_URL}-pr-#{release}/#{os}/latest/download-by-type/file/binary-dist"
70
- end
74
+ tag = release == 'latest' ? latest_tag : release
75
+ if linux?
76
+ file = "cardano-wallet-#{tag}-linux64.tar.gz"
77
+ elsif mac?
78
+ file = "cardano-wallet-#{tag}-macos-intel.tar.gz"
79
+ elsif win?
80
+ file = "cardano-wallet-#{tag}-win64.zip"
71
81
  end
72
- url
82
+
83
+ "#{CardanoUp::BINS_BASE_URL}/releases/download/#{tag}/#{file}"
73
84
  end
74
85
 
75
86
  ##
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Version
4
4
  module CardanoUp
5
- VERSION ||= '0.1.1'
5
+ VERSION ||= '0.1.3'
6
6
  end