yamled 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b12e52914e467ff844a439d5f5b8c4e0548b1e3
4
+ data.tar.gz: da67aa74da30518e7cd91044def6e9219c617038
5
+ SHA512:
6
+ metadata.gz: 8572666d707130e363c9f70d6cdf30e906b3133927c827313fe9793a2691cdc26a266e6cc798c1360f7a92af8b145de7db72d470ee47ed3847b7d634261ff3df
7
+ data.tar.gz: 313b166b2f4f29a4dcf4ad432afc44315cdf6204b7a155cbf34db8c59874f16634e198ef47634314a640f1b65402bb4137afffbf37b0638c1cf9f0d96597b119
data/LICENSE ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # yamled
2
+
3
+ TBD
data/bin/yamled ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative "../config/environment"
3
+
4
+ Yamled::CLI.run(
5
+ argv: ARGV,
6
+ stdin: $stdin,
7
+ stdout: $stdout,
8
+ stderr: $stderr,
9
+ )
@@ -0,0 +1,6 @@
1
+
2
+ PROJ_ROOT = File.expand_path(File.dirname(__FILE__) + "/..")
3
+
4
+ $LOAD_PATH << "#{PROJ_ROOT}/lib"
5
+
6
+ require 'yamled'
data/lib/yamled.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Yamled
2
+ end
3
+
4
+ require 'yamled/cli'
5
+ require 'yamled/actions'
6
+ require 'yamled/io'
7
+ require 'yamled/errors'
8
+
@@ -0,0 +1,65 @@
1
+ module Yamled
2
+ module ReprintAction
3
+ class << self
4
+ def run(options, input, output, logger)
5
+ data = input.get_data
6
+ # puts "ReprintAction: data=#{data.inspect}"
7
+ output.print_data data
8
+ end
9
+ end
10
+ end
11
+
12
+ module ReadAction
13
+ class << self
14
+ def run(options, input, output, logger)
15
+ path = options[:path]
16
+ data = input.get_data
17
+ # puts "ReadAction: data=#{data.inspect}"
18
+ node = data
19
+ if !path.nil?
20
+ steps = path.split("/")
21
+ while steps.count > 0
22
+ step = steps.shift
23
+ node = node[step]
24
+ end
25
+ end
26
+ output.print_value node
27
+ end
28
+ end
29
+ end
30
+
31
+ module WriteAction
32
+ class << self
33
+ def run(options, input, output, logger)
34
+ path = options[:path]
35
+ value = options[:value]
36
+ data = input.get_data
37
+ parent = nil
38
+ step = nil
39
+ node = data
40
+ if !path.nil?
41
+ steps = path.split("/")
42
+ while steps.count > 0
43
+ step = steps.shift
44
+ parent = node
45
+ node = node[step]
46
+ end
47
+ end
48
+
49
+ if !parent.nil?
50
+ parent[step] = value
51
+ else
52
+ data = value
53
+ end
54
+
55
+ output.print_value data
56
+ end
57
+ end
58
+ end
59
+
60
+ Actions = {
61
+ reprint: ReprintAction,
62
+ get: ReadAction,
63
+ set: WriteAction
64
+ }
65
+ end
data/lib/yamled/cli.rb ADDED
@@ -0,0 +1,108 @@
1
+ module Yamled
2
+ module CLI
3
+ class << self
4
+ def run(argv:,stdin:,stdout:,stderr:)
5
+
6
+ options, input, output, logger = setup_io(
7
+ Options.load(argv),
8
+ stdin,
9
+ stdout,
10
+ stderr)
11
+
12
+ action_id = options.delete(:action)
13
+ action = Yamled::Actions[action_id]
14
+ action.run(options, input, output, logger)
15
+
16
+ input.close
17
+ output.close
18
+ logger.close
19
+ end
20
+
21
+ private
22
+
23
+ def setup_io(options, stdin, stdout, stderr)
24
+ options = options.dup
25
+
26
+ infile = options.delete(:infile)
27
+ instream = if infile == :stdin
28
+ stdin
29
+ else
30
+ File.open(infile, "r")
31
+ end
32
+
33
+ outfile = options.delete(:outfile)
34
+ outstream = if outfile == :stdout
35
+ stdout
36
+ else
37
+ File.open(outfile, "wb")
38
+ end
39
+
40
+ logfile = options.delete(:logfile)
41
+ logstream = if logfile == :stderr
42
+ stderr
43
+ elsif logfile == :stdout
44
+ stdout
45
+ else
46
+ File.open(logfile, "wb")
47
+ end
48
+
49
+ input = Yamled::IO.input_from(instream)
50
+ output = Yamled::IO.output_to(outstream)
51
+ logger = Yamled::IO.log_to(stderr)
52
+
53
+ return [options, input, output, logger]
54
+ end
55
+ end
56
+
57
+ module Options
58
+ Defaults = {
59
+ infile: :stdin,
60
+ outfile: :stdout,
61
+ logfile: :stderr,
62
+ action: :reprint,
63
+ }
64
+
65
+ class << self
66
+ def load(argv)
67
+ argv_dup = argv.dup
68
+
69
+ options = Defaults
70
+
71
+ require 'optparse'
72
+ parser = OptionParser.new do |opts|
73
+ opts.banner = "Usage: yamled [options]"
74
+
75
+ opts.on("-g", "--get [PATH]", String, "Read a value at a path.") do |path|
76
+ options[:action] = :get
77
+ options[:path] = path
78
+ end
79
+ opts.on("-s", "--set [PATH]", String, "Write a value at a path. Requires --value option.") do |path|
80
+ options[:action] = :set
81
+ options[:path] = path
82
+ end
83
+ opts.on("-r", "--set-root", String, "Write a value at the root. Requires --value option.") do |path|
84
+ options[:action] = :set
85
+ options[:path] = ""
86
+ end
87
+ opts.on("-v", "--value [VALUE]", String, "Value to set at the location of --path.") do |strval|
88
+ options[:value] = YAML.load(strval)
89
+ end
90
+ end
91
+
92
+ begin
93
+ parser.parse!(argv_dup)
94
+ return validate(options)
95
+ rescue => e
96
+ puts e.message
97
+ puts parser.help
98
+ exit Yamled::Errors::BadOptions
99
+ end
100
+ end
101
+
102
+ def validate(options)
103
+ options
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,6 @@
1
+ module Yamled
2
+ module Errors
3
+ BadInput = 1
4
+ BadOptions = 2
5
+ end
6
+ end
data/lib/yamled/io.rb ADDED
@@ -0,0 +1,78 @@
1
+ module Yamled
2
+ module IO
3
+ EmptyData = nil
4
+
5
+ class Base
6
+ attr_reader :io
7
+ def initialize(io)
8
+ @io = io
9
+ end
10
+
11
+ def close
12
+ @io.close
13
+ end
14
+ end
15
+
16
+ class Input < Base
17
+ require 'yaml'
18
+ def get_data
19
+ input = io.read
20
+ if input.strip == ''
21
+ return EmptyData
22
+ else
23
+ YAML.load(input)
24
+ end
25
+ end
26
+ end
27
+
28
+ class Output < Base
29
+ require 'yaml'
30
+ def print_data(data)
31
+ if data == IO::EmptyData
32
+ print_empty
33
+ else
34
+ io.print YAML.dump(data)
35
+ end
36
+ end
37
+
38
+ def print_value(v)
39
+ str = case v
40
+ when Hash, Array
41
+ YAML.dump(v)
42
+ when nil
43
+ ''
44
+ else
45
+ v.inspect
46
+ end
47
+ io.print str
48
+ end
49
+
50
+ def print_empty
51
+ io.print ''
52
+ end
53
+ end
54
+
55
+ class Logger < Base
56
+ def initialize(io)
57
+ @io = io
58
+ end
59
+ def log(str)
60
+ io.puts ">> #{str}"
61
+ end
62
+ end
63
+
64
+ class << self
65
+ def input_from(in_io)
66
+ Input.new(in_io)
67
+ end
68
+
69
+ def output_to(out_io)
70
+ Output.new(out_io)
71
+ end
72
+
73
+ def log_to(out_io)
74
+ Logger.new(out_io)
75
+ end
76
+ end
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yamled
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Crosby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Read and write YAML docs and values from the command line. Enables YAML
14
+ data use and generation in shell scripts.
15
+ email: dcrosby42@gmail.com
16
+ executables:
17
+ - yamled
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - bin/yamled
24
+ - config/environment.rb
25
+ - lib/yamled.rb
26
+ - lib/yamled/actions.rb
27
+ - lib/yamled/cli.rb
28
+ - lib/yamled/errors.rb
29
+ - lib/yamled/io.rb
30
+ homepage: http://github.com/dcrosby42/yamled
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.4.5.1
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: YAML CLI editor
54
+ test_files: []