Skip to content

Shell Command Abstraction - Let's you interact with shell commands as if they were objects.

License

Notifications You must be signed in to change notification settings

ktlacaelel/abstract_command

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Abstract Command

Shell Command Abstraction - Let's you interact with shell commands as if they were objects.

Ideas behind:

  • Enforces standardization.
  • Enforces separation of command definition and consumption.
  • Enforces configuration over code.
  • Enforces configuration over refactoring.
  • Enforces simple shell-command definition.
  • Enforces automatic sanitization of variables that get interpolated.
  • Provides a simple Object Oriented Interface.
  • Provides a scope for variables that belong to the command.
  • Provides getters and setter for every interpolation in command.
  • Provides a neat interface that plugs to data structures transparently.
  • Avoids methods with many arguments.
  • Avoids changes in the standared libarary: system, backtick, etc.

Hello world example:

require 'abstract_command'

# Hello World Example.
class HelloCommand < AbstractCommand
  def template
    'echo Hello %<name>s'
  end
end

command = HelloCommand.new
command.name = 'World.'

puts command.to_s
$ ruby example.rb
echo Hello World.

Dynamic variables in command:

require 'abstract_command'

class VarsCommand < AbstractCommand
  def template
    'echo %<var_1>s %<var_2>s %<var_3>s %<var_4>s %<var_5>s'
  end
end

command = VarsCommand.new
command.var_1 = 'one'
command.var_2 = 'two'
command.var_3 = 'three'
command.var_4 = 'four'
command.var_5 = 'five'
puts command.to_s
puts command.system
$ ruby variables.rb
echo one two three four five
one two three four five

Namespace suggestion:

require 'abstract_command'

module Command

  class Hello < AbstractCommand
    def template
      'echo Hello %<name>s'
    end
  end

  class Bye < AbstractCommand
    def template
      'echo Bye %<name>s'
    end
  end

end

command = Command::Hello.new
command.name = 'world'
puts command.to_s
puts command.system
puts command.backtick
$ ruby namespace_suggestion.rb
echo Hello world
Hello world
true
Hello world

Fast initialization:

require 'abstract_command'

module Command
  class Hello < AbstractCommand
    def template
      'echo Hello %<name>s'
    end
  end
end

command = Command::Hello.new(:name => 'Kazu')
puts command.to_s
puts command.system
$ ruby constructor.rb
echo Hello Kazu
Hello Kazu
true

Automatic Sanitization:

require 'abstract_command'

module Command
  class Hello < AbstractCommand
    def template
      'echo Hello %<name>s'
    end
  end
end

command = Command::Hello.new(:name => '; touch /tmp/x')
puts command.to_s
puts command.system
$ ruby sanitization.rb
echo Hello \;\ touch\ /tmp/x
Hello ; touch /tmp/x
true

Transparent data interpretation:

require 'abstract_command'

developers = [
  { :name => 'Kazu' },
  { :name => 'Adrian' },
  { :name => 'Cesar' },
  { :name => 'Sergio' }
]

module Command
  class Hello < AbstractCommand
    def template
      'echo Hello %<name>s'
    end
  end
end

developers.each do |developer|
  Command::Hello.new(developer).system
end
$ ruby hello_developers.rb
Hello Kazu
Hello Adrian
Hello Cesar
Hello Sergio

I hope it is useful to you :)

About

Shell Command Abstraction - Let's you interact with shell commands as if they were objects.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages