osp 0.6.1 → 0.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4a335af911427aaf3e8a29425695f112f14adca0
4
- data.tar.gz: fc95cd8f1afcec58eb93f183f45e65403c59c76c
2
+ SHA256:
3
+ metadata.gz: 86f11c7cb6af7a4b1a75643ea75910022c5bd1dee4f369e4f97ef51595dc8841
4
+ data.tar.gz: c4e7a36a5c4caa26f304068170319cb0a47140bfd732d0a3f2d88a32b793c886
5
5
  SHA512:
6
- metadata.gz: d1b068af34fc47a3acd0864a97f03e3b137c87c53b3a79f138aae09a2457112a6ec02cfc0bef6345c8bb2ccae7af7378760914c32dcdebe7035642c061f8dec4
7
- data.tar.gz: a3b0b30b76fe5590ca0516531e0a5d1108fe54365d9f12b3248492df1c0e7472b4ff77a588774353e5285529e8141897249981c8d33baf1f720772a9358194ac
6
+ metadata.gz: ea147ab56fbc29c8d407039075f6ecb2bb6739f291dc513924848570c245043b18eaa3472b745fe69df319ed9b2b8c67ad8d3650148ff1093c531f4a79d9bc41
7
+ data.tar.gz: 588e29ef9b6108b9f74cb5f8d4b0b2f1279c125ca69acd75b4f46a8e808e89baf69c5411d2ecfc55daa59f94926972636b5f74a96339902ea5cf91cc5abd8598
@@ -3,12 +3,13 @@
3
3
  root = true
4
4
 
5
5
  [*]
6
- indent_style = tab
7
- indent_size = 4
6
+ indent_style = space
7
+ indent_size = 2
8
8
  end_of_line = lf
9
9
  charset = utf-8
10
10
  trim_trailing_whitespace = false
11
11
  insert_final_newline = true
12
12
 
13
- [*.yml]
14
- indent_style = space
13
+ [*.sh]
14
+ indent_style = tab
15
+ indent_size = 4
@@ -0,0 +1,2 @@
1
+ GEM_NAME=osp
2
+ GEMSPEC_FILE=osp.gemspec
data/.gitignore CHANGED
@@ -1,9 +1,10 @@
1
- /.bundle/
2
- .setup
1
+ .env
2
+ /.idea/
3
+ /.vagrant/
3
4
  /bin/dev
4
5
  CHANGELOG-*.txt
5
6
  /coverage/
6
7
  Gemfile.lock
7
- /releases/
8
8
  *.osp
9
9
  *.osp~*
10
+ /tmp/
@@ -1,47 +1,47 @@
1
1
  before_script:
2
- - gem update --system
3
- - gem install bundler -v '~>1.13'
4
- - bundler --version
5
- - make
2
+ - gem update --system
3
+ - gem install bundler -v '~>1.13'
4
+ - bundler --version
5
+ - bundler install
6
6
 
7
7
  stages:
8
- - test
9
- - release
8
+ - test
9
+ - release
10
10
 
11
11
  test_21:
12
- image: ruby:2.1
13
- stage: test
14
- environment: test
15
- only:
16
- - tags
17
- script:
18
- - make test
12
+ image: ruby:2.1
13
+ stage: test
14
+ environment: test
15
+ only:
16
+ - tags
17
+ script:
18
+ - ./bin/test.sh
19
19
 
20
20
  test_22:
21
- image: ruby:2.2
22
- stage: test
23
- environment: test
24
- only:
25
- - tags
26
- script:
27
- - make test
21
+ image: ruby:2.2
22
+ stage: test
23
+ environment: test
24
+ only:
25
+ - tags
26
+ script:
27
+ - ./bin/test.sh
28
28
 
29
29
  test_23:
30
- image: ruby:2.3
31
- stage: test
32
- environment: test
33
- only:
34
- - tags
35
- script:
36
- - make test
30
+ image: ruby:2.3
31
+ stage: test
32
+ environment: test
33
+ only:
34
+ - tags
35
+ script:
36
+ - ./bin/test.sh
37
37
 
