hostor 1.0.1 → 2.0.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.
Files changed (2) hide show
  1. data/bin/hostor +98 -24
  2. metadata +26 -28
data/bin/hostor CHANGED
@@ -1,8 +1,31 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
2
4
  require 'rubygems'
3
5
  require 'optparse'
4
6
  require 'etc'
5
- require 'ftools'
7
+ require "iconv"
8
+
9
+ host_os = RbConfig::CONFIG['host_os']
10
+ if host_os =~ /mswin|msys|mingw32/
11
+ alias _puts puts
12
+ def puts string
13
+ _puts Iconv.iconv("GB2312","UTF-8", string)
14
+ end
15
+ end
16
+
17
+ if RUBY_VERSION =~ /1\.8\../
18
+ require 'ftools'
19
+ def copy(src, dest)
20
+ File.copy(src, dest)
21
+ end
22
+ else
23
+ require 'fileutils'
24
+ def copy(src, dest)
25
+ FileUtils.cp(src, dest)
26
+ end
27
+ end
28
+
6
29
  require 'tempfile'
7
30
  require 'rbconfig'
8
31
 
@@ -54,6 +77,8 @@ module Hostor
54
77
  os.eql? :linux
55
78
  end
56
79
 
80
+
81
+
57
82
  #todo: 根据不同平台获取当前目录
58
83
  def hostor_dir
59
84
  user = Etc.getlogin
@@ -70,16 +95,14 @@ module Hostor
70
95
 
71
96
  def get_host_name(path)
72
97
  unknown = "未定名称的HOST文件"
73
- ln = nil
74
- File.open(path, 'r').each_line do |line|
75
- ln = line
76
- break
77
- end
78
- return unknown if ln.nil?
98
+ ln = File.open(path){|f| f.read}
99
+ return unknown if ln.empty?
79
100
 
