cfa 0.6.3 → 0.6.4

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
  SHA256:
3
- metadata.gz: 0b935ded0de71ea141ce56290ee7e46c62d3b5c4f3719d392a2f3d2607fb6e50
4
- data.tar.gz: 6d8ec5f5cc7fd9490a1bad3a2f19466873f5a906c15a3351fe6de507fbdfcefe
3
+ metadata.gz: b16f5b90e04efbadc819f5a47ef4855118d4ea2b286cfa790edd64ee333ce129
4
+ data.tar.gz: 8ec15e83b710cbc323077b16309d4f2b58584890dd40e8d7bf21ee4019bcc82d
5
5
  SHA512:
6
- metadata.gz: 04f2051c36312ebbc098eb1e4c26f5f6ad0339d89e34c57a4395286862ef302858306242ef951810c69452298cf0ad1f043e1f8e355518ffb4c29d678bf4e234
7
- data.tar.gz: 1935ed5dff889f4a223363aa4b50892f88abf525a29a93c750a4add401d802aab8cc14fe1d461adab44d0cbc1fe16f1ce5217f2d1fb8df7825713730e464501f
6
+ metadata.gz: f2ef2156c4810935459f0dbed8c15e365b61ec82b80652efb65d056df6262d4151f824ea5887972abe3dd9650b92f96977ca0e9df7437ee783d04c7e8ed81e2d
7
+ data.tar.gz: 375e2b81646579375e7824b2852960223414c146cdf342129d95b3cbec66e5fa6951e3db4726e79fa650b41e9f0800d257b5fe3e5c176e85259a4697d56aa26b
@@ -298,15 +298,20 @@ module CFA
298
298
  # require "cfa/augeas_parser"
299
299
  #
300
300
  # parser = CFA::AugeasParser.new("Sysconfig.lns")
301
+ # parser.file_name = "/etc/default/grub" # for error reporting
301
302
  # data = parser.parse(File.read("/etc/default/grub"))
302
303
  #
303
304
  # puts data["GRUB_DISABLE_OS_PROBER"]
304
305
  # data["GRUB_DISABLE_OS_PROBER"] = "true"
305
306
  # puts parser.serialize(data)
306
307
  class AugeasParser
308
+ # @return [String] optional, used for error reporting
309
+ attr_accessor :file_name
310
+
307
311
  # @param lens [String] a lens name, like "Sysconfig.lns"
308
312
  def initialize(lens)
309
313
  @lens = lens
314
+ @file_name = nil
310
315
  end
311
316
 
312
317
  # @param raw_string [String] a string to be parsed
@@ -324,7 +329,8 @@ module CFA
324
329
  root = load_path = nil
325
330
  Augeas.open(root, load_path, Augeas::NO_MODL_AUTOLOAD) do |aug|
326
331
  aug.set("/input", raw_string)
327
- report_error(aug) unless aug.text_store(@lens, "/input", "/store")
332
+ report_error(aug, "parsing", file_name) \
333
+ unless aug.text_store(@lens, "/input", "/store")
328
334
 
329
335
  return AugeasReader.read(aug, "/store")
330
336
  end
@@ -343,7 +349,7 @@ module CFA
343
349
  AugeasWriter.new(aug).write("/store", data)
344
350
 
345
351
  res = aug.text_retrieve(@lens, "/input", "/store", "/output")
346
- report_error(aug) unless res
352
+ report_error(aug, "serializing", file_name) unless res
347
353
 
348
354
  return aug.get("/output")
349
355
  end
@@ -358,16 +364,30 @@ module CFA
358
364
  private
359
365
 
360
366
  # @param aug [::Augeas]
361
- def report_error(aug)
367
+ # @param activity ["parsing", "serializing"] for better error messages
368
+ # @param file_name [String,nil] a file name
369
+ def report_error(aug, activity, file_name)
362
370
  error = aug.error
363
371
  # zero is no error, so problem in lense
364
- if aug.error[:code].nonzero?
365
- raise "Augeas error #{error[:message]}. Details: #{error[:details]}."
372
+ if error[:code].nonzero?
373
+ raise "Augeas error: #{error[:message]}. Details: #{error[:details]}."
366
374
  end
367
375
 
368
- msg = aug.get("/augeas/text/store/error/message")
369
- location = aug.get("/augeas/text/store/error/lens")
370
- raise "Augeas parsing/serializing error: #{msg} at #{location}"
376
+ file_name ||= "(unknown file)"
377
+ raise format("Augeas #{activity} error: %<message>s" \
378
+ " at #{file_name}:%<line>s:%<char>s, lens %<lens>s",
379
+ aug_get_error(aug))
380
+ end
381
+
382
+ def aug_get_error(aug)
383
+ {
384
+ message: aug.get("/augeas/text/store/error/message"),
385
+ line: aug.get("/augeas/text/store/error/line"),
386
+ char: aug.get("/augeas/text/store/error/char"), # column
387
+ # file, line+column range, like
388
+ # "/usr/share/augeas/lenses/dist/hosts.aug:23.12-.42:"
389
+ lens: aug.get("/augeas/text/store/error/lens")
390
+ }
371
391
  end
372
392
  end
373
393
  end
@@ -39,6 +39,7 @@ module CFA
39
39
  # insertion of such values in the first place.
40
40
  def save(changes_only: false)
41
41
  merge_changes if changes_only
42
+ @parser.file_name = @file_path if @parser.respond_to?(:file_name=)
42
43
  @file_handler.write(@file_path, @parser.serialize(data))
43
44
  end
44
45
 
@@ -51,6 +52,7 @@ module CFA
51
52
  # @raise a *parser* specific error. If the parsed String is malformed, then
52
53
  # depending on the used parser it may raise an error.
53
54
  def load
55
+ @parser.file_name = @file_path if @parser.respond_to?(:file_name=)
54
56
  self.data = @parser.parse(@file_handler.read(@file_path))
55
57
  @loaded = true
56
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josef Reidinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-09 00:00:00.000000000 Z
11
+ date: 2018-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-augeas