Module: UniversaTools::Commons

Included in:
Uniring, KeyRing
Defined in:
lib/universa_tools/commons.rb

Defined Under Namespace

Modules: CliCommands

Constant Summary collapse

ALNUMS =
(alnums + alnums.downcase + '_' + '0123456789').chars.to_ary
NUMBERS =
"0123456789".chars.to_ary

Instance Method Summary collapse

Instance Method Details

#create_temp_file_name(root_path, extension) ⇒ Object



125
126
127
128
129
130
# File 'lib/universa_tools/commons.rb', line 125

def create_temp_file_name root_path, extension
  loop do
    name = "#{root_path}/#{17.random_alnums}.#{extension}"
    return name if !File.exists?(name)
  end
end

#error(message) ⇒ Object



153
154
155
# File 'lib/universa_tools/commons.rb', line 153

def error(message)
  raise UniversaTools::MessageException, message
end

#error_style(message = nil) ⇒ Object



53
54
55
56
# File 'lib/universa_tools/commons.rb', line 53

def error_style(message = nil)
  message ||= yield
  ANSI.bold { ANSI.red { message } }
end

#human_to_i(value, factor = 1000) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/universa_tools/commons.rb', line 8

def human_to_i value, factor = 1000
  head, tail = value[0...-1], value[-1]
  case tail
    when 'k', 'K'
      head.to_i * 1000
    when 'M', 'm'
      head.to_i * factor * factor
    when 'G', 'g'
      head.to_i * factor * factor * factor
    else
      value.to_t
  end
end

#load_contract(path) ⇒ Universa::Contract

Load contratc from the path

Parameters:

  • path (String)

    to load contract from

Returns:

  • (Universa::Contract)


135
136
137
# File 'lib/universa_tools/commons.rb', line 135

def load_contract(path)
  open(path, 'rb') { |f| Universa::Contract.from_packed(f.read) }
end

#load_private_key(path, password: nil) ⇒ Universa::PrivateKey

Load private key from file

Parameters:

  • path (String)

    to load key from

  • password (String) (defaults to: nil)

    optional password

Returns:

  • (Universa::PrivateKey)


144
145
146
147
148
149
150
151
# File 'lib/universa_tools/commons.rb', line 144

def load_private_key(path, password: nil)
  file_name = File.expand_path path
  if !File.exists?(file_name)
    file_name += ".private.unikey"
    raise NotFoundException.new(path) unless File.exists?(file_name)
  end
  open(file_name, 'rb') { |f| Universa::PrivateKey.from_packed(f.read, password: password) }
end

#run_options_parser(opt_parser, tasks_before_commands: false, check_empty: true, &command_parser) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/universa_tools/commons.rb', line 28

def run_options_parser(opt_parser, tasks_before_commands: false, check_empty: true, &command_parser)
  commands = opt_parser.order!
  if check_empty && @tasks.empty?
    puts opt_parser.banner
    puts "\nnothing to do. Use -h for help\n"
  else
    @tasks.each { |t| t.call } if tasks_before_commands
    command_parser&.call(commands)
    @tasks.each { |t| t.call } unless tasks_before_commands
  end
rescue MessageException, OptionParser::ParseError => e
  STDERR.puts ANSI.red { ANSI.bold { "\nError: #{e}\n" } }
  exit(1000)
rescue Interrupt
  exit(1010)
rescue
  STDERR.puts ANSI.red { "\n#{$!.backtrace.reverse.join("\n")}\n" }
  STDERR.puts ANSI.red { ANSI.bold { "Error: #$! (#{$!.class.name})" } }
  exit(2000)
end

#seconds_to_hms(seconds) ⇒ Object



22
23
24
25
26
# File 'lib/universa_tools/commons.rb', line 22

def seconds_to_hms seconds
  mm, ss = seconds.divmod(60)
  hh, mm = mm.divmod(60)
  "%d:%02d:%02d" % [hh, mm, ss]
end

#success_style(message = nil) ⇒ Object



58
59
60
61
# File 'lib/universa_tools/commons.rb', line 58

def success_style(message = nil)
  message ||= yield
  ANSI.bold { ANSI.green { message } }
end

#todo!(text) ⇒ Object

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/universa_tools/commons.rb', line 49

def todo!(text)
  raise NotImplementedError, text
end

#warning_style(message = nil) ⇒ Object



63
64
65
66
# File 'lib/universa_tools/commons.rb', line 63

def warning_style(message = nil)
  message ||= yield
  ANSI.bold { ANSI.yellow { message } }
end