38
- release_gem:
39
- image: ruby:2.3
40
- stage: release
41
- environment: gem
42
- only:
43
- - tags
44
- script:
45
- - mkdir -p ~/.gem
46
- - 'printf "%s\n:rubygems_api_key: %s" "---" "${RUBYGEMSORG_API_KEY}" > ~/.gem/credentials; chmod 0600 ~/.gem/credentials'
47
- - make release
38
+ release:
39
+ image: ruby:2.4
40
+ stage: release
41
+ environment: gem
42
+ only:
43
+ - tags
44
+ script:
45
+ - mkdir -p ~/.gem
46
+ - 'printf "%s\n:rubygems_api_key: %s" "---" "${RUBYGEMSORG_API_KEY}" > ~/.gem/credentials; chmod 0600 ~/.gem/credentials'
47
+ - ./bin/release.sh
@@ -1,22 +1,23 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1
4
- - 2.2
5
- - 2.3
6
- - 2.4.0
7
- - ruby-head
3
+ - 2.1
4
+ - 2.2
5
+ - 2.3
6
+ - 2.4.0
7
+ - 2.5.0
8
+ - ruby-head
8
9
  sudo: required
9
10
  before_install:
10
- - gem update --system
11
- - gem install bundler -v '~>1.13'
12
- - bundler --version
11
+ - gem update --system
12
+ - gem install bundler -v '~>1.13'
13
+ - bundler --version
13
14
  install:
14
- - make
15
- - gem build osp.gemspec
16
- - gem install osp-*.gem
17
- - gem list -l osp
15
+ - bundler install
16
+ - gem build osp.gemspec
17
+ - gem install osp-*.gem
18
+ - gem list -l osp
18
19
  script:
19
- - make test
20
- - cd
21
- - which -a osp
22
- - osp --version
20
+ - ./bin/test.sh
21
+ - cd
22
+ - which -a osp
23
+ - osp --version
data/README.md CHANGED
@@ -5,11 +5,15 @@
5
5
  The preferred method of installation is via RubyGems.org:
6
6
  <https://rubygems.org/gems/osp>
7
7
 
8
- gem install osp
8
+ ```sh
9
+ $ gem install osp
10
+ ```
9
11
 
10
12
  or via `Gemfile`:
11
13
 
12
- gem 'osp', '~>0.6'
14
+ ```sh
15
+ $ gem 'osp', '~>0.6'
16
+ ```
13
17
 
14
18
  ## Project Links
15
19
 
@@ -0,0 +1,34 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ Vagrant.configure("2") do |config|
5
+ config.vm.box = "generic/debian9"
6
+ config.vm.box_check_update = false
7
+
8
+ config.vm.hostname = 'osp'
9
+ # config.vm.network "forwarded_port", guest: 3000, host: 3000
10
+
11
+ # config.vm.network "private_network", ip: "192.168.33.10"
12
+ config.vm.synced_folder ".", "/app"
13
+
14
+ config.vm.provider "virtualbox" do |vb|
15
+ vb.gui = false
16
+ vb.memory = 1024
17
+ end
18
+
19
+ config.vm.provision "shell" do |s|
20
+ s.env = {
21
+ 'DEBIAN_FRONTEND' => 'noninteractive',
22
+ }
23
+ s.inline = <<-SHELL
24
+ apt-get update -yqq
25
+ apt-get upgrade -y
26
+ apt-get install -y htop vim ack-grep lsof net-tools rsync ruby ruby-dev
27
+
28
+ gem update --system && \
29
+ gem install bundler -v '~>1.13.0'
30
+
31
+ echo 'done'
32
+ SHELL
33
+ end
34
+ end
@@ -0,0 +1 @@
1
+ test.rb
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Development setup.
4
+
5
+ SCRIPT_BASEDIR=$(dirname "$0")
6
+
7
+
8
+ set -e
9
+ which cp &> /dev/null || { echo 'ERROR: cp not found in PATH'; exit 1; }
10
+ which bundler &> /dev/null || { echo 'ERROR: bundler not found in PATH'; exit 1; }
11
+
12
+ cd "${SCRIPT_BASEDIR}/.."
13
+
14
+ # Create .env file.
15
+ if [[ ! -f .env ]]; then
16
+ cp .env.example .env
17
+ fi
18
+
19
+ bundler install
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Install Gem local.
4
+
5
+ SCRIPT_BASEDIR=$(dirname "$0")
6
+
7
+
8
+ option=$1
9
+ set -e
10
+ which mkdir &> /dev/null || { echo 'ERROR: mkdir not found in PATH'; exit 1; }
11
+ which mv &> /dev/null || { echo 'ERROR: mv not found in PATH'; exit 1; }
12
+ which chmod &> /dev/null || { echo 'ERROR: chmod not found in PATH'; exit 1; }
13
+ which gem &> /dev/null || { echo 'ERROR: gem not found in PATH'; exit 1; }
14
+ which bundler &> /dev/null || { echo 'ERROR: bundler not found in PATH'; exit 1; }
15
+
16
+ cd "${SCRIPT_BASEDIR}/.."
17
+
18
+ # Load Environment Variables
19
+ [[ -f .env ]] && source .env
20
+
21
+ if [[ -z "${GEMSPEC_FILE}" ]] ; then
22
+ echo 'ERROR: one of the environment variables is missing'
23
+
24
+ echo "GEMSPEC_FILE: '${GEMSPEC_FILE}'"
25
+
26
+ exit 1
27
+ fi
28
+
29
+ gem_file=$(gem build "${GEMSPEC_FILE}" 2> /dev/null | grep 'File:' | tail -1 | awk '{ print $2 }')
30
+
31
+ if [[ -z "$gem_file" ]] ; then
32
+ echo 'ERROR: gem_file variable not set'
33
+ exit 1
34
+ fi
35
+
36
+ echo "install gem file '$gem_file'"
37
+ gem install "$gem_file"
38
+
39
+ # Create tmp directory.
40
+ if [[ ! -d tmp/releases ]]; then
41
+ mkdir -p tmp/releases
42
+
43
+ chmod u=rwx,go-rwx tmp
44
+ chmod u=rwx,go-rwx tmp/releases
45
+ fi
46
+
47
+ if [[ "$option" = "-f" ]]; then
48
+ mv -v "$gem_file" tmp/releases
49
+ else
50
+ mv -v -i "$gem_file" tmp/releases
51
+ fi
data/bin/osp CHANGED
@@ -9,35 +9,35 @@ require 'osp'
9
9
 
