shashi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ require "io/console"
2
+
3
+ module Shashi
4
+ module Command
5
+ module SetSet
6
+
7
+ def self.perform(file:, path:, key:, value:, force:, echo:)
8
+ data = Utils::Database.read_database(file: file)
9
+ data_reference = Utils::Data.walk(data: data, path: path)
10
+ if data_reference.has_key?(key)
11
+ if force
12
+ update_data(file: file, data: data, data_reference: data_reference, key: key, value: value, echo: echo)
13
+ else
14
+ print "The key #{key} already exists at #{path.join(".")}, overwrite? [y/N] "
15
+ choice = gets
16
+ case choice.strip
17
+ when "y"
18
+ update_data(file: file, data: data, data_reference: data_reference, key: key, value: value, echo: echo)
19
+ when ""
20
+ # do nothing
21
+ when "N"
22
+ # do nothing
23
+ else
24
+ puts "Choice not understood, ignoring."
25
+ end
26
+ end
27
+ else
28
+ update_data(file: file, data: data, data_reference: data_reference, key: key, value: value, echo: echo)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def self.update_data(file:, data:, data_reference:, key:, value:, echo:)
35
+ data_reference[key] = if value
36
+ value
37
+ else
38
+ print "[#{key}] = "
39
+ if echo
40
+ gets.strip
41
+ else
42
+ STDIN.noecho(&:gets).strip
43
+ end
44
+ end
45
+ Utils::Database.write_database(file: file, data: data)
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,30 @@
1
+ require "fileutils"
2
+
3
+ module Shashi
4
+ module Command
5
+ module Setup
6
+
7
+ def self.perform(file:)
8
+ prepare_directory(file: file)
9
+ Utils::Database.write_database(file: file, data: {})
10
+ end
11
+
12
+ private
13
+
14
+ def self.prepare_directory(file:)
15
+ dirname = File.dirname(file)
16
+ if File.exists?(dirname)
17
+ if File.directory?(dirname)
18
+ # ok
19
+ else
20
+ puts "You specified #{dirname} as a directory but a file with that name already exists."
21
+ exit 1
22
+ end
23
+ else
24
+ FileUtils.mkdir_p(dirname)
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ require "pp"
2
+
3
+ module Shashi
4
+ module Command
5
+ module ShowKeys
6
+
7
+ def self.perform(file:, path:, keys:, deep:)
8
+ data = Utils::Database.read_database(file: file)
9
+ data_reference = Utils::Data.walk(data: data, path: path)
10
+ keys.each do |key|
11
+ if data_reference.has_key?(key)
12
+ value = data_reference[key]
13
+ print "#{key}: "
14
+ if Utils::Data.is_list?(value)
15
+ if deep
16
+ pp value
17
+ else
18
+ puts ""
19
+ puts "#List#"
20
+ end
21
+ elsif Utils::Data.is_set?(value)
22
+ if deep
23
+ puts ""
24
+ pp value
25
+ else
26
+ puts "#Set#"
27
+ end
28
+ else
29
+ puts value
30
+ end
31
+ else
32
+ puts "NOT FOUND: #{key}"
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,48 @@
1
+ require "json"
2
+
3
+ module Shashi
4
+ module Utils
5
+
6
+ def self.blank?(value)
7
+ value == "" || value == nil
8
+ end
9
+
10
+ module Database
11
+
12
+ def self.read_database(file:)
13
+ JSON.parse(File.read(file))
14
+ end
15
+
16
+ def self.write_database(file:, data:)
17
+ File.write(file, JSON.pretty_generate(data))
18
+ end
19
+
20
+ end
21
+
22
+ module Data
23
+
24
+ def self.walk(data:, path:)
25
+ current = data
26
+ path.each do |key|
27
+ if current.has_key?(key)
28
+ current = current[key]
29
+ else
30
+ puts "Path #{path.join(".")} is not valid."
31
+ exit 1
32
+ end
33
+ end
34
+ current
35
+ end
36
+
37
+ def self.is_list?(value)
38
+ value.is_a?(Array)
39
+ end
40
+
41
+ def self.is_set?(value)
42
+ value.is_a?(Hash)
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Shashi
2
+ VERSION = "0.1.0"
3
+ end
data/shashi.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shashi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "shashi"
8
+ spec.version = Shashi::VERSION
9
+ spec.authors = ["Maurizio De Magnis"]
10
+ spec.email = ["root@olisti.co"]
11
+
12
+ spec.summary = %q{Store and fetch data from a JSON file using the command line.}
13
+ spec.homepage = "https://github.com/olistik/shashi"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.12"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shashi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maurizio De Magnis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-08 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - root@olisti.co
58
+ executables:
59
+ - shashi
60
+ - shashi__db.json.sample
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - exe/shashi
75
+ - exe/shashi__db.json.sample
76
+ - gpl-3.0.txt
77
+ - lib/shashi.rb
78
+ - lib/shashi/cli.rb
79
+ - lib/shashi/command.rb
80
+ - lib/shashi/command/create_list.rb
81
+ - lib/shashi/command/create_set.rb
82
+ - lib/shashi/command/list_delete.rb
83
+ - lib/shashi/command/list_push.rb
84
+ - lib/shashi/command/list_show.rb
85
+ - lib/shashi/command/set_delete.rb
86
+ - lib/shashi/command/set_set.rb
87
+ - lib/shashi/command/setup.rb
88
+ - lib/shashi/command/show_keys.rb
89
+ - lib/shashi/utils.rb
90
+ - lib/shashi/version.rb
91
+ - shashi.gemspec
92
+ homepage: https://github.com/olistik/shashi
93
+ licenses: []
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Store and fetch data from a JSON file using the command line.
115
+ test_files: []