bashy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/LICENSE +3 -0
  2. data/README +3 -0
  3. data/Rakefile +45 -0
  4. data/bin/bashy +73 -0
  5. data/lib/bashy.rb +128 -0
  6. metadata +72 -0
data/LICENSE ADDED
@@ -0,0 +1,3 @@
1
+ == Bashy
2
+
3
+ Put appropriate LICENSE for your project here.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ == Bashy
2
+
3
+ You should document your project here.
@@ -0,0 +1,45 @@
1
+ #
2
+ # To change this template, choose Tools | Templates
3
+ # and open the template in the editor.
4
+
5
+
6
+ require 'rubygems'
7
+ require 'rake'
8
+ require 'rake/clean'
9
+ require 'rake/gempackagetask'
10
+ require 'rake/rdoctask'
11
+ require 'rake/testtask'
12
+
13
+ spec = Gem::Specification.new do |s|
14
+ s.name = 'bashy'
15
+ s.version = '0.0.1'
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ['README', 'LICENSE']
18
+ s.summary = 'Your summary here'
19
+ s.description = s.summary
20
+ s.author = ''
21
+ s.email = ''
22
+ s.executables = ['bashy']
23
+ s.files = %w(LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
24
+ s.require_path = "lib"
25
+ s.bindir = "bin"
26
+ end
27
+
28
+ Rake::GemPackageTask.new(spec) do |p|
29
+ p.gem_spec = spec
30
+ p.need_tar = true
31
+ p.need_zip = true
32
+ end
33
+
34
+ Rake::RDocTask.new do |rdoc|
35
+ files =['README', 'LICENSE', 'lib/**/*.rb']
36
+ rdoc.rdoc_files.add(files)
37
+ rdoc.main = "README" # page to start on
38
+ rdoc.title = "Bashy Docs"
39
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
40
+ rdoc.options << '--line-numbers'
41
+ end
42
+
43
+ Rake::TestTask.new do |t|
44
+ t.test_files = FileList['test/**/*.rb']
45
+ end
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), *%w[.. lib bashy])
4
+
5
+ require 'rubygems'
6
+ require 'highline/import'
7
+
8
+ STDOUT.sync = true
9
+
10
+ # This hash will hold all of our passed command line options.
11
+ options = {}
12
+
13
+ begin
14
+ raise ArgumentError.new('You need to specify a command to run.') unless ARGV.length > 0
15
+
16
+ options[:command] = ARGV[0]
17
+ # Translate the command aliases into real commands.
18
+ options[:command] = 'add' if ['a'].include?(options[:command])
19
+ options[:command] = 'list' if ['ls'].include?(options[:command])
20
+ options[:command] = 'remove' if ['rm'].include?(options[:command])
21
+ options[:command] = 'run' if ['r'].include?(options[:command])
22
+ options[:command] = 'show' if ['s'].include?(options[:command])
23
+ options[:command] = 'pull' if ['p'].include?(options[:command])
24
+
25
+ raise ArgumentError.new('You need to specify a valid command to run.') unless ['add', 'list', 'remove', 'run', 'show', 'pull'].include?(options[:command])
26
+ raise ArgumentError.new('You need to give this command a command name or path to work on.') unless ['list'].include?(options[:command]) || ARGV.length > 1
27
+
28
+ case options[:command]
29
+ when 'add'
30
+ options[:name] = ARGV[1]
31
+ options[:path] = ARGV[2]
32
+ when 'run'
33
+ options[:name] = ARGV[1]
34
+ options[:args] = ARGV[2..-1]
35
+ when 'show', 'pull'
36
+ options[:name] = ARGV[1]
37
+ when 'remove'
38
+ options[:names] = ARGV[1..-1]
39
+ end
40
+
41
+ case options[:command]
42
+ when 'add'
43
+ if options[:path].nil?
44
+ options[:snippet] = ask('Please enter the contents you want to have in this bashy snippet: ') { |q| q.default = nil }
45
+ end
46
+ Bashy::Add.new(options)
47
+ when 'list'
48
+ snippets = Bashy::List.new(options).snippets
49
+ puts (snippets.empty?) ? 'No snippets.' : snippets
50
+ when 'remove'
51
+ Bashy::Remove.new(options)
52
+ when 'run'
53
+ Bashy::Run.new(options)
54
+ when 'show'
55
+ puts Bashy::Show.new(options).contents
56
+ end
57
+ rescue Exception => e
58
+ # Show help.
59
+ puts e
60
+ puts ''
61
+ puts 'bashy: usage: bashy COMMAND [ARGS]'
62
+ puts ''
63
+ puts 'Commands'
64
+ puts ' add: Adds a snippet. You must provide a name. You can provide a path'
65
+ puts ' to a file to import, but if ommited you can enter your snippet'
66
+ puts ' into the STDIN'
67
+ puts ' list: Lists all added snippets.'
68
+ puts ' remove: Removes all snippets matching the names provided.'
69
+ puts ' run: Runs the given command, passing it any arguments given after'
70
+ puts ' the command name.'
71
+ puts ' show: Shows the contents of the given command file.'
72
+ puts ' help: Shows this help manual.'
73
+ end
@@ -0,0 +1,128 @@
1
+ require 'optparse'
2
+ require 'fileutils'
3
+ require 'etc'
4
+ require 'yaml'
5
+
6
+ module Bashy
7
+ # Our folder where we'll be storing our snippets and config.
8
+ SNIPPET_PATH = File.join(Etc.getpwuid.dir, '.bashy/snippets')
9
+ CONFIG_PATH = File.join(SNIPPET_PATH, 'bashy_config.yml')
10
+
11
+ class << self
12
+ attr_accessor :config
13
+
14
+ def check_and_load_config_directory
15
+ # Check that our storage path is in tact and ready to write to.
16
+ FileUtils.mkdir_p(SNIPPET_PATH) unless File.exists?(SNIPPET_PATH)
17
+ # Now we'll create our config file if it doesn't exist.
18
+ unless File.exists?(CONFIG_PATH)
19
+ FileUtils.touch(CONFIG_PATH)
20
+ @config = default_config_file
21
+ save_config
22
+ else
23
+ # Setup our config object to be accessed everywhere.
24
+ File.open(CONFIG_PATH) do |yf|
25
+ @config = YAML::load(yf)
26
+ end
27
+ end
28
+ end
29
+
30
+ def save_config
31
+ File.open(CONFIG_PATH, 'w' ) do |out|
32
+ YAML.dump(@config, out)
33
+ end
34
+ end
35
+
36
+ private
37
+ def default_config_file
38
+ { :snippets => [] }
39
+ end
40
+ end
41
+
42
+ class ArgumentError < RuntimeError; end
43
+
44
+ class Clean
45
+ def initialize(options = {})
46
+
47
+ end
48
+ end
49
+
50
+ class Add
51
+ def initialize(options = {})
52
+ # Check that our directories are in tact.
53
+ Bashy::check_and_load_config_directory
54
+ # If we haven't been passed the name of the bash snippet, raise an exception.
55
+ raise(ArgumentError) unless options.has_key?(:name)
56
+ # We're going to do different things here if we're operating on a path or the user wants to enter a snippet of code directly.
57
+ snippet_path = File.join(SNIPPET_PATH, options[:name])
58
+ if(!options[:path].nil?)
59
+ FileUtils.cp(options[:path], snippet_path)
60
+ else(options.has_key?(:snippet))
61
+ File.open(snippet_path, 'w') do |f|
62
+ f.write("#!/usr/bin/env bash\n") unless options[:snippet] =~ /^#!/
63
+ f.write(options[:snippet])
64
+ end
65
+ FileUtils.chmod(0777, snippet_path)
66
+ end
67
+ Bashy.config[:snippets] << options[:name]
68
+ Bashy.save_config
69
+ end
70
+ end
71
+
72
+ class List
73
+ attr_reader :snippets
74
+
75
+ def initialize(options = {})
76
+ # Check that our directories are in tact.
77
+ Bashy::check_and_load_config_directory
78
+ # Set our class accessor for the snippets to the array of our snippets.
79
+ @snippets = Bashy.config[:snippets]
80
+ end
81
+ end
82
+
83
+ class Remove
84
+ def initialize(options = {})
85
+ # Check that our directories are in tact.
86
+ Bashy::check_and_load_config_directory
87
+ # Loop through passed names, and remove all that we can.
88
+ options[:names].each do |snippet|
89
+ if Bashy.config[:snippets].include?(snippet)
90
+ Bashy.config[:snippets].delete(snippet)
91
+ Bashy.save_config
92
+ FileUtils.rm(File.join(SNIPPET_PATH, snippet))
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ class Run
99
+ def initialize(options = {})
100
+ # Check that our directories are in tact.
101
+ Bashy::check_and_load_config_directory
102
+ unless options[:name].nil?
103
+ exec(File.join(SNIPPET_PATH, options[:name]), *options[:args])
104
+ end
105
+ end
106
+ end
107
+
108
+ class Show
109
+ attr_reader :contents
110
+
111
+ def initialize(options = {})
112
+ # Check that our directories are in tact.
113
+ Bashy::check_and_load_config_directory
114
+ unless options[:name].nil?
115
+ File.open(File.join(SNIPPET_PATH, options[:name])) do |f|
116
+ @contents = f.readlines
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+ class Pull
123
+ def initialize(options = {})
124
+ # Check that our directories are in tact.
125
+ Bashy::check_and_load_config_directory
126
+ end
127
+ end
128
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bashy
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - ""
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-17 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Your summary here
23
+ email: ""
24
+ executables:
25
+ - bashy
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README
30
+ - LICENSE
31
+ files:
32
+ - LICENSE
33
+ - README
34
+ - Rakefile
35
+ - bin/bashy
36
+ - lib/bashy.rb
37
+ has_rdoc: true
38
+ homepage:
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Your summary here
71
+ test_files: []
72
+