10
10
 
11
11
  @options = {
12
- :database_path => Pathname.new('.osp').expand_path(Dir.home),
13
- :database_lock_path => Pathname.new('.osp~lock').expand_path(Dir.home),
12
+ :database_path => Pathname.new('.osp').expand_path(Dir.home),
13
+ :database_lock_path => Pathname.new('.osp~lock').expand_path(Dir.home),
14
14
  }
15
15
  opts = OptionParser.new do |o|
16
- o.banner = 'Usage: osp [options]'
17
- o.separator('')
18
-
19
- o.on('-d', '--database <path>', 'Path to the database file.') do |path|
20
- @options[:database_path] = Pathname.new(path).expand_path
21
- @options[:database_lock_path] = Pathname.new("#{@options[:database_path]}~lock").expand_path
22
- end
23
-
24
- o.on_tail('-V', '--version', 'Show version.') do
25
- puts "osp #{TheFox::OSP::VERSION} (#{TheFox::OSP::DATE})"
26
- puts TheFox::OSP::HOMEPAGE
27
- exit
28
- end
29
-
30
- o.on_tail('-h', '--help', 'Show this message.') do
31
- puts o
32
- puts
33
- exit 3
34
- end
16
+ o.banner = 'Usage: osp [options]'
17
+ o.separator('')
18
+
19
+ o.on('-d', '--database <path>', 'Path to the database file.') do |path|
20
+ @options[:database_path] = Pathname.new(path).expand_path
21
+ @options[:database_lock_path] = Pathname.new("#{@options[:database_path]}~lock").expand_path
22
+ end
23
+
24
+ o.on_tail('-V', '--version', 'Show version.') do
25
+ puts "osp #{TheFox::OSP::VERSION} (#{TheFox::OSP::DATE})"
26
+ puts TheFox::OSP::HOMEPAGE
27
+ exit
28
+ end
29
+
30
+ o.on_tail('-h', '--help', 'Show this message.') do
31
+ puts o
32
+ puts
33
+ exit 3
34
+ end
35
35
  end
36
36
  opts.parse(ARGV)
37
37
 
38
38
 
39
39
  if !STDIN.tty?
40
- raise "STDIN isn't a TTY."
40
+ raise "STDIN isn't a TTY."
41
41
  end
42
42
 
43
43
  @cli = HighLine.new
@@ -47,12 +47,12 @@ puts TheFox::OSP::HOMEPAGE
47
47
  puts
48
48
 
49
49
  if @options[:database_lock_path].exist?
50
- puts "WARNING: lock file exist: '#{@options[:database_lock_path]}'"
51
- overwrite = @cli.ask('Overwrite? '){ |q| q.default = 'n' }.downcase
52
- if overwrite == 'n'
53
- puts 'Quit.'
54
- exit
55
- end
50
+ puts "WARNING: lock file exist: '#{@options[:database_lock_path]}'"
51
+ overwrite = @cli.ask('Overwrite? '){ |q| q.default = 'n' }.downcase
52
+ if overwrite == 'n'
53
+ puts 'Quit.'
54
+ exit
55
+ end
56
56
  end
