-
Notifications
You must be signed in to change notification settings - Fork 281
/
replace_secrets.rb
executable file
·71 lines (54 loc) · 1.48 KB
/
replace_secrets.rb
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
#!/usr/bin/env ruby
##
## This Script loads a collection of (JSON Encoded) secrets, and performs replacement operations
## over a given Template.
##
## Note that in order for the Replacement OP's to work, your placeholders should look like this:
##
## %{SIMPLENOTE_CREDENTIALS}
##
require 'json'
require 'optparse'
## Parse Input Parameters
##
options = {}
optparse = OptionParser.new do |opts|
opts.on('-s', '--secrets file', 'Secrets filename (must be in JSON format!)') do |secrets|
options[:secrets] = secrets
end
opts.on('-i', '--input file', 'Input filename') do |input|
options[:input] = input
end
end
optparse.parse!
## Validate Parameters
##
for parameter in [:secrets, :input]
filename = options[parameter]
if filename.nil? == true || File.exists?(filename) == false then
puts optparse
exit
end
end
## Loads the Secrets at the specified Path. This must be a JSON Valid file!
##
def load(secrets_path)
raw_secrets = File.read(secrets_path)
output = JSON.parse(raw_secrets, :symbolize_names => true)
output[:timestamp] = Time.now.strftime("%b %d, %Y at %H:%M:%S")
return output
end
## Loads the Template at the specified path, and applies Placeholder Replacement OP's, with the secrets
## located at a given path.
##
def process(template_path, secrets_path)
secrets = load(secrets_path)
template = File.open(template_path, "r")
template.each_line { |line|
puts line % secrets
}
template.close
end
## Main!
##
process(options[:input], options[:secrets])