80
101
  if (ln =~ /#==\[.*?\]==/).eql? 0
81
- m = ln.match /#==\[(.*)?\.host\]==\B/
82
- m[1] || unknown
102
+ m = ln.match /#==\[(.*)?\]==\B/
103
+ m1 = m[1].to_s
104
+ m1.gsub!(/\.host/, "") if m1
105
+ m1 || unknown
83
106
  else
84
107
  unknown
85
108
  end
@@ -92,7 +115,7 @@ module Hostor
92
115
  Dir.mkdir(hostor_dir)
93
116
  puts "创建hostor配置目录#{hostor_dir}"
94
117
 
95
- File.copy(sys_host_file, default_host)
118
+ copy(sys_host_file, default_host)
96
119
  puts "复制#{sys_host_file}到#{default_host}"
97
120
 
98
121
  File.prepend(default_host, "#==[default.host]==\n")
@@ -111,8 +134,39 @@ module Hostor
111
134
  raise "名为#{name}.host的文件不存在"
112
135
  end
113
136
 
137
+ def common_host
138
+ p1 = "#{hostor_dir}/common"
139
+ return p1 if File.exists? p1
140
+ p2 = "#{hostor_dir}/common.host"
141
+ return p2 if File.exists? p2
142
+ return nil
143
+ end
144
+
114
145
  def switch(file)
115
- File.copy(get_path_by_name(file), sys_host_file)
146
+ if file =~ /\+/
147
+ pathes = file.split('+').collect {|f| get_path_by_name(f) }
148
+ s = File.open(sys_host_file, 'w')
149
+ s.write("#==[" + file +"]==#\n")
150
+ pathes.each {|p| s.write(File.read(p)) }
151
+ s.close
152
+ return
153
+ end
154
+
155
+ path = get_path_by_name(file)
156
+ unless file == 'common'
157
+ comm = common_host()
158
+ if comm
159
+ s = File.open(sys_host_file, 'w')
160
+ head = File.open(path){|f| f.readline }
161
+ s.write(head)
162
+ s.write(File.read(comm))
163
+ s.write(File.read(path))
164
+ s.close
165
+ return
166
+ end
167
+ end
168
+
169
+ copy(path, sys_host_file)
116
170
  end
117
171
 
118
172
  def current
@@ -155,7 +209,7 @@ module Hostor
155
209
  if File.exists? dest
156
210
  raise "已经添加了名为#{basename}文件,请重命名后添加或者使用#$0 --remove #{basename} 删除已有文件"
157
211
  else
158
- File.copy(file_bak, dest)
212
+ copy(file_bak, dest)
159
213
  File.prepend(dest, "#==[#{basename}]==\n")
160
214
  end
161
215
  else
@@ -175,17 +229,21 @@ module Hostor
175
229
  init()
176
230
  end
177
231
 
178
- opts.on('-a', '--add <file>', "将指定文件加入hostor管理") do |file|
232
+ opts.on('--empty', "清空host") do |file|
233
+ s = File.open(sys_host_file, 'w')
234
+ #s.write("#==[empty.host]==\n")
235
+ s.close
236
+ end
237
+
238
+ opts.on('--add <hostname>', "将指定文件加入hostor管理") do |file|
179
239
  add(file)
180
240
  end
181
241
 
182
- opts.on('-e', '--edit [file]', "编辑指定的HOST文件") do |file|
242
+ opts.on('-e', '--edit [hostname]', "编辑指定的HOST文件") do |file|
183
243
  f = file || current
184
244
  dest = get_path_by_name(f)
185
- if is_mac
186
- `open -a macvim #{dest}`
187
- elsif is_linux
188
- `xterm -e vim #{dest}`
245
+ if is_mac or is_linux
246
+ system(ENV["EDITOR"] || "vim",dest)
189
247
  elsif is_windows
190
248
  `notepad #{dest}`
191
249
  end
@@ -194,19 +252,19 @@ module Hostor
194
252
  end
195
253
  end
196
254
 
197
- opts.on('-r', '--remove <file>', "从hostor文件夹移除指定的HOST文件") do |file|
255
+ opts.on('--remove <hostname>', "从hostor文件夹移除指定的HOST文件") do |file|
198
256
  remove(file)
199
257
  end
200
258
 
201
259
  #适用于 -a -d
202
- #opts.on('-t', '--target <file>', "指定操作的HOST文件") do |name|
260
+ #opts.on('-t', '--target <hostname>', "指定操作的HOST文件") do |name|
203
261
  #end
204
262
 
205
263
  opts.on('-l', '--list', "列举当前用户的所有HOST文件") do
206
264
  list().each{|l| puts l}
207
265
  end
208
266
 
209
- opts.on('-v', '--view [file]', "查看指定的HOST文件") do |name|
267
+ opts.on('-v', '--view [hostname]', "查看指定的HOST文件") do |name|
210
268
  view(name)
211
269
  end
212
270
 
@@ -214,13 +272,31 @@ module Hostor
214
272
  puts current()
215
273
  end
216
274
 
217
- opts.on('-s', '--switch <file>', "将指定HOST文件设为系统当前HOST文件") do |file|
275
+ opts.on('-s', '--switch <hostname>', "将指定HOST文件设为系统当前HOST文件") do |file|
218
276
  switch(file)
219
277
  end
278
+
279
+ opts.on('-d', '--delete <host_or_ip> [hostname]', "删除指定设置") do |host_or_ip|
280
+ path = get_path_by_name(args[0] || current)
281
+ #puts "ruby -p -e \"gsub(/^.*#{host_or_ip.gsub('.','\.')}.*?$/,'')\" #{path}"
282
+ `ruby -pi.bak -e "gsub(/^.*#{host_or_ip.gsub('.','\.')}.*?\n/,'')" #{path}`
283
+ end
284
+
285
+ opts.on('-r', '--reload', "重新载入设置") do
286
+ switch(current)
287
+ end
288
+
289
+ opts.on('-a', '--append <setting> [hostname]', "追加设置") do |setting|
290
+ path = get_path_by_name(args[0] || current)
291
+ File.open(path, 'a') do |f|
292
+ f.puts setting
293
+ end
294
+ end
220
295
  end
221
296
 
222
297
  begin
223
298
  if args.length.eql? 0
299
+ #puts current
224
300
  view(nil)
225
301
  elsif args.length.eql? 1 and args[0] =~ /^[^-]/
226
302
  switch(args[0])
@@ -237,5 +313,3 @@ end
237
313
 
238
314
  include Hostor
239
315
  main ARGV
240
-
241
-
metadata CHANGED
@@ -3,35 +3,35 @@ name: hostor
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
- - 1
7
- - 0
8
- - 1
9
- version: 1.0.1
6
+ - 2
7
+ - 0
8
+ - 0
9
+ version: 2.0.0
10
10
  platform: ruby
11
11
  authors:
12
- - Zhong Xingdou
12
+ - Zhong Xingdou
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-24 00:00:00 +08:00
17
+ date: 2011-07-21 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: " system host file manager\n"
21
+ description: " host management tools\n host\xE7\xAE\xA1\xE7\x90\x86\xE5\xB7\xA5\xE5\x85\xB7\n"
22
22
  email: zhongxingdou@gmail.com
23
23
  executables:
24
- - hostor
25
- - hostor_gui
24
+ - hostor
25
+ - hostor_gui
26
26
  extensions: []
27
27
 
28
28
  extra_rdoc_files: []
29
29
 
30
30
  files:
31
- - bin/hostor_gui
32
- - bin/hostor
33
- - bin/hostor.png
34
- - bin/hostor_gui.bat
31
+ - bin/hostor_gui
32
+ - bin/hostor
33
+ - bin/hostor.png
34
+ - bin/hostor_gui.bat
35
35
  has_rdoc: true
36
36
  homepage:
37
37
  licenses: []
@@ -40,29 +40,27 @@ post_install_message:
40
40
  rdoc_options: []
41
41
 
42
42
  require_paths:
43
- - lib
43
+ - lib
44
44
  required_ruby_version: !ruby/object:Gem::Requirement
45
- none: false
46
45
  requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- segments:
50
- - 0
51
- version: "0"
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
52
51
  required_rubygems_version: !ruby/object:Gem::Requirement
53
- none: false
54
52
  requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- segments:
58
- - 0
59
- version: "0"
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
60
58
  requirements: []
61
59
 
62
60
  rubyforge_project:
63
- rubygems_version: 1.3.7
61
+ rubygems_version: 1.3.6
64
62
  signing_key:
65
63
  specification_version: 3
66
- summary: system host file manager
64
+ summary: host management tools
67
65
  test_files: []
68
66