57
57
  File.write(@options[:database_lock_path], 'tmp')
58
58
  @options[:database_lock_path].chmod(0600)
@@ -60,11 +60,19 @@ File.binwrite(@options[:database_lock_path], Process.pid)
60
60
 
61
61
  puts 'Master Login'
62
62
  if $DEBUG
63
- email = 'example@example.com'
64
- password = 'password'
63
+ email = 'example@example.com'
64
+ password1 = 'password'
65
+ password2 = 'password'
65
66
  else
66
- email = @cli.ask(' Email: ')
67
- password = @cli.ask('Password: '){ |q| q.echo = '*' }
67
+ email = @cli.ask(' Email: ')
68
+ password1 = @cli.ask('Password: '){ |q| q.echo = '*' }
69
+ password2 = @cli.ask(' Confirm: '){ |q| q.echo = '*' }
70
+
71
+ if password1 != password2
72
+ remove_lock
73
+ puts 'Passwords do not match.'
74
+ exit 1
75
+ end
68
76
  end
69
77
 
70
78
  Console.cursor_jump_to_column
@@ -74,335 +82,340 @@ puts ' Email: *****'
74
82
  puts 'Password: *****'
75
83
  puts
76
84
 
77
- @osp_class = TheFox::OSP::OSP
78
- @osp_class = TheFox::OSP::OSPDev if $DEBUG
85
+ if $DEBUG
86
+ @osp_class = TheFox::OSP::OSPDev
87
+ else
88
+ @osp_class = TheFox::OSP::OSP
89
+ end
79
90
 
80
- printf "Calculating base hash: #{@osp_class::HASHES_N} (#{@osp_class::HASHES_EXP}-bit) - please wait ..."
81
- @osp = @osp_class.new(email, password)
91
+ print 'Calculating base hash: %d (%d-bit) - please wait ...' % [
92
+ @osp_class::HASHES_N, @osp_class::HASHES_EXP,
93
+ ]
94
+ @osp = @osp_class.new(email, password1)
82
95
  @osp.key_derivation
83
96
  puts ' done'
84
97
 
85
98
  def database_load_step(step, msg)
86
- puts "#{step} #{msg}"
99
+ puts "#{step} #{msg}"
87
100
  end
88
101
 
89
102
  def database_write_step(step, msg)
90
- puts "#{step} #{msg}"
103
+ puts "#{step} #{msg}"
91
104
  end
92
105
 
93
106
  def remove_lock
94
- if @options[:database_lock_path].exist?
95
- @options[:database_lock_path].unlink
96
- end
107
+ if @options[:database_lock_path].exist?
108
+ @options[:database_lock_path].unlink
109
+ end
97
110
  end
98
111
 
99
112
  @database = TheFox::OSP::Database.new(@options[:database_path], @osp)
100
113
  @database.load_callback_method = self.method('database_load_step')
101
114
  @database.write_callback_method = self.method('database_write_step')
102
115
  begin
103
- @database.load
116
+ @database.load
104
117
  rescue Exception => e
105
- puts
106
- puts "FATAL ERROR: couldn't open database:"
107
- puts "#{e}"
108
- remove_lock
109
- exit 1
118
+ puts
119
+ puts "FATAL ERROR: couldn't open database:"
120
+ puts "#{e}"
121
+ remove_lock
122
+ exit 1
110
123
  end
111
124
 
112
125
  def password_callback_method(step, pw)
113
- printf '.'
126
+ printf '.'
114
127
  end
115
128
  @osp.password_callback_method = self.method('password_callback_method')
116
129
 
117
130
  def host_show(host, regenerate_password = false)
118
- if !host.has_generated_password? || regenerate_password
119
- print 'Generate password '
120
- host.generate_password(regenerate_password)
121
- puts ' done'
122
- end
123
-
124
- puts " Hostname: #{host.name}"
125
- puts "Generation: #{host.generation}"
126
- puts " Length: #{host.length}"
127
- puts " Symbols: #{host.symbols}"
128
- puts " Hashes: #{host.hashes}"
129
- puts " Password: #{host.password}"
131
+ if !host.has_generated_password? || regenerate_password
132
+ print 'Generate password '
133
+ host.generate_password(regenerate_password)
134
+ puts ' done'
135
+ end
136
+
137
+ puts " Hostname: #{host.name}"
138
+ puts "Generation: #{host.generation}"
139
+ puts " Length: #{host.length}"
140
+ puts " Symbols: #{host.symbols}"
141
+ puts " Hashes: #{host.hashes}"
142
+ puts " Password: #{host.password}"
130
143
  end
