Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Sep 9, 2021
0 parents commit 086615d
Show file tree
Hide file tree
Showing 26 changed files with 1,523 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/Gemfile.lock
/doc/
/pkg/
/vendor/bundle/
.DS_Store
.yardoc
*.log
*.swp
*~
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--colour --format documentation
1 change: 1 addition & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--markup markdown --title "hexdump Documentation" --protected --quiet
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
source 'https://rubygems.org'

gemspec

group :development do
gem 'rake'
gem 'rubygems-tasks', '~> 0.2'
gem 'rspec', '~> 3.0'
gem 'kramdown'
gem 'yard', '~> 0.9'
gem 'yard-spellcheck'
end
20 changes: 20 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2021 Hal Brodigan

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
124 changes: 124 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# command_mapper

* [Source](https://github.com/postmodern/command_mapper)
* [Issues](https://github.com/postmodern/command_mapper/issues)
* [Documentation](http://rubydoc.info/gems/command_mapper/frames)

## Description

Command Mapper maps a command's arguments to Class attributes to allow easily
and safely automating commands.

## Features

* Supports defining commands as Ruby classes.
* Supports mapping in options and additional arguments.
* Supports common option formats:
* `List`: `VALUE,...`
* `KeyValue`: `KEY:VALUE` or `KEY=VALUE`
* `Map`: `enabled|disables` or `yes|no`
* Supports mapping in sub-commands.
* Safely executes commands and their individual command-line arguments,
in order to prevent command or option injection.
* Allows running the command via `IO.popen` to read the command's output.
* Allows running commands with additional environment variables.
* Allows overriding the command name or path to the command.
* Allows running commands via `sudo`.

## Examples

```ruby
require 'command_mapper/command'

#
# Represents the `grep` command
#
class Grep < CommandMapper::Command

command "grep"

option "--extended-regexp"
option "--fixed-strings"
option "--basic-regexp"
option "--perl-regexp"
option "--regexp", equals: true, value: true
option "--file", equals: true, value: true
option "--ignore-case"
option "--no-ignore-case"
option "--word-regexp"
option "--line-regexp"
option "--null-data"
option "--no-messages"
option "--invert-match"
option "--version"
option "--help"
option "--max-count", equals: true, value: true
option "--byte-offset"
option "--line-number"
option "--line-buffered"
option "--with-filename"
option "--no-filename"
option "--label", equals: true, value: true
option "--only-matching"
option "--quiet"
option "--binary-files", equals: true, value: true
option "--text"
option "-I", name: :ignore_binary
option "--directories", equals: true, value: true
option "--devices", equals: true, value: true
option "--recursive"
option "--dereference-recursive"
option "--include", equals: true, value: true
option "--exclude", equals: true, value: true
option "--exclude-from", equals: true, value: true
option "--exclude-dir", equals: true, value: true
option "--files-without-match", value: true
option "--files-with-matches"
option "--count"
option "--initial-tab"
option "--null"
option "--before-context", equals: true, value: true
option "--after-context", equals: true, value: true
option "--context", equals: true, value: true
option "--group-separator", equals: true, value: true
option "--no-group-separator"
option "--color", equals: :optional, value: {required: false}
option "--colour", equals: :optional, value: {required: false}
option "--binary"

argument :patterns
argument :file, required: false

end
```

## Requirements

* [ruby] >= 2.0.0

## Install

```shell
$ gem install command_mapper
```

### Gemfile

```ruby
gem 'command_mapper'
```

### gemspec

```ruby
gemspec.add_dependency 'command_mapper', '~> 0.1'
```

## License

Copyright (c) 2021 Hal Brodigan

See {file:LICENSE.txt} for license information.

[command_mapper]: https://github.com/postmodern/command_mapper.rb#readme
[ruby]: htt[s://www.ruby-lang.org/
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rubygems'

require 'rubygems/tasks'
Gem::Tasks.new

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
task :test => :spec
task :default => :spec

require 'yard'
YARD::Rake::YardocTask.new
61 changes: 61 additions & 0 deletions commnad_mapper.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# encoding: utf-8

require 'yaml'

Gem::Specification.new do |gem|
gemspec = YAML.load_file('gemspec.yml')

gem.name = gemspec.fetch('name')
gem.version = gemspec.fetch('version') do
lib_dir = File.join(File.dirname(__FILE__),'lib')
$LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)

require 'command_mapper/version'
CommandMapper::VERSION
end

gem.summary = gemspec['summary']
gem.description = gemspec['description']
gem.licenses = Array(gemspec['license'])
gem.authors = Array(gemspec['authors'])
gem.email = gemspec['email']
gem.homepage = gemspec['homepage']
gem.metadata = gemspec['metadata'] if gemspec['metadata']

glob = lambda { |patterns| gem.files & Dir[*patterns] }

gem.files = `git ls-files`.split($/)
gem.files = glob[gemspec['files']] if gemspec['files']

gem.executables = gemspec.fetch('executables') do
glob['bin/*'].map { |path| File.basename(path) }
end
gem.default_executable = gem.executables.first if Gem::VERSION < '1.7.'

gem.extensions = glob[gemspec['extensions'] || 'ext/**/extconf.rb']
gem.test_files = glob[gemspec['test_files'] || '{test/{**/}*_test.rb']
gem.extra_rdoc_files = glob[gemspec['extra_doc_files'] || '*.{txt,md}']

gem.require_paths = Array(gemspec.fetch('require_paths') {
%w[ext lib].select { |dir| File.directory?(dir) }
})

gem.requirements = gemspec['requirements']
gem.required_ruby_version = gemspec['required_ruby_version']
gem.required_rubygems_version = gemspec['required_rubygems_version']
gem.post_install_message = gemspec['post_install_message']

split = lambda { |string| string.split(/,\s*/) }

if gemspec['dependencies']
gemspec['dependencies'].each do |name,versions|
gem.add_dependency(name,split[versions])
end
end

if gemspec['development_dependencies']
gemspec['development_dependencies'].each do |name,versions|
gem.add_development_dependency(name,split[versions])
end
end
end
22 changes: 22 additions & 0 deletions gemspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: command_mapper
summary: Maps commands and options to Ruby classes
description:
CommandMapper maps commands and their options to classes, keyword arguments,
and methods.

license: MIT
authors: Postmodern
email: [email protected]
homepage: https://github.com/postmodern/command_mapper.rb#readme
has_yard: true

metadata:
documentation_uri: https://rubydoc.info/gems/command_mapper
source_code_uri: https://github.com/postmodern/command_mapper.rb
bug_tracker_uri: https://github.com/postmodern/command_mapper.rb/issues
changelog_uri: https://github.com/postmodern/command_mapper.rb/blob/master/ChangeLog.md

required_ruby_version: ">= 2.0.0"

development_dependencies:
bundler: ~> 2.0
23 changes: 23 additions & 0 deletions lib/command_mapper/argument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'command_mapper/argument_value'

module CommandMapper
class Argument < ArgumentValue

attr_reader :name

def initialize(name, repeats: false, **kwargs,&block)
@name = name
@repeats = repeats

super(**kwargs,&block)
end

def repeats?
@repeats
end

def argv(value)
end

end
end
25 changes: 25 additions & 0 deletions lib/command_mapper/argument_value.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module CommandMapper
class ArgumentValue

attr_reader :format

def initialize(format: nil, required: true, &block)
@format = format
@required = required

@formatter = block
end

def required?
@required
end

def optional?
!@required
end

def argv(value)
end

end
end
Loading

0 comments on commit 086615d

Please sign in to comment.