yamlsh 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 430146c89706b6f3af849d42eb882f0e8503b0be
4
+ data.tar.gz: 6d58467576bed5951376106b4b07715eade68ec7
5
+ SHA512:
6
+ metadata.gz: 4c6c148646409f954b14e1ec0853a45a1f398d04e3c8855bc43e8e141ba5a49478f05243918e97aa35399d81137aa7f5353ee1ca2a1a3c6caf67950ac9a9728c
7
+ data.tar.gz: da4550c096f1b6d59053d17e2f4e55aff7fe62773093728dc313bbec1e013d3a50553c75ea21596f5451afbd319e7fac5e03913673c82177574a60e16d15ffd5
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yamlsh.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Franck Verrot
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a
4
+ copy of this software and associated documentation files (the "Software"),
5
+ to deal in the Software without restriction, including without limitation
6
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ and/or sell copies of the Software, and to permit persons to whom the
8
+ Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
+ DEALINGS IN THE SOFTWARE.
20
+
@@ -0,0 +1,60 @@
1
+ # yamlsh
2
+
3
+ `yamlsh` is a YAML editing tool.
4
+
5
+ Editing YAML, especially on large files, is a total nightmare. Most tooling
6
+ rely on IDE/text editors folding-like features, but it's still up to the
7
+ developers and translators to search and scroll up and down.
8
+
9
+ Also when it comes to translations, the common guideline is to have one file
10
+ per language. Synchronizing language files manually leads to many errors.
11
+
12
+ `yamlsh` brings:
13
+
14
+ 1. An easy command-line navigation a-la-shell (`cd`, `ls`, etc.)
15
+ 2. An extensible command infrastructure
16
+
17
+
18
+ # Pre-requisite
19
+
20
+ * Any Ruby & 2.0 installation
21
+
22
+
23
+ # Installation
24
+
25
+ sudo gem install yamlsh
26
+
27
+
28
+ # Usage
29
+
30
+ λ yamlsh foo.yml
31
+ # yamlsh - Franck Verrot <franck@verrot.fr> - MIT License - 2015
32
+ $(root)> ls
33
+ => en
34
+ $(root)> cd en
35
+ $(en)> ls
36
+ => key1
37
+ key2
38
+ $(en)> get key1
39
+ => Hello
40
+ $(en)> get key2
41
+ => {"foo"=>"bar", "baz"=>"bat"}
42
+ $(en)> set key1
43
+ Hit ENTER to stop typing
44
+ > Hello World!
45
+ => Hello World!
46
+ $(en)> get key1
47
+ => Hello World!
48
+ $(en)> save
49
+ => ok
50
+ $(en)> quit
51
+
52
+
53
+ Quitting...
54
+
55
+ λ
56
+
57
+
58
+ # License
59
+
60
+ MIT License, Franck Verrot, Copyright 2015
@@ -0,0 +1,64 @@
1
+ require 'rubygems'
2
+ require 'rubygems/specification'
3
+
4
+ require 'bundler'
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ require 'rake/testtask'
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.pattern = "test/**/*_test.rb"
11
+ t.verbose = true
12
+ t.warning = true
13
+ end
14
+
15
+ # -*- encoding: utf-8 -*-
16
+ $:.unshift File.expand_path("../lib", __FILE__)
17
+ require 'yamlsh'
18
+
19
+ def gemspec
20
+ @gemspec ||= begin
21
+ file = File.expand_path('../yamlsh.gemspec', __FILE__)
22
+ eval(File.read(file), binding, file)
23
+ end
24
+ end
25
+
26
+ desc "Clean the current directory"
27
+ task :clean do
28
+ rm_rf 'tmp'
29
+ rm_rf 'pkg'
30
+ end
31
+
32
+ desc "Run the full spec suite"
33
+ task :full => ["clean", "test"]
34
+
35
+ desc "install the gem locally"
36
+ task :install => :package do
37
+ sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}}
38
+ end
39
+
40
+ desc "validate the gemspec"
41
+ task :gemspec do
42
+ gemspec.validate
43
+ end
44
+
45
+ desc "Build the gem"
46
+ task :gem => [:gemspec, :build] do
47
+ mkdir_p "pkg"
48
+ sh "gem build yamlsh.gemspec"
49
+ mv "#{gemspec.full_name}.gem", "pkg"
50
+
51
+ require 'digest/sha2'
52
+ built_gem_path = "pkg/#{gemspec.full_name}.gem"
53
+ checksum = Digest::SHA512.new.hexdigest(File.read(built_gem_path))
54
+ checksum_path = "checksums/#{gemspec.version}.sha512"
55
+ File.open(checksum_path, 'w' ) {|f| f.write(checksum) }
56
+ end
57
+
58
+ desc "Install yamlsh"
59
+ task :install => :gem do
60
+ sh "gem install pkg/#{gemspec.full_name}.gem"
61
+ end
62
+
63
+ task :default => :full
64
+
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby -w
2
+ # set ft=ruby
3
+ $:<< 'lib'
4
+ require 'yamlsh'
5
+
6
+ def show_program_name(out = $stdout)
7
+ out.puts "# yamlsh - Franck Verrot <franck@verrot.fr> - MIT License - 2015"
8
+ end
9
+
10
+ def show_banner(out = $stdout)
11
+ show_program_name(out)
12
+ out.puts <<-EOS
13
+ usage: yamlsh <yaml file>
14
+ EOS
15
+ end
16
+
17
+ if ARGV.length != 1
18
+ show_banner
19
+ puts "Missing yaml file argument"
20
+ exit 1
21
+ else
22
+ home = ENV.fetch('HOME','~/')
23
+ history_file = ENV.fetch('HISTORY_FILE', File.join(home, '.yamlsh_history'))
24
+ Yamlsh::CLI.new(ARGV.shift, history_file).run
25
+ exit 0
26
+ end
@@ -0,0 +1,16 @@
1
+ module Yamlsh
2
+ end
3
+
4
+ require 'yamlsh/path'
5
+ require 'yamlsh/commands'
6
+ require 'yamlsh/cli/output'
7
+ require 'yamlsh/cli/no_output'
8
+ require 'yamlsh/cli'
9
+
10
+ require 'yamlsh/commands/cd'
11
+ require 'yamlsh/commands/get'
12
+ require 'yamlsh/commands/ls'
13
+ require 'yamlsh/commands/save'
14
+ require 'yamlsh/commands/set'
15
+ require 'yamlsh/commands/pwd'
16
+ require 'yamlsh/commands/quit'
@@ -0,0 +1,73 @@
1
+ require 'readline'
2
+ require 'yamlsh/env'
3
+
4
+ module Yamlsh
5
+ class CLI
6
+ def initialize(file, history_file)
7
+ @env = Env.new(file)
8
+ @history_file = history_file
9
+ end
10
+
11
+ def show_prompt
12
+ Readline.readline("$(#{@env.current_path})> ", true)
13
+ end
14
+
15
+ def load_history
16
+ IO.foreach(@history_file) do |line|
17
+ Readline::HISTORY.push line.chomp if line.size > 0
18
+ end
19
+ rescue Errno::ENOENT
20
+ if $-w
21
+ puts "No history file located at: #{@history_file}"
22
+ end
23
+ end
24
+
25
+ def add_to_history line
26
+ File.open(@history_file, 'a+') { |f| f.puts line }
27
+ end
28
+
29
+ def run
30
+ show_program_name
31
+
32
+ Readline.completion_append_character = ""
33
+ Readline.completion_proc = Proc.new do |str|
34
+ @env.keys.grep(/^#{Regexp.escape(str)}/)
35
+ end
36
+
37
+ trap('INT') do
38
+ Readline.delete_text
39
+ Readline.point = 0
40
+ Readline.refresh_line
41
+ end
42
+
43
+ load_history
44
+
45
+ begin
46
+ loop do
47
+ command_line = show_prompt
48
+ exit 0 if command_line.nil?
49
+
50
+ command, *args = command_line.split
51
+
52
+ unless command
53
+ Readline::HISTORY.pop# unless command && command.size == 0
54
+ end
55
+
56
+ begin
57
+ if Readline::HISTORY[Readline::HISTORY.length-2] == command
58
+ Readline::HISTORY.pop
59
+ end
60
+ rescue IndexError
61
+ end
62
+ status, output, @env = Commands[command.downcase][@env, command, args]
63
+ output.show
64
+ add_to_history command_line if status == Command::Success
65
+ end
66
+ rescue RuntimeError => e
67
+ puts "\n\nQuitting #{e.class} #{e.message}...\n"
68
+ puts e.backtrace
69
+ exit 1
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ class NoOutput < Struct.new(:content)
2
+ def show; end
3
+ end
@@ -0,0 +1,5 @@
1
+ class Output < Struct.new(:content)
2
+ def show
3
+ $stdout.puts "=> #{content}"
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require 'yamlsh/cli/output'
2
+ require 'yamlsh/cli/no_output'
3
+
4
+ Commands = Hash.new do
5
+ lambda do |env, command, args|
6
+ [Command::Error, Output.new("unknown command #{command}"), env]
7
+ end
8
+ end
9
+
10
+ module Command
11
+ Success = Class.new
12
+ Error = Class.new
13
+ end
@@ -0,0 +1,8 @@
1
+ Commands['cd'] = lambda do |env, command, args|
2
+ begin
3
+ env.cd(args.first)
4
+ [Command::Success, NoOutput.new, env]
5
+ rescue Yamlsh::Env::CdError => e
6
+ [Command::Error, Output.new("cd: #{e.message}"), env]
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ Commands['get'] = lambda do |env, command, args|
2
+ value = env.get(args.first)
3
+ [Command::Success, Output.new(value), env]
4
+ end
@@ -0,0 +1,6 @@
1
+ Commands['ls'] = lambda do |env, command, args|
2
+ output = env.keys.map do |key|
3
+ key
4
+ end.join("\n")
5
+ [Command::Success, Output.new(output), env]
6
+ end
@@ -0,0 +1,8 @@
1
+ Commands['pwd'] = lambda do |env, command, args|
2
+ begin
3
+ pwd = env.pwd
4
+ [Command::Success, Output.new(pwd), env]
5
+ rescue Yamlsh::Env::PwdError => e
6
+ [Command::Error, Output.new("pwd: #{e.message}"), env]
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ Commands['quit'] = lambda do |env, command, args|
2
+ exit 0
3
+ end
@@ -0,0 +1,8 @@
1
+ Commands['save'] = lambda do |env, command, args|
2
+ begin
3
+ value = env.save
4
+ [Command::Success, Output.new(value), env]
5
+ rescue Yaml::Env::SaveError => e
6
+ [Command::Error, Output.new("save: #{e.message}"), env]
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ Commands['set'] = lambda do |env, command, args|
2
+ begin
3
+ target, _ = args
4
+ Readline.pre_input_hook = proc do
5
+ old_value = begin
6
+ env.get(target).to_s || ''
7
+ rescue
8
+ ''
9
+ end
10
+ Readline.insert_text(old_value)
11
+ Readline.redisplay
12
+ end
13
+ new_value = Readline.readline("Hit ENTER to stop typing\n> ", true)
14
+ env.set(target, new_value)
15
+ Readline.pre_input_hook = nil
16
+ [Command::Success, Output.new(new_value), env]
17
+ rescue Yamlsh::Env::SetError => e
18
+ [Command::Error, Output.new("set: #{e.message}"), env]
19
+ end
20
+ end
@@ -0,0 +1,74 @@
1
+ require 'yaml'
2
+
3
+ module Yamlsh
4
+ class Env
5
+ CdError = Class.new(RuntimeError)
6
+ PwdError = Class.new(RuntimeError)
7
+ SetError = Class.new(RuntimeError)
8
+ SaveError = Class.new(RuntimeError)
9
+
10
+ def initialize(data_store, path = Path.new)
11
+ @data_store = data_store
12
+ @yaml = YAML.load_file(data_store)
13
+ @path = path
14
+ @current_yaml_at_path = @yaml.dup
15
+ end
16
+
17
+ def keys
18
+ @current_yaml_at_path.keys.sort
19
+ end
20
+
21
+ def get(key)
22
+ @current_yaml_at_path[key]
23
+ end
24
+
25
+ def set(key, value)
26
+ @current_yaml_at_path[key] = value
27
+ end
28
+
29
+ def pwd
30
+ @path.to_s
31
+ end
32
+
33
+ def cd key_path # . separated path
34
+ key_path ||= ''
35
+
36
+ if key_path == 'root'
37
+ @path.reset
38
+ @current_yaml_at_path = @yaml
39
+ elsif key_path == '.'
40
+ # nop
41
+ elsif key_path == '..'
42
+ @path.pop
43
+ new_path = @path.to_s
44
+ cd 'root'
45
+ cd new_path
46
+ else
47
+ segments = key_path.split('.')
48
+ segments.each do |segment|
49
+ yaml = @current_yaml_at_path[segment]
50
+
51
+ if yaml.respond_to? :keys
52
+ @current_yaml_at_path = yaml
53
+ @path.push segment
54
+ else
55
+ raise CdError, "#{segment} is a key, use SET to modify it"
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def save
62
+ begin
63
+ File.open(@data_store, 'w') { |f| f.puts @yaml.to_yaml }
64
+ "ok"
65
+ rescue => e
66
+ "ko -- #{e.message}"
67
+ end
68
+ end
69
+
70
+ def current_path
71
+ @path.to_s
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,31 @@
1
+ module Yamlsh
2
+ class Path
3
+ def initialize
4
+ @segments = Array.new
5
+ end
6
+
7
+ def to_s
8
+ if @segments.any?
9
+ segments
10
+ else
11
+ ['root']
12
+ end.join('.')
13
+ end
14
+
15
+ def push path
16
+ @segments.push path
17
+ end
18
+
19
+ def reset
20
+ @segments.clear
21
+ end
22
+
23
+ def pop
24
+ @segments.pop
25
+ end
26
+
27
+ def segments
28
+ @segments
29
+ end
30
+ end
31
+ end
@@ -0,0 +1 @@
1
+ require 'minitest/autorun'
@@ -0,0 +1,29 @@
1
+ require 'helper'
2
+ require 'yamlsh/path'
3
+
4
+ class PathTest < Minitest::Test
5
+ def setup
6
+ @path = Yamlsh::Path.new
7
+ end
8
+
9
+ def test_initialize_empty
10
+ assert_equal 0, @path.segments.size
11
+ end
12
+
13
+ def test_push_pop_segmnets
14
+ @path.push 'foo'
15
+ assert_equal 1, @path.segments.size
16
+ assert_equal ['foo'], @path.segments
17
+
18
+ result = @path.pop
19
+ assert_equal 0, @path.segments.size
20
+ assert_equal 'foo', result
21
+ end
22
+
23
+ def test_reset
24
+ @path.push 'foo'
25
+ @path.reset
26
+
27
+ assert_equal 0, @path.segments.size
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "yamlsh"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Franck Verrot"]
9
+ spec.email = ["franck@verrot.fr"]
10
+ spec.summary = %q{yamlsh - a YAML editor}
11
+ spec.description = spec.summary
12
+ spec.homepage = "https://github.com/franckverrot/yamlsh"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yamlsh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Franck Verrot
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: yamlsh - a YAML editor
42
+ email:
43
+ - franck@verrot.fr
44
+ executables:
45
+ - yamlsh
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.md
52
+ - README.md
53
+ - Rakefile
54
+ - bin/yamlsh
55
+ - lib/yamlsh.rb
56
+ - lib/yamlsh/cli.rb
57
+ - lib/yamlsh/cli/no_output.rb
58
+ - lib/yamlsh/cli/output.rb
59
+ - lib/yamlsh/commands.rb
60
+ - lib/yamlsh/commands/cd.rb
61
+ - lib/yamlsh/commands/get.rb
62
+ - lib/yamlsh/commands/ls.rb
63
+ - lib/yamlsh/commands/pwd.rb
64
+ - lib/yamlsh/commands/quit.rb
65
+ - lib/yamlsh/commands/save.rb
66
+ - lib/yamlsh/commands/set.rb
67
+ - lib/yamlsh/env.rb
68
+ - lib/yamlsh/path.rb
69
+ - test/helper.rb
70
+ - test/path_test.rb
71
+ - yamlsh.gemspec
72
+ homepage: https://github.com/franckverrot/yamlsh
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.4
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: yamlsh - a YAML editor
96
+ test_files:
97
+ - test/helper.rb
98
+ - test/path_test.rb
99
+ has_rdoc: