Class: ToPass::Cli
- Inherits:
-
Object
- Object
- ToPass::Cli
- Defined in:
- lib/to_pass/cli.rb
Overview
ToPass::Cli wraps the ugly code needed for option-parsing an such
The default values for the options can be passed in to make different executables behave differently.
Class Method Summary collapse
-
.algorithms ⇒ Object
output list of algorithms.
-
.converters ⇒ Object
output list of converters.
-
.setup(value = nil) ⇒ Object
setup working directory.
Instance Method Summary collapse
-
#get_input_string ⇒ Object
protected
get the input string.
-
#initialize(options = {}) ⇒ Cli
constructor
creates a new Cli-wrapper for option-parsing and such.
-
#output ⇒ Object
output the result of the string transformation.
-
#parse_options(options = {}) ⇒ Object
protected
parse the options.
-
#transform ⇒ Object
protected
perform “heavy” work.
Constructor Details
#initialize(options = {}) ⇒ Cli
creates a new Cli-wrapper for option-parsing and such. Almost everthing needed is done inside the initalizer.
The default options can be overriden. Defaults are:
= {
:algorithm => 'basic_de',
:pipe_out => false,
:pipe_in => false
}
If you want to override always, you can create a config file with the following name:
~/.to_pass/config
The file is expected to be a YAML-File. Keys and values are transformed symbols automatically. See ToPass::ConfigReader for details.
29 30 31 32 33 |
# File 'lib/to_pass/cli.rb', line 29 def initialize( = {}) @options = () @string = get_input_string @password = transform end |
Class Method Details
.algorithms ⇒ Object
output list of algorithms
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/to_pass/cli.rb', line 46 def algorithms puts "" puts " available password algorithms" puts " ============================================" AlgorithmReader.discover.each do |algorithm| puts " - #{algorithm}" end puts " ============================================" puts "" end |
.converters ⇒ Object
output list of converters
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/to_pass/cli.rb', line 58 def converters puts "" puts " available converters for password algorithms" puts " ============================================" ConverterReader.new.discover.each do |converter| puts " - #{converter}" end puts " ============================================" puts "" end |
.setup(value = nil) ⇒ Object
setup working directory
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/to_pass/cli.rb', line 70 def setup(value = nil) dir = Pathname.new(value || Directories[:user]). dir.mkpath (dir + 'algorithms').mkpath (dir + 'converters').mkpath if File.exist?(dir + 'config') puts "configuration in #{dir} not overwritten" else File.open((dir + 'config'), 'w') do |file| config = {} ConfigReader.load.each do |key, value| config[key.to_s] = ( value.is_a?(Symbol) ? value.to_s : value ) end YAML.dump(config, file) end end puts "successfully created configuration directory in #{dir}" end |
Instance Method Details
#get_input_string ⇒ Object (protected)
get the input string
168 169 170 171 172 173 174 |
# File 'lib/to_pass/cli.rb', line 168 def get_input_string unless @options[:pipe_in] ARGV[0] else $stdin.read.chomp end end |
#output ⇒ Object
output the result of the string transformation
36 37 38 39 40 41 42 |
# File 'lib/to_pass/cli.rb', line 36 def output if @options[:pipe_out] $stdout << @password else puts @password end end |
#parse_options(options = {}) ⇒ Object (protected)
parse the options
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/to_pass/cli.rb', line 98 def ( = {}) = { :algorithm => 'basic_de', :pipe_out => false, :pipe_in => false }.merge( ConfigReader.load ).merge() = OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} [options] passphrase" opts.separator "" opts.on('-a', '--algorithm ALGORITM', "use specified algorithm for transformation") do |value| [:algorithm] = value end opts.on('-p', '--[no-]pipe', "pipe result to stdout (without trailing linebreak)") do |value| [:pipe_out] = value end opts.on('-c', '--config [PATH]', "look in PATH for configurations (instead of ~/.to_pass)") do |value| dir = Pathname.new(value). if dir.exist? [:path] = dir else puts 'configuration path not found' puts "run '#{File.basename($0)} --setup #{dir}' to set it up" exit 1 end end ## ACTIONS opts.on('--setup [PATH]', "create a configuration directory") do |value| Cli.setup(value) exit end opts.on('-A', '--algorithms', "list available algorithms") do |value| Cli.algorithms exit end opts.on('-C', '--converters', "list available converters for password algorithms") do |value| Cli.converters exit end opts.separator "" opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end .parse! if ARGV[0].nil? [:pipe_in] = [:pipe_out] = true end rescue OptionParser::InvalidOption puts exit 0 end |