summaryrefslogtreecommitdiff
path: root/bin/trocla
blob: dd32f844aaf332961de5e96fc0a3ab5eba7aea8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env ruby
# CLI client for Trocla.
#
require 'rubygems'
require 'trocla'
require 'optparse'
require 'yaml'

options = { :config_file => nil, :ask_password => true }

OptionParser.new do |opts|
  opts.on("--version", "-V", "Version information") do
    puts Trocla::VERSION::STRING
    exit
  end

  opts.on("--config CONFIG", "-c", "Configuration file") do |v|
    if File.exist?(v)
      options[:config_file] = v
    else
      STDERR.puts "Cannot find config file: #{v}"
      exit 1
    end
  end

  opts.on("--no-random") do
    options['random'] = false
  end

  opts.on("--length LENGTH") do |v|
    options['length'] = v.to_i
  end

  opts.on("--password [PASSWORD]", "-p", "Provide password at command line") do |pass|
    options[:ask_password] = false
    options[:password] = pass
  end

end.parse!

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

def get(options)
  Trocla.new(options.delete(:config_file)).get_password(
    options.delete(:trocla_key),
    options.delete(:trocla_format)
  )
end
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!"
      exit 1
    end
  else
    password = options.delete(:password) || STDIN.read.chomp
  end
  format = options.delete(:trocla_format)
  Trocla.new(options.delete(:config_file)).set_password(
    options.delete(:trocla_key),
    format,
    Trocla::Formats[format].format(password, options.delete(:other_options).shift.to_s)
  )
  ""
end

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

def delete(options)
  Trocla.new(options.delete(:config_file)).delete_password(
    options.delete(:trocla_key),
    options.delete(:trocla_format)
  )
end

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

actions=['create','get','set','reset','delete']

if !(ARGV.length < 2) && (action=ARGV.shift) && actions.include?(action)
    options[:trocla_key] = ARGV.shift
    options[:trocla_format] = ARGV.shift
    options[:other_options] = ARGV
    check_format(options[:trocla_format]) unless action == 'delete'
    begin
      if result = send(action,options)
        puts result.is_a?(String) ? result : result.inspect
      end
    rescue Exception => e
      STDERR.puts "Action failed with the following message: #{e.message}" unless e.message == 'exit'
      exit 1
    end
else
    STDERR.puts "Please supply one of the following actions: #{actions.join(', ')}"
    exit 1
end