Module: Dklet::Util
- Defined in:
- lib/dklet/util.rb
Class Method Summary collapse
-
.gen_password(len = 10) ⇒ Object
secure.
- .host_ip ⇒ Object
- .host_os ⇒ Object
- .human_timestamp(t = Time.now) ⇒ Object
- .on_mac? ⇒ Boolean
- .single_line?(cmds) ⇒ Boolean
- .tmpfile_for(str, prefix: 'dklet-tmp') ⇒ Object
Class Method Details
.gen_password(len = 10) ⇒ Object
secure
43 44 45 |
# File 'lib/dklet/util.rb', line 43 def gen_password(len = 10) SecureRandom.alphanumeric(len) end |
.host_ip ⇒ Object
21 22 23 |
# File 'lib/dklet/util.rb', line 21 def host_ip Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address end |
.host_os ⇒ Object
25 26 27 |
# File 'lib/dklet/util.rb', line 25 def host_os ::OS end |
.human_timestamp(t = Time.now) ⇒ Object
9 10 11 |
# File 'lib/dklet/util.rb', line 9 def (t = Time.now) t.strftime("%Y%m%d%H%M%S") end |
.on_mac? ⇒ Boolean
29 30 31 |
# File 'lib/dklet/util.rb', line 29 def on_mac? host_os.mac? end |
.single_line?(cmds) ⇒ Boolean
33 34 35 36 37 38 39 40 |
# File 'lib/dklet/util.rb', line 33 def single_line?(cmds) return false if cmds.is_a? Array cmds = cmds.chomp return false if cmds =~ /\n/ return true if cmds =~ /^\s*(bash|sh)/ return false if cmds =~ /.+;/ true end |
.tmpfile_for(str, prefix: 'dklet-tmp') ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/dklet/util.rb', line 13 def tmpfile_for(str, prefix: 'dklet-tmp') file = Tempfile.new(prefix) file.write str file.close # save to disk # unlink清理问题:引用进程结束时自动删除?👍 file.path end |