forked from duritong/trocla
/
trocla
executable file
·136 lines (117 loc) · 3.48 KB
/
trocla
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env ruby
# CLI client for Trocla.
#
require 'rubygems'
require 'trocla'
require 'optparse'
require 'yaml'
options = { :config_file => nil, :ask_password => true, 'random' => false }
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("--random", "-r", "Generate random password") do
options['random'] = true
options[:ask_password] = false
end
opts.on("--no-random", "Do not generate random password") do
options['random'] = false
end
opts.on("--length LENGTH", "-l", "Specify the length of password") 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
opts.on("--pwd-from-stdin", "Read password from STDIN") do
options[:ask_password] = false
end
end.parse!
def create(options)
check_format(options[:trocla_format])
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)
check_format(options[:trocla_format])
Trocla.new(options.delete(:config_file)).get_password(
options.delete(:trocla_key),
options.delete(:trocla_format)
)
end
def set(options)
check_format(options[:trocla_format])
if options.delete(:ask_password)
require 'highline/import'
password = ask("Enter your password: ") { |q| q.echo = "x" }
pwd2 = ask("Repeat password: ") { |q| q.echo = "x" }
unless password == pwd2
STDERR.puts "Passwords did not match, exiting!"
exit 1
end
else
# FIXME: I don't know if we have to delete other options here.
password = options.delete(:password)
password = STDIN.read if not password
end
Trocla.new(options.delete(:config_file)).set_password(
options.delete(:trocla_key),
options.delete(:trocla_format),
password
)
""
end
def reset(options)
check_format(options[:trocla_format])
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 not Trocla::Formats.available?(format_name)
STDERR.puts ":: Error: The format #{format_name} is not available"
exit 1
end
false
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
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