state_mate 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a688ee124e85b425b1448494e3a59bb5244097d
4
- data.tar.gz: e3f8f643717a325cf6743c4f690cbf4f2bc45fef
3
+ metadata.gz: 7e41284705be1f86eac6d6e3d67e0867fae16c30
4
+ data.tar.gz: b89e28497de8c95c21226c873bcb0bc84d453139
5
5
  SHA512:
6
- metadata.gz: 55925c3df961ce4eef5df8d88e5f4e48a53173bce138321df7f1ac9dabe54a04cefc3143821c5c7ee683806cbe388fdc4fc3bcf19b73d98b8c4c5da6a410b0c2
7
- data.tar.gz: aec931eefee2567705e5a604bdde89be85967bf8c3c0d6f0bf7910275ebce938298ce005ab5b4e30e49ba7759783dc405698805abe1a3ce4c3119d4c9087df84
6
+ metadata.gz: fcf1b94b353247bad4191400d91ad633b32c1197e7b46a12c1d8ed2a81a5349d9fd3af3a72748a64d85c6b9fe22cc8465e2f8c8e2253abab523b6e18e3204f71
7
+ data.tar.gz: b766eeee693b372c6abf79a91d0e8eeb6a2d0913334196219982ac6030626e2f2ae7a9420fbfbf843760b9cd46fc47ce288f8377e26caa032ea7bdde4e23a889
@@ -0,0 +1,85 @@
1
+ require 'yaml'
2
+ require 'diffable_yaml'
3
+
4
+ require 'state_mate'
5
+ require 'state_mate/adapters/defaults'
6
+
7
+ module StateMate::Adapters::YAML
8
+ include StateMate::Adapters
9
+ register 'yaml'
10
+
11
+ @preorder = []
12
+
13
+ def self.preorder= keys
14
+ @preorder = keys
15
+ end
16
+
17
+ def self.parse_key key
18
+ # use the same key seperation as Defaults
19
+ StateMate::Adapters::Defaults.parse_key key
20
+ end
21
+
22
+ def self.cast_seg key_seg
23
+ if key_seg =~ /\A[0-9]+\z/
24
+ key_seg.to_i
25
+ else
26
+ key_seg
27
+ end
28
+ end
29
+
30
+ def self.read key, options = {}
31
+ filepath, key_segs = parse_key key
32
+
33
+ contents = File.read(File.expand_path(filepath))
34
+
35
+ value = ::YAML.load contents
36
+
37
+ key_segs.each do |seg|
38
+ seg = cast_seg seg
39
+
40
+ value = case value
41
+ when Hash, Array
42
+ value = value[seg]
43
+ else
44
+ nil
45
+ end
46
+ end
47
+
48
+ value
49
+ end
50
+
51
+ def self.write key, value, options = {}
52
+ StateMate.debug key: key, value: value, options: options
53
+
54
+ filepath, key_segs = parse_key key
55
+
56
+ new_root = if key_segs.length > 1
57
+ root = read filepath
58
+
59
+ deep_write! root, key_segs, value
60
+
61
+ root
62
+ else
63
+ value
64
+ end
65
+
66
+ content = DiffableYAML.dump new_root, preorder: @preorder
67
+
68
+ File.open(filepath, 'w') do |f|
69
+ f.write content
70
+ end
71
+ end
72
+
73
+ def self.deep_write! obj, key_segs, value
74
+ seg = cast_seg key_segs.first
75
+ rest = key_segs[1..-1]
76
+
77
+ if rest.empty?
78
+ obj[seg] = value
79
+
80
+ else
81
+ deep_write! obj[seg], rest, value
82
+
83
+ end
84
+ end
85
+ end # YAML
@@ -33,6 +33,7 @@ module StateMate::Adapters
33
33
  begin
34
34
  require "state_mate/adapters/#{ name }"
35
35
  rescue LoadError => e
36
+ StateMate.debug "failed to require adapter #{ name }", e
36
37
  end
37
38
 
38
39
  unless @@index.key? name
@@ -1,3 +1,3 @@
1
1
  module StateMate
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/state_mate.rb CHANGED
@@ -9,6 +9,8 @@ require "state_mate/error"
9
9
  require "state_mate/adapters"
10
10
 
11
11
  module StateMate
12
+ @debug = false
13
+ @debug_mode = 'a'
12
14
 
