datalackeytools 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/files2object ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright © 2019-2021 Ismo Kärkkäinen
5
+ # Licensed under Universal Permissive License. See LICENSE.txt.
6
+
7
+ require 'pathname'
8
+ require_relative '../lib/common'
9
+
10
+
11
+ def usage
12
+ aargh %q(Usage: files2object [--wait|-w] [key filename ...]
13
+ Prints out key: file contents pairs as a JSON object.
14
+ File contents are not checked to be valid JSON.
15
+ If filename starts with :, the rest of string is taken to be a JSON value.
16
+ --wait/-w option as first argument will wait for a line/EOF from stdin first.
17
+ (Intended for debugging the program that gets the output from this.)
18
+ ), 1
19
+ end
20
+
21
+ wait = (ARGV.first == '--wait' or ARGV.first == '-w')
22
+ ARGV.shift if wait
23
+ usage if !ARGV.empty? && ARGV.size.odd?
24
+
25
+ ARGV.each_slice(2) do |pair|
26
+ next if pair.last.start_with? ':'
27
+ fp = Pathname.new pair.last
28
+ aargh "Not a file: #{pair.last}", 1 unless fp.file?
29
+ end
30
+
31
+ if wait
32
+ begin
33
+ $stdin.readline
34
+ rescue StandardError
35
+ end
36
+ end
37
+
38
+ separator = ''
39
+ print '{'
40
+ ARGV.each_slice(2) do |pair|
41
+ print %(#{separator}"#{pair.first}":)
42
+ if pair.last.start_with? ':'
43
+ print pair.last[1..pair.last.size]
44
+ else
45
+ fp = Pathname.new pair.last
46
+ offset = 0
47
+ while offset < fp.size
48
+ print fp.read(1048576, offset)
49
+ $stdout.flush
50
+ offset += 1048576
51
+ end
52
+ end
53
+ separator = ','
54
+ end
55
+ puts '}'
56
+ $stdout.flush
data/bin/object2files ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright © 2019-2021 Ismo Kärkkäinen
5
+ # Licensed under Universal Permissive License. See LICENSE.txt.
6
+
7
+ require 'json'
8
+ require 'pathname'
9
+ require_relative '../lib/common'
10
+
11
+
12
+ if !ARGV.empty? && ARGV.size.odd?
13
+ aargh %q(Usage: object2files [key filename ...]
14
+ Saves each key:value pair value in input JSON object to file.
15
+ File name is either taken from arguments, or key is used as is.
16
+ Directories are created if filename seems to contain them.
17
+ Unused key/filename argument pairs are ignored.), 1
18
+ end
19
+
20
+ map = ARGV.empty? ? {} : Hash[ARGV.each_slice(2).to_a]
21
+
22
+ begin
23
+ obj = JSON.parse($stdin.read)
24
+ rescue JSON::ParserError
25
+ aargh 'Error parsing input.', 2
26
+ end
27
+
28
+ obj.each_pair do |key, value|
29
+ file = map.fetch(key, key)
30
+ fp = Pathname.new file
31
+ dn = fp.dirname
32
+ begin
33
+ dn.mkpath
34
+ rescue Errno::EEXIST
35
+ aargh "Failed to create key #{key} path in #{file}", 3
36
+ end
37
+ begin
38
+ fp.open('w') do |f|
39
+ f.write value.to_json
40
+ end
41
+ rescue Errno::EACCES, Errno::ENOTDIR, Errno::ENOENT
42
+ aargh "Failed to write key #{key} to #{file}", 3
43
+ end
44
+ end
data/lib/common.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright © 2019-2021 Ismo Kärkkäinen
4
+ # Licensed under Universal Permissive License. See LICENSE.txt.
5
+
6
+ def aargh(message, exit_code = nil)
7
+ $stderr.puts message
8
+ exit(exit_code) unless exit_code.nil?
9
+ end