131
144
 
132
145
  def host_edit(host = nil)
133
- host = TheFox::OSP::Host.new(@osp) if host.nil?
134
-
135
- puts
136
- puts 'New Host'
137
- puts
138
-
139
- tmp = @cli.ask(' Hostname: ' + (host.name.nil? ? '[]' : "[#{host.name}]") + ' ').strip.to_s
140
- host.name = tmp if tmp != ''
141
- host.name = nil if host.name == ''
142
- if host.name.nil?
143
- return nil
144
- end
145
-
146
- host.generation = @cli.ask("Generation: [#{host.generation}] ", Integer){ |q|
147
- q.default = host.generation
148
- q.in = 1..99 }.to_i
149
-
150
- host.length = @cli.ask(" Length: [#{host.length}] ", Integer){ |q|
151
- q.default = host.length
152
- q.in = @osp_class::PASSWORD_MIN_SIZE..@osp_class::PASSWORD_MAX_SIZE }.to_i
153
-
154
- host.symbols = @cli.ask(" Symbols: [#{host.symbols}] ", Integer){ |q|
155
- q.default = host.symbols
156
- q.in = 0..3 }.to_i
157
-
158
- host.updated_at = DateTime.now
159
-
160
- host
146
+ host = TheFox::OSP::Host.new(@osp) if host.nil?
147
+
148
+ puts
149
+ puts 'New Host'
150
+ puts
151
+
152
+ tmp = @cli.ask(' Hostname: ' + (host.name.nil? ? '[]' : "[#{host.name}]") + ' ').strip.to_s
153
+ host.name = tmp if tmp != ''
154
+ host.name = nil if host.name == ''
155
+ if host.name.nil?
156
+ return nil
157
+ end
158
+
159
+ host.generation = @cli.ask("Generation: [#{host.generation}] ", Integer){ |q|
160
+ q.default = host.generation
161
+ q.in = 1..99 }.to_i
162
+
163
+ host.length = @cli.ask(" Length: [#{host.length}] ", Integer){ |q|
164
+ q.default = host.length
165
+ q.in = @osp_class::PASSWORD_MIN_SIZE..@osp_class::PASSWORD_MAX_SIZE }.to_i
166
+
167
+ host.symbols = @cli.ask(" Symbols: [#{host.symbols}] ", Integer){ |q|
168
+ q.default = host.symbols
169
+ q.in = 0..3 }.to_i
170
+
171
+ host.updated_at = DateTime.now
172
+
173
+ host
161
174
  end
162
175
 
163
176
  actions = ['begin']
164
177
  while true