13
15
  DIRECTIVES = Set.new [
14
16
  :set,
@@ -67,6 +69,16 @@ module StateMate
67
69
  key = nil
68
70
  directives = []
69
71
  type_name = nil
72
+ unset_when_false = false
73
+
74
+ # the :unset_when option can be provided to change the directive to
75
+ # :unset when the option's value is true.
76
+ #
77
+ # this is useful for things that should simply unset the key when
78
+ # turned off instead of setting it to false or something.
79
+ #
80
+ unset_when = nil
81
+
70
82
  options = state['options'] || {}
71
83
 
72
84
  unless options.is_a? Hash
@@ -94,6 +106,10 @@ module StateMate
94
106
  directives << [k, v]
95
107
  elsif k == :type
96
108
  type_name = v
109
+ elsif k == :unset_when_false
110
+ unset_when_false = v
111
+ elsif k == :unset_when
112
+ unset_when = StateMate.cast 'bool', v
97
113
  else
98
114
  # any other keys are set as options
99
115
  # this is a little convience feature that avoids having to
@@ -126,7 +142,20 @@ module StateMate
126
142
  raise "multiple directives found in #{ state.inspect }"
127
143
  end
128
144
 
129
- unless type_name.nil?
145
+ # handle :unset_when_false option, which changes the operation to
146
+ # an unset when the *directive value* is explicity false
147
+ if unset_when_false &&
148
+ (value === false || ['False', 'false'].include?(value))
149
+ directive = :unset
150
+ value = nil
151
+
152
+ # handle :unset_when, which also changes the operation to :unset
153
+ # when the option's value is true.
154
+ elsif unset_when
155
+ directive = :unset
156
+ value = nil
157
+
158
+ elsif type_name
130
159
  value = StateMate.cast type_name, value
131
160
  end
132
161
 
@@ -277,6 +306,40 @@ module StateMate
277
306
  end # rollback
278
307
  end # StateSet
279
308
 
309
+ # @api dev
310
+ #
311
+ # turns debug on or off
312
+ #
313
+ # @param mode [Boolean|String]
314
+ # if a string, enables and sets the debug file mode (use 'a' or 'w').
315
+ # if a boolean, sets enabled.
316
+ #
317
+ def self.debug= mode
318
+ if mode.is_a? String
319
+ @debug_mode = mode
320
+ end
321
+ @debug = !!mode
322
+ end
323
+
324
+ def self.debug *messages
325
+ return unless @debug
326
+
327
+ @debug_file ||= File.open('./state_mate.debug.log', @debug_mode)
328
+
329
+ messages.each_with_index do |message, index|
330
+ if index == 0
331
+ @debug_file.write 'DEBUG '
332
+ end
333
+
334
+ if message.is_a? String
335
+ @debug_file.puts message
336
+ else
337
+ @debug_file.puts
338
+ PP.pp(message, @debug_file)
339
+ end
340
+ end
341
+ end
342
+
280
343
  # @api util
281
344
  # *pure*
282
345
  #
data/state_mate.gemspec CHANGED
@@ -31,4 +31,5 @@ END
31
31
  spec.add_dependency 'nrser', '~> 0.0', '>= 0.0.13'
32
32
  spec.add_dependency 'CFPropertyList', '~> 2.3'
33
33
  spec.add_dependency 'cmds', '~> 0.0', '>= 0.0.9'
34
+ spec.add_dependency 'diffable_yaml', '~> 0.0', '>= 0.0.2'
34
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_mate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - nrser
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-22 00:00:00.000000000 Z
11
+ date: 2016-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -148,6 +148,26 @@ dependencies:
148
148
  - - ">="
149
149
  - !ruby/object:Gem::Version
150
150
  version: 0.0.9
151
+ - !ruby/object:Gem::Dependency
152
+ name: diffable_yaml
153
+ requirement: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - "~>"
156
+ - !ruby/object:Gem::Version
157
+ version: '0.0'
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: 0.0.2
161
+ type: :runtime
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: '0.0'
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: 0.0.2
151
171
  description: |
152
172
  helps manage state on OSX by wrapping system commands like `defaults`, `nvram`,
153
173
  `lanuchctl`, `scutil` and more.
@@ -164,7 +184,6 @@ files:
164
184
  - LICENSE.txt
165
185
  - README.md
166
186
  - Rakefile
167
- - ansible/library/state
168
187
  - bin/console
169
188
  - lib/state_mate.rb
170
189
  - lib/state_mate/adapters.rb
@@ -176,6 +195,7 @@ files:
176
195
  - lib/state_mate/adapters/pmset.rb
177
196
  - lib/state_mate/adapters/scutil.rb
178
197
  - lib/state_mate/adapters/time_machine.rb
198
+ - lib/state_mate/adapters/yaml.rb
179
199
  - lib/state_mate/error.rb
180
200
  - lib/state_mate/version.rb
181
201
  - notes/state-set-steps.md
@@ -200,9 +220,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
200
220
  version: '0'
201
221
  requirements: []
202
222
  rubyforge_project:
203
- rubygems_version: 2.2.2
223
+ rubygems_version: 2.6.4
204
224
  signing_key:
205
225
  specification_version: 4
206
226
  summary: i heard it's meant to help you with your state, mate!
207
227
  test_files: []
208
- has_rdoc:
@@ -1,59 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # WANT JSON
3
-
4
- require 'json'
5
- require 'shellwords'
6
- require 'pp'
7
-
8
- require 'bundler/setup'
9
-
10
- require 'nrser'
11
- require 'state_mate'
12
-
13
- using NRSER
14
-
15
- MODULE_COMPLEX_ARGS = "<<INCLUDE_ANSIBLE_MODULE_COMPLEX_ARGS>>"
16
-
17
- def parse input
18
- # require 'shellwords'
19
- parsed = {}
20
- Shellwords.split(input).each do |word|
21
- (key, value) = word.split('=', 2)
22
- parsed[key] = value
23
- end
24
- unless MODULE_COMPLEX_ARGS.empty?
25
- parsed.update JSON.load(MODULE_COMPLEX_ARGS)
26
- end
27
- parsed
28
- end
29
-
30
- def main
31
- input = nil
32
- args = nil
33
- changed = false
34
-
35
- begin
36
- input = File.read ARGV[0]
37
- args = parse input
38
-
39
- spec = args
40
-
41
- changes = StateMate.execute spec
42
-
43
- print JSON.dump({
44
- 'changed' => !changes.empty?,
45
- 'changes' => changes,
46
- })
47
- rescue Exception => e
48
- print JSON.dump({
49
- 'failed' => true,
50
- 'msg' => e.format,
51
- # 'input' => input,
52
- 'args' => args,
53
- # 'ARGV' => ARGV,
54
- # 'ruby' => RUBY_VERSION,
55
- })
56
- end
57
- end
58
-
59
- main if __FILE__ == $0