keyth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/keyth_admin +37 -0
  2. data/lib/keyth.rb +100 -0
  3. metadata +49 -0
data/bin/keyth_admin ADDED
@@ -0,0 +1,37 @@
1
+ #! /usr/bin/ruby
2
+ # encoding: utf-8
3
+ require 'keyth'
4
+
5
+ ADD_USAGE = 'keyth_admin add <keyname> <keyvalue>'
6
+ DELETE_USAGE = 'keyth_admin delete <keyname>'
7
+ LIST_USAGE = 'keyth_admin list [namespace]'
8
+
9
+ def show_errors(errors)
10
+ return if errors.size == 0
11
+ puts 'Usage: ' + errors[0]
12
+ errors[1..-1].each { |e| puts ' ' + e }
13
+ exit 1
14
+ end
15
+
16
+ def check_params(params)
17
+ case params[0]
18
+ when 'add'
19
+ show_errors([ADD_USAGE]) unless params[1] && params[2]
20
+ when 'delete'
21
+ show_errors([DELETE_USAGE]) unless params[1]
22
+ when 'list'
23
+ else
24
+ show_errors([ADD_USAGE, DELETE_USAGE, LIST_USAGE])
25
+ end
26
+ end
27
+
28
+ check_params(ARGV)
29
+
30
+ case ARGV[0]
31
+ when 'add'
32
+ Keyth.add_key(ARGV[1], ARGV[2])
33
+ when 'delete'
34
+ Keyth.delete_key(ARGV[1])
35
+ when 'list'
36
+ Keyth.keys(ARGV[1]).each { |k, v| puts "#{k}=#{v}" }
37
+ end
data/lib/keyth.rb ADDED
@@ -0,0 +1,100 @@
1
+ # encoding: utf-8
2
+ require 'yaml'
3
+
4
+ # Keyhandling module with various functions for keeping keys
5
+ # out of configuration files.
6
+ module Keyth
7
+ def self.get_key(key)
8
+ load_keyfile unless @key_list
9
+ application, key_name = key.split('/')
10
+ fail "Application not found: #{application}!" unless @key_list[application]
11
+ fail "Key not found: #{key}!" unless @key_list[application][key_name]
12
+ @key_list[application][key_name]
13
+ end
14
+
15
+ def self.get_key_safe(key)
16
+ get_key(key) rescue nil
17
+ end
18
+
19
+ def self.add_key(key, value)
20
+ load_keyfile unless @key_list
21
+ application, key_name = key.split('/')
22
+ @key_list[application] = {} unless @key_list.key?(application)
23
+ @key_list[application][key_name] = value
24
+ save_keyfile
25
+ end
26
+
27
+ def self.delete_key(key)
28
+ load_keyfile unless @key_list
29
+ application, key_name = key.split('/')
30
+ @key_list[application].delete(key_name)
31
+ @key_list.delete(application) if @key_list[application].empty?
32
+ save_keyfile
33
+ end
34
+
35
+ def self.keys(application = nil)
36
+ load_keyfile unless @key_list
37
+ keys = {}
38
+ @key_list.each do |k1, v|
39
+ v.each do |k2, v2|
40
+ keys[k1 + '/' + k2] = v2 if k1 == application || application.nil?
41
+ end
42
+ end
43
+ keys
44
+ end
45
+
46
+ def self.load_yaml(file)
47
+ load_keyfile unless @key_list
48
+ keys = YAML.load(file)
49
+ fetch_keys(keys)
50
+ end
51
+
52
+ def self.fetch_keys(obj)
53
+ load_keyfile unless @key_list
54
+ case
55
+ when obj.respond_to?(:keys)
56
+ obj.each do |k, v|
57
+ obj[k] = fetch_keys(v)
58
+ end
59
+ when obj.respond_to?(:each)
60
+ obj.each do |v|
61
+ fetch_keys(v)
62
+ end
63
+ when obj.is_a?(String)
64
+ obj.gsub!(/^keyth\:(.*)/) { get_key(Regexp.last_match[1]) }
65
+ end
66
+ obj
67
+ end
68
+
69
+ def self.namespace(namespace)
70
+ @namespace = namespace
71
+ @keylist = nil
72
+ @namespace
73
+ end
74
+
75
+ def self.keyfile_location
76
+ @namespace = 'default' unless @namespace
77
+ ENV['KEYTH_KEYFILE'] || File.join(Dir.home, '.keyth', @namespace + '.yml')
78
+ end
79
+
80
+ def self.load_keyfile
81
+ if File.file?(keyfile_location)
82
+ @key_list = YAML.load(File.open(keyfile_location))
83
+ else
84
+ @key_list = {}
85
+ end
86
+ end
87
+
88
+ def self.save_keyfile
89
+ load_keyfile unless @key_list
90
+ File.open(keyfile_location, 'w') { |f| f.write @key_list.to_yaml }
91
+ end
92
+ end
93
+
94
+ # puts Keyth.fetch_keys 'keyth:es-upbeat-home-dev/NEW_RELIC_LICENSE'
95
+
96
+ # keys = YAML.load(File.open('../my_deets.yml'))
97
+ # keys = Keyth.fetch_keys(keys)
98
+ # puts keys.to_s
99
+
100
+ # puts Keyth.keys.to_s
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keyth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - K M Lawrence
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Handles named keys for use in config files
15
+ email: keith@kludge.co
16
+ executables:
17
+ - keyth_admin
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/keyth.rb
22
+ - bin/keyth_admin
23
+ homepage: http://rubygems.org/gems/keyth
24
+ licenses:
25
+ - MIT
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Keyth!
48
+ test_files: []
49
+ has_rdoc: