Top Level Namespace

Defined Under Namespace

Classes: Trocla

Instance Method Summary collapse

Instance Method Details

#check_format(format_name) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'bin/trocla', line 129

def check_format(format_name)
  if format_name.nil?
    STDERR.puts 'Missing format, exiting...'
    exit 1
  elsif !Trocla::Formats.available?(format_name)
    STDERR.puts "Error: The format #{format_name} is not available"
    exit 1
  end
end

#create(options) ⇒ Object



48
49
50
51
52
53
54
# File 'bin/trocla', line 48

def create(options)
  [Trocla.new(options.delete(:config_file)).password(
    options.delete(:trocla_key),
    options.delete(:trocla_format),
    options.merge(YAML.safe_load(options.delete(:other_options).shift.to_s) || {})
  ), 0]
end

#delete(options) ⇒ Object



101
102
103
104
105
106
107
# File 'bin/trocla', line 101

def delete(options)
  res = Trocla.new(options.delete(:config_file)).delete_password(
    options.delete(:trocla_key),
    options.delete(:trocla_format)
  )
  [res, res.nil? ? 1 : 0]
end

#formats(options) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'bin/trocla', line 109

def formats(options)
  key = (options.delete(:trocla_key) || '')
  if key.empty?
    "Available formats: #{Trocla::Formats.all.join(', ')}"
  else
    res = Trocla.new(options.delete(:config_file)).available_format(
      key,
      options.merge(YAML.safe_load(options.delete(:other_options).shift.to_s) || {})
    )
    [res.nil? ? res : res.join(', '), res.nil? ? 1 : 0]
  end
end

#get(options) ⇒ Object



56
57
58
59
60
61
62
63
# File 'bin/trocla', line 56

def get(options)
  res = Trocla.new(options.delete(:config_file)).get_password(
    options.delete(:trocla_key),
    options.delete(:trocla_format),
    options.merge(YAML.safe_load(options.delete(:other_options).shift.to_s) || {})
  )
  [res, res.nil? ? 1 : 0]
end

#reset(options) ⇒ Object



93
94
95
96
97
98
99
# File 'bin/trocla', line 93

def reset(options)
  [Trocla.new(options.delete(:config_file)).reset_password(
    options.delete(:trocla_key),
    options.delete(:trocla_format),
    options.merge(YAML.safe_load(options.delete(:other_options).shift.to_s) || {})
  ), 0]
end

#search(options) ⇒ Object



122
123
124
125
126
127
# File 'bin/trocla', line 122

def search(options)
  res = Trocla.new(options.delete(:config_file)).search_key(
    options.delete(:trocla_key)
  )
  [res.nil? ? res : res.join("\n"), res.nil? ? 1 : 0]
end

#set(options) ⇒ Object



65
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
# File 'bin/trocla', line 65

def set(options)
  if options.delete(:ask_password)
    require 'highline/import'
    password = ask('Enter your password: ') { |q| q.echo = 'x' }.to_s
    pwd2 = ask('Repeat password: ') { |q| q.echo = 'x' }.to_s
    unless password == pwd2
      STDERR.puts 'Passwords did not match, exiting!'
      return [nil, 1]
    end
  else
    password = options.delete(:password) || STDIN.read.chomp
  end
  format = options.delete(:trocla_format)
  no_format = options.delete('no_format')
  trocla = Trocla.new(options.delete(:config_file))
  value = if no_format
            password
          else
            trocla.formats(format).format(password, (YAML.safe_load(options.delete(:other_options).shift.to_s) || {}))
          end
  trocla.set_password(
    options.delete(:trocla_key),
    format,
    value
  )
  ['', 0]
end