squill 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fe04730ead5ea933e96f55f1a46ec2bd12e2a8b8
4
+ data.tar.gz: 64328ab776f037b01a7cf189c0214c51dac17596
5
+ SHA512:
6
+ metadata.gz: 692a546b15340488bccaf17e06c2e5c0b6a1320319a243a70fe971f63b866425e7761eebc4809d035eb0150ee0f376bd3b6e34685995edf229fda69651c06489
7
+ data.tar.gz: 10e9966c6edb9f3b9b5eb9a0ef4e0773591d4d6a93534f9f4a85478181e2029f4455936c5a97b53fa109c3bbb8e9a568e3d4f661bd33f49bca7a08c9490f6573
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in squill.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Andy Beering
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Squill
2
+
3
+ A very lightweight tool for storing, searching and executing ad-hoc SQL.
4
+
5
+ ## Installation
6
+
7
+ $ gem install squill
8
+
9
+ ## Configuration
10
+
11
+ Squill will sometimes want to open an editor for you to insert your SQL (if you're not adding from a file). If you want to use an editor other than the default `vi` then you should set your editor environment variable.
12
+
13
+ $ export EDITOR="/path/to/editor"
14
+
15
+ Squill was written to save me many many minutes of my life spent re-writing ad-hoc SQL I had already written which eventually added up to my entire existence. (Basically)
16
+
17
+ You might want to try saving yourself even a bit more time with an alias.
18
+
19
+ $ alias sq='squill'
20
+
21
+ ## Usage
22
+
23
+ ### Full help on command-line
24
+
25
+ $ squill help
26
+
27
+ ### Adding Squills
28
+
29
+ To add a squill:
30
+
31
+ $ squill add some-sql-i-wrote
32
+ $ squill a some-sql-i-wrote
33
+
34
+ With no other options you will be prompted for a description and it will open your `$EDITOR` or simply `vi`. (See Configuration)
35
+
36
+ Names are strings and can have any characters but I'd suggest you keep it simple or strange things might happen. Who knows.
37
+
38
+ To add a squill from a file:
39
+
40
+ $ squill add some-sql /path/to/ad-hoc.sql
41
+
42
+ Descriptions can be input on the command-line:
43
+
44
+ $ squill add does-weird-stuff path/to/file.sql -d 'this sql does something strange'
45
+
46
+ ### Searching Squills
47
+
48
+ To search squills by name and description use the search command:
49
+
50
+ $ squill search foo
51
+ $ squill s foo
52
+
53
+ You can give multi-word search strings with quotes:
54
+
55
+ $ squill search 'from that_one_table'
56
+
57
+ ### Outputting Squill SQL
58
+
59
+ Printing the SQL associated with the squill:
60
+
61
+ $ squill print some-sql
62
+ $ squill p some-sql
63
+ $ squill p some-sql | psql
64
+
65
+ ### Listing All Squills
66
+
67
+ If you're having a hard time finding what you want sometimes its best to just list them all and stare blankly at your screen for a while.
68
+
69
+ $ squill list
70
+
71
+ ### Deleting Squills
72
+
73
+ Eventually you wont need that SQL ever again. Lucky you.
74
+
75
+ $ squill delete i-wish-it-was-all-of-them
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it ( https://github.com/abeering/squill/fork )
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
data/bin/squill ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby -wU
2
+
3
+ require 'squill'
4
+
5
+ Squill::CLIClient.start(ARGV)
data/lib/squill.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "squill/version"
2
+ require 'squill/cli'
3
+
4
+ module Squill
5
+
6
+ end
data/lib/squill/cli.rb ADDED
@@ -0,0 +1,124 @@
1
+ require 'thor'
2
+ require 'squill/squill_file'
3
+ require 'squill/squill_file_searcher'
4
+
5
+ module Squill
6
+ class CLIClient < Thor
7
+
8
+ include Thor::Actions
9
+
10
+ desc "add <name> (<file>)", "Adds a file to squill library."
11
+ long_desc <<-ADD_DESC
12
+ Adds sql in <file> to squill library with name <name>.
13
+ If no <file> is given, will open your EDITOR or `vi` by default to get SQL.
14
+
15
+ Examples:
16
+
17
+ Add a squill and prompt for the description and SQL:
18
+
19
+ `squill add just-some-sql`
20
+
21
+ Add a squill from a file:
22
+
23
+ `squill add just-some-sql path/to/file.sql`
24
+
25
+ Include a description:
26
+
27
+ `squill add does-weird-stuff path/to/file.sql -d 'this sql does something strange'`
28
+ ADD_DESC
29
+ option :desc, :type => :string, :aliases => :d, :desc => "Can take a description on command-line instead of opening EDITOR for description body."
30
+ option :replace, :type => :boolean, :aliases => :r, :desc => "Replaces a squill named <name> which already exists."
31
+ def add(name, file=nil)
32
+ squill_file = Squill::SquillFile.new(name)
33
+ if squill_file.exists_as_squill_file? && !options[:replace]
34
+ puts "\na squill by this name already exists. use the the --replace option to replace it.\n"
35
+ return
36
+ end
37
+ squill_file.description = options[:desc].nil? ? ask("Briefly describe #{name}: ") : options[:desc]
38
+ if file.nil?
39
+ squill_file.set_sql
40
+ else
41
+ squill_file.set_sql_from_file(file)
42
+ end
43
+ squill_file.save
44
+ puts "\nsaved squill #{name}.\n"
45
+ end
46
+ map "a" => "add"
47
+
48
+ desc "print <name>", "Outputs file from squill library."
49
+ long_desc <<-RUN_DESC
50
+ Adds sql in <file> to Squill library with name <name>.
51
+ If no description option is given, will open the EDITOR set in your environment.
52
+
53
+ Example:
54
+
55
+ `squill run just-some-sql`
56
+ RUN_DESC
57
+ def print(name)
58
+ squill_file = Squill::SquillFile.new(name)
59
+ if squill_file.exists_as_squill_file?
60
+ puts squill_file.sql
61
+ end
62
+ end
63
+ map "p" => "print"
64
+
65
+ desc "search <string>", "Searches for squills whose name or description match <string>."
66
+ long_desc <<-SEARCH_DESC
67
+ Adds sql in <file> to Squill library with name <name>.
68
+ If no description option is given, will open the EDITOR set in your environment.
69
+
70
+ Example:
71
+
72
+ `squill search some_table`
73
+ SEARCH_DESC
74
+ def search(search_string)
75
+ searcher = Squill::SquillFileSearcher.new
76
+ results = searcher.search(search_string)
77
+ results.each { |result|
78
+ puts "#{result[:name_highlight]} - #{result[:description_highlight]}"
79
+ }
80
+ puts "\nfound #{results.length} squills.\n"
81
+ end
82
+ map "s" => "search"
83
+
84
+ desc "delete <name>", "Deletes squill with name matching <name>."
85
+ long_desc <<-DELETE_DESC
86
+ Deletes squill matching <name>.
87
+
88
+ Example:
89
+
90
+ `squill delete some-sql`
91
+ DELETE_DESC
92
+ def delete(name)
93
+ squill_file = Squill::SquillFile.new(name)
94
+ if squill_file.exists_as_squill_file?
95
+ if yes?("are you sure you want to delete #{name}?")
96
+ squill_file.delete
97
+ puts "\nsquill #{name} deleted.\n"
98
+ end
99
+ else
100
+ puts "\ncould not find squill named #{name} to delete.\n"
101
+ end
102
+ end
103
+ map "d" => "delete"
104
+
105
+ desc "list", "Lists all squills."
106
+ long_desc <<-LIST_DESC
107
+ Lists all available squills.
108
+
109
+ Example:
110
+
111
+ `squill list`
112
+ LIST_DESC
113
+ def list
114
+ searcher = Squill::SquillFileSearcher.new
115
+ results = searcher.list
116
+ results.each { |result|
117
+ puts "#{result.name} - #{result.description}"
118
+ }
119
+ puts "\nlisted #{results.length} squills.\n"
120
+ end
121
+ map "l" => "list"
122
+
123
+ end
124
+ end
@@ -0,0 +1,116 @@
1
+ module Squill
2
+ class SquillFile
3
+
4
+ attr_accessor :description, :sql, :name
5
+
6
+ def initialize(name)
7
+ ensure_squill_dir
8
+ @name = name
9
+ if exists_as_squill_file?
10
+ load_from_squill_file
11
+ end
12
+ end
13
+
14
+ def load_from_squill_file
15
+ orig_file = File.open(squill_file, "r")
16
+ content = orig_file.read
17
+ @description = content.match(/#Description: (.+?)\n/m)[1]
18
+ @sql = content.match(/#SQL:\n(.+?)#ENDSQL;/m)[1]
19
+ orig_file.close
20
+ end
21
+
22
+ def exists_as_squill_file?
23
+ File.exist?(squill_file)
24
+ end
25
+
26
+ def set_sql_from_file(filename)
27
+ orig_file = File.open(filename, "r")
28
+ @sql = orig_file.read
29
+ orig_file.close
30
+ end
31
+
32
+ def set_sql
33
+ prep_tempfile_for_sql
34
+ set_tempfile
35
+ @sql = read_tempfile_for_sql
36
+ end
37
+
38
+ def save
39
+ write_file
40
+ end
41
+
42
+ def delete
43
+ File.delete(squill_file)
44
+ end
45
+
46
+ def yaml
47
+ { name: @name, description: @description, sql: @sql }
48
+ end
49
+
50
+ private
51
+
52
+ def prep_tempfile_for_sql
53
+ file = File.open(tempfile, "w")
54
+ file.write <<-SQL_TEMPLATE
55
+
56
+ #ENDSQL;
57
+ # Insert SQL to be added to squill above these commented lines
58
+ SQL_TEMPLATE
59
+ file.close
60
+ end
61
+
62
+ def read_tempfile_for_sql
63
+ file = File.open(tempfile, "r")
64
+ sql = ""
65
+ file.each_line { |line|
66
+ break if line.match(/^#ENDSQL;/)
67
+ sql << line
68
+ }
69
+ file.close
70
+ sql
71
+ end
72
+
73
+ def set_tempfile
74
+ system(user_editor, tempfile)
75
+ end
76
+
77
+ def write_file
78
+ file = File.open(squill_file, "w")
79
+ file.write(contents_for_squill_file_write)
80
+ file.close
81
+ end
82
+
83
+ def contents_for_squill_file_write
84
+ <<-OUTPUT_FILE
85
+ #Name: #{@name}
86
+ #Description: #{@description}
87
+ #SQL:
88
+ #{@sql}
89
+ #ENDSQL;
90
+ OUTPUT_FILE
91
+ end
92
+
93
+ def user_editor
94
+ !ENV['EDITOR'].nil? && !ENV['EDITOR'].empty? ? ENV['EDITOR'] : 'vi'
95
+ end
96
+
97
+ def ensure_squill_dir
98
+ if !File.directory?(squill_dir)
99
+ Dir.mkdir(squill_dir)
100
+ end
101
+ end
102
+
103
+ def tempfile
104
+ File.join(squill_dir, '.temp')
105
+ end
106
+
107
+ def squill_file
108
+ File.join(squill_dir, "#{name}.squill")
109
+ end
110
+
111
+ def squill_dir
112
+ File.join(File.expand_path('~'),'.squill')
113
+ end
114
+
115
+ end
116
+ end
@@ -0,0 +1,35 @@
1
+ module Squill
2
+ class SquillFileSearcher
3
+
4
+ def search(search_string)
5
+ grep_results = `egrep -l '^#(Description|Name):.*#{search_string}.*$' #{search_argument} | sort`
6
+ grep_results.split("\n").map { |result|
7
+ squillfile = Squill::SquillFile.new(File.basename(result.strip).gsub(/.squill/,''))
8
+ {
9
+ name_highlight: highlight(squillfile.name, search_string),
10
+ description_highlight: highlight(squillfile.description, search_string)
11
+ }
12
+ }
13
+ end
14
+
15
+ def list
16
+ results = `find #{squill_search_dir} -type f -name '*.squill' | sort`
17
+ results.split("\n").map { |result| Squill::SquillFile.new(File.basename(result.strip).gsub(/.squill/,'')) }
18
+ end
19
+
20
+ private
21
+
22
+ def highlight(string, search_string)
23
+ string.gsub(/(#{search_string})/) { |found| "\e[1m#{found}\e[0m" }
24
+ end
25
+
26
+ def search_argument
27
+ File.join(squill_search_dir,'*')
28
+ end
29
+
30
+ def squill_search_dir
31
+ File.join(File.expand_path('~'),'.squill')
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Squill
2
+ VERSION = "0.0.1"
3
+ end
data/squill.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'squill/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "squill"
8
+ spec.version = Squill::VERSION
9
+ spec.authors = ["Andy Beering"]
10
+ spec.email = ["andy.beering@gmail.com"]
11
+ spec.summary = %q{Simple CLI tool for storing, searching and executing ad-hoc SQL.}
12
+ spec.description = %q{Simple CLI tool for storing, searching and executing ad-hoc SQL.}
13
+ spec.homepage = "https://github.com/abeering/squill"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'thor'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: squill
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andy Beering
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Simple CLI tool for storing, searching and executing ad-hoc SQL.
56
+ email:
57
+ - andy.beering@gmail.com
58
+ executables:
59
+ - squill
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/squill
69
+ - lib/squill.rb
70
+ - lib/squill/cli.rb
71
+ - lib/squill/squill_file.rb
72
+ - lib/squill/squill_file_searcher.rb
73
+ - lib/squill/version.rb
74
+ - squill.gemspec
75
+ homepage: https://github.com/abeering/squill
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Simple CLI tool for storing, searching and executing ad-hoc SQL.
99
+ test_files: []