165
- case actions.pop
166
- when 'begin'
167
- puts
168
- puts "Type '?' for help."
169
- when 'n'
170
- puts
171
- host = host_edit
172
- if !host.nil?
173
- host_show(host)
174
- puts
175
-
176
- add = @cli.ask('Add to database? [yN] ', String){ |q| q.character = true }.strip.downcase
177
- add = 'n' if add == ''
178
- puts "Answer: '#{add}'"
179
-
180
- if add == 'y'
181
- @database.add_host(host)
182
- end
183
- else
184
- puts "ERROR: hostname can't be nothing."
185
- end
186
- when 'l'
187
- puts
188
- puts 'List Hosts'
189
- puts
190
-
191
- hosts_n = @database.hosts.count
192
- format = '%' + hosts_n.to_s.length.to_s + 'd'
193
-
194
- n = 0
195
- @database.hosts.values.each do |host|
196
- n += 1
197
- printf "#{format}. %s\n", n, host.name
198
- end
199
- when 's'
200
- found_hosts = []
201
-
202
- search_actions = ['begin']
203
- while true
204
- case search_actions.pop
205
- when 'begin'
206
- puts
207
- puts 'Entering search submenu.'
208
-
209
- search_actions << 's'
210
- when 's'
211
- puts
212
-
213
- host_name = @cli.ask('Search host names: ', String).strip.downcase
214
- re = Regexp.new(host_name, Regexp::IGNORECASE)
215
-
216
- found_hosts = @database.hosts.select{ |name, host| re.match(host.name) }
217
-
218
- search_actions << 'l'
219
- when 'l'
220
- puts
221
- if found_hosts.count > 0
222
- puts "#{found_hosts.count} host(s) found:"
223
-
224
- hosts_n = found_hosts.count
225
- format = '%' + hosts_n.to_s.length.to_s + 'd'
226
- n = 0
227
- found_hosts.each do |name, host|
228
- n += 1
229
- printf "#{format}. %s\n", n, host.name
230
- end
231
- else
232
- puts 'No hosts found.'
233
- end
234
- when 'i'
235
- puts
236
- if found_hosts.count > 0
237
- index = 1
238
- if found_hosts.count > 1
239
- index = @cli.ask('Show host, select by number: ', Integer){ |q|
240
- q.character = true if found_hosts.count < 10 }.to_i
241
- puts
242
- end
243
-
244
- if index >= 1 && index <= found_hosts.count
245
- puts "Host ##{index} selected:"
246
- index -= 1
247
- selected_host = nil
248
- begin
249
- selected_host = found_hosts.values[index]
250
- rescue Exception => e
251
- puts "ERROR: #{e}"
252
- end
253
-
254
- if !selected_host.nil?
255
- host_show(selected_host)
256
- end
257
- else
258
- puts 'Abort.'
259
- end
260
- else
261
- puts 'No hosts found.'
262
- end
263
- when 'e'
264
- puts
265
- if found_hosts.count > 0
266
- index = 1
267
- if found_hosts.count > 1
268
- index = @cli.ask('Edit, select host by number: ', Integer){ |q|
269
- q.character = true if found_hosts.count < 10 }.to_i
270
- puts
271
- end
272
-
273
- if index >= 1 && index <= found_hosts.count
274
- puts "Host ##{index} selected:"
275
- index -= 1
276
- selected_host = nil
277
- begin
278
- selected_host = found_hosts.values[index].clone
279
- rescue Exception => e
280
- puts "ERROR: #{e}"
281
- end
282
-
283
- if !selected_host.nil?
284
- host_edit(selected_host)
285
- host_show(selected_host, true)
286
-
287
- @database.add_host(selected_host)
288
- end
289
- else
290
- puts 'Abort.'
291
- end
292
- else
293
- puts 'No hosts found.'
294
- end
295
- when 'd'
296
- puts
297
- if found_hosts.count > 0
298
- index = 1
299
- if found_hosts.count > 1
300
- index = @cli.ask('Delete, select host by number: ', Integer){ |q|
301
- q.character = true if found_hosts.count < 10 }.to_i
302
- puts
303
- end
304
-
305
- if index >= 1 && index <= found_hosts.count
306
- puts "Delete host ##{index}."
307
- index -= 1
308
- selected_host = nil
309
- begin
310
- selected_host = found_hosts.values[index]
311
- rescue Exception => e
312
- puts "ERROR: #{e}"
313
- end
314
-
315
- if !selected_host.nil?
316
- if @database.remove_host(selected_host)
317
- found_hosts.delete(selected_host.name)
318
- puts "Host '#{selected_host.name}' deleted."
319
- else
320
- puts "ERROR: Can't delete host '#{selected_host.name}'."
321
- end
322
- end
323
- else
324
- puts 'Abort.'
325
- end
326
- else
327
- puts 'No hosts found.'
328
- end
329
- when 'c'
330
- system('clear')
331
- when 'q'
332
- break
333
- when '?'
334
- puts
335
- puts 'Search-menu help:'
336
- puts "\ts - New search."
337
- puts "\tl - List found hosts."
338
- puts "\ti - Print informations about a host found by a search."
339
- puts "\te - Edit a host found by search."
340
- puts "\td - Delete a host found by search."
341
- puts "\tc - Clear screen."
342
- puts "\tq - Quit search."
343
- puts "\t? - Print help."
344
- else
345
- puts "WARNING: invalid input. Type '?' for help."
346
- end
347
-
348
- next if search_actions.count > 0
349
-
350
- puts
351
- search_actions << @cli.ask('[sliedcq?] >> '){ |q| q.character = true }.downcase
352
- end
353
- when 'c'
354
- system('clear')
355
- when 'x'
356
- actions << 'q'
357
- actions << 'w'
358
- when 'w'
359
- @database.write
360
- when 'q'
361
- if @database.has_changed
362
- puts
363
- puts 'You have unsaved database changes.'
364
-
365
- save = @cli.ask('Would you like to save the database? [Yn] ', String){ |q| q.character = true }.strip.downcase
366
- save = 'y' if save == ''
367
- puts "Answer: '#{save}'"
368
-
369
- if save == 'y'
370
- actions << 'x'
371
- else
372
- really = @cli.ask('Really? [yN] ', String){ |q| q.character = true }.strip.downcase
373
- really = 'n' if really == ''
374
- puts "Answer: '#{really}'"
375
-
376
- if really == 'y'
377
- @database.has_changed = false
378
- actions << 'q'
379
- else
380
- puts 'Qutting aborted.'
381
- end
382
- end
383
- else
384
- puts 'Quit.'
385
- break
386
- end
387
- when '?'
388
- puts
389
- puts 'Main-menu help:'
390
- puts "\tn - New password."
391
- puts "\tl - List hosts from database."
392
- puts "\ts - Search submenu."
393
- puts "\tc - Clear screen."
394
- puts "\tx - Same as wq."
395
- puts "\tw - Write database to file."
396
- puts "\tq - Quit."
397
- puts "\t? - Print help."
398
- else
399
- puts "WARNING: invalid input. Type '?' for help."
400
- end
401
-
402
- next if actions.count > 0
403
-
404
- puts
405
- actions << @cli.ask('[nlscxwq?] > '){ |q| q.character = true }.downcase
178
+ case actions.pop
179
+ when 'begin'
180
+ puts
181
+ puts "Type '?' for help."
182
+ when 'n'
183
+ puts
184
+ host = host_edit
185
+ if !host.nil?
186
+ host_show(host)
187
+ puts
188
+
189
+ add = @cli.ask('Add to database? [yN] ', String){ |q| q.character = true }.strip.downcase
190
+ add = 'n' if add == ''
191
+ puts "Answer: '#{add}'"
192
+
193
+ if add == 'y'
194
+ @database.add_host(host)
195
+ end
196
+ else
197
+ puts "ERROR: hostname can't be nothing."
198
+ end
199
+ when 'l'
200
+ puts
201
+ puts 'List Hosts'
202
+ puts
203
+
204
+ hosts_n = @database.hosts.count
205
+ format = '%' + hosts_n.to_s.length.to_s + 'd'
206
+
207
+ n = 0
208
+ @database.hosts.values.each do |h|
209
+ n += 1
210
+ printf "#{format}. %s\n", n, h.name
211
+ end
212
+ when 's'
213
+ found_hosts = []
214
+
215
+ search_actions = ['begin']
216
+ while true
217
+ case search_actions.pop
218
+ when 'begin'
219
+ puts
220
+ puts 'Entering search submenu.'
221
+
222
+ search_actions << 's'
223
+ when 's'
224
+ puts
225
+
226
+ host_name = @cli.ask('Search host names: ', String).strip.downcase
227
+ re = Regexp.new(host_name, Regexp::IGNORECASE)
228
+
229
+ found_hosts = @database.hosts.select{ |name, h| re.match(h.name) }
230
+
231
+ search_actions << 'l'
232
+ when 'l'
233
+ puts
234
+ if found_hosts.count > 0
235
+ puts "#{found_hosts.count} host(s) found:"
236
+
237
+ hosts_n = found_hosts.count
238
+ format = '%' + hosts_n.to_s.length.to_s + 'd'
239
+ n = 0
240
+ found_hosts.each do |name, h|
241
+ n += 1
242
+ printf "#{format}. %s\n", n, h.name
243
+ end
244
+ else
245
+ puts 'No hosts found.'
246
+ end
247
+ when 'i'
248
+ puts
249
+ if found_hosts.count > 0
250
+ index = 1
251
+ if found_hosts.count > 1
252
+ index = @cli.ask('Show host, select by number: ', Integer){ |q|
253
+ q.character = true if found_hosts.count < 10 }.to_i
254
+ puts
255
+ end
256
+
257
+ if index >= 1 && index <= found_hosts.count
258
+ puts "Host ##{index} selected:"
259
+ index -= 1
260
+ selected_host = nil
261
+ begin
262
+ selected_host = found_hosts.values[index]
263
+ rescue Exception => e
264
+ puts "ERROR: #{e}"
265
+ end
266
+
267
+ if !selected_host.nil?
268
+ host_show(selected_host)
269
+ end
270
+ else
271
+ puts 'Abort.'
272
+ end
273
+ else
274
+ puts 'No hosts found.'
275
+ end
276
+ when 'e'
277
+ puts
278
+ if found_hosts.count > 0
279
+ index = 1
280
+ if found_hosts.count > 1
281
+ index = @cli.ask('Edit, select host by number: ', Integer){ |q|
282
+ q.character = true if found_hosts.count < 10 }.to_i
283
+ puts
284
+ end
285
+
286
+ if index >= 1 && index <= found_hosts.count
287
+ puts "Host ##{index} selected:"
288
+ index -= 1
289
+ selected_host = nil
290
+ begin
291
+ selected_host = found_hosts.values[index].clone
292
+ rescue Exception => e
293
+ puts "ERROR: #{e}"
294
+ end
295
+
296
+ if !selected_host.nil?
297
+ host_edit(selected_host)
298
+ host_show(selected_host, true)
299
+
300
+ @database.add_host(selected_host)
301
+ end
302
+ else
303
+ puts 'Abort.'
304
+ end
305
+ else
306
+ puts 'No hosts found.'
307
+ end
308
+ when 'd'
309
+ puts
310
+ if found_hosts.count > 0
311
+ index = 1
312
+ if found_hosts.count > 1
313
+ index = @cli.ask('Delete, select host by number: ', Integer){ |q|
314
+ q.character = true if found_hosts.count < 10 }.to_i
315
+ puts
316
+ end
317
+
318
+ if index >= 1 && index <= found_hosts.count
319
+ puts "Delete host ##{index}."
320
+ index -= 1
321
+ selected_host = nil
322
+ begin
323
+ selected_host = found_hosts.values[index]
324
+ rescue Exception => e
325
+ puts "ERROR: #{e}"
326
+ end
327
+
328
+ if !selected_host.nil?
329
+ if @database.remove_host(selected_host)
330
+ found_hosts.delete(selected_host.name)
331
+ puts "Host '#{selected_host.name}' deleted."
332
+ else
333
+ puts "ERROR: Can't delete host '#{selected_host.name}'."
334
+ end
335
+ end
336
+ else
337
+ puts 'Abort.'
338
+ end
339
+ else
340
+ puts 'No hosts found.'
341
+ end
342
+ when 'c'
343
+ system('clear')
344
+ when 'q'
345
+ break
346
+ when '?'
347
+ puts
348
+ puts 'Search-menu help:'
349
+ puts "\ts - New search."
350
+ puts "\tl - List found hosts."
351
+ puts "\ti - Print informations about a host found by a search."
352
+ puts "\te - Edit a host found by search."
353
+ puts "\td - Delete a host found by search."
354
+ puts "\tc - Clear screen."
355
+ puts "\tq - Quit search."
356
+ puts "\t? - Print help."
357
+ else
358
+ puts "WARNING: invalid input. Type '?' for help."
359
+ end
360
+
361
+ next if search_actions.count > 0
362
+
363
+ puts
364
+ search_actions << @cli.ask('[sliedcq?] >> '){ |q| q.character = true }.downcase
365
+ end
366
+ when 'c'
367
+ system('clear')
368
+ when 'x'
369
+ actions << 'q'
370
+ actions << 'w'
371
+ when 'w'
372
+ @database.write
373
+ when 'q'
374
+ if @database.has_changed
375
+ puts
376
+ puts 'You have unsaved database changes.'
377
+
378
+ save = @cli.ask('Would you like to save the database? [Yn] ', String){ |q| q.character = true }.strip.downcase
379
+ save = 'y' if save == ''
380
+ puts "Answer: '#{save}'"
381
+
382
+ if save == 'y'
383
+ actions << 'x'
384
+ else
385
+ really = @cli.ask('Really? [yN] ', String){ |q| q.character = true }.strip.downcase
386
+ really = 'n' if really == ''
387
+ puts "Answer: '#{really}'"
388
+
389
+ if really == 'y'
390
+ @database.has_changed = false
391
+ actions << 'q'
392
+ else
393
+ puts 'Qutting aborted.'
394
+ end
395
+ end
396
+ else
397
+ puts 'Quit.'
398
+ break
399
+ end
400
+ when '?'
401
+ puts
402
+ puts 'Main-menu help:'
403
+ puts "\tn - New password."
404
+ puts "\tl - List hosts from database."
405
+ puts "\ts - Search submenu."
406
+ puts "\tc - Clear screen."
407
+ puts "\tx - Same as wq."
408
+ puts "\tw - Write database to file."
409
+ puts "\tq - Quit."
410
+ puts "\t? - Print help."
411
+ else
412
+ puts "WARNING: invalid input. Type '?' for help."
413
+ end
414
+
415
+ next if actions.count > 0
416
+
417
+ puts
418
+ actions << @cli.ask('[nlscxwq?] > '){ |q| q.character = true }.downcase
406
419
  end
407
420
 
408
421
  remove_lock