kanrisuru 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (252) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/lib/kanrisuru/core/apt/commands/autoclean.rb +13 -0
  4. data/lib/kanrisuru/core/apt/commands/autoremove.rb +15 -0
  5. data/lib/kanrisuru/core/apt/commands/clean.rb +13 -0
  6. data/lib/kanrisuru/core/apt/commands/full_upgrade.rb +14 -0
  7. data/lib/kanrisuru/core/apt/commands/install.rb +22 -0
  8. data/lib/kanrisuru/core/apt/commands/list.rb +21 -0
  9. data/lib/kanrisuru/core/apt/commands/purge.rb +18 -0
  10. data/lib/kanrisuru/core/apt/commands/remove.rb +18 -0
  11. data/lib/kanrisuru/core/apt/commands/search.rb +18 -0
  12. data/lib/kanrisuru/core/apt/commands/show.rb +21 -0
  13. data/lib/kanrisuru/core/apt/commands/update.rb +14 -0
  14. data/lib/kanrisuru/core/apt/commands/upgrade.rb +14 -0
  15. data/lib/kanrisuru/core/apt/commands.rb +14 -0
  16. data/lib/kanrisuru/core/apt/parser.rb +6 -0
  17. data/lib/kanrisuru/core/apt/parsers/base.rb +51 -0
  18. data/lib/kanrisuru/core/apt/parsers/list.rb +28 -0
  19. data/lib/kanrisuru/core/apt/parsers/search.rb +32 -0
  20. data/lib/kanrisuru/core/apt/parsers/show.rb +90 -0
  21. data/lib/kanrisuru/core/apt/types.rb +36 -0
  22. data/lib/kanrisuru/core/apt.rb +4 -284
  23. data/lib/kanrisuru/core/archive/commands/tar.rb +126 -0
  24. data/lib/kanrisuru/core/archive/commands.rb +3 -0
  25. data/lib/kanrisuru/core/archive/types.rb +9 -0
  26. data/lib/kanrisuru/core/archive.rb +3 -121
  27. data/lib/kanrisuru/core/disk/commands/blkid.rb +35 -0
  28. data/lib/kanrisuru/core/disk/commands/df.rb +26 -0
  29. data/lib/kanrisuru/core/disk/commands/du.rb +24 -0
  30. data/lib/kanrisuru/core/disk/commands/lsblk.rb +54 -0
  31. data/lib/kanrisuru/core/disk/commands.rb +6 -0
  32. data/lib/kanrisuru/core/disk/parser.rb +6 -0
  33. data/lib/kanrisuru/core/disk/parsers/blkid.rb +72 -0
  34. data/lib/kanrisuru/core/disk/parsers/df.rb +33 -0
  35. data/lib/kanrisuru/core/disk/parsers/du.rb +25 -0
  36. data/lib/kanrisuru/core/disk/parsers/lsblk.rb +96 -0
  37. data/lib/kanrisuru/core/disk/types.rb +21 -0
  38. data/lib/kanrisuru/core/disk.rb +5 -290
  39. data/lib/kanrisuru/core/dmi/commands/dmi.rb +42 -0
  40. data/lib/kanrisuru/core/dmi/commands.rb +3 -0
  41. data/lib/kanrisuru/core/dmi/parser.rb +3 -0
  42. data/lib/kanrisuru/core/dmi/parsers/dmi.rb +149 -0
  43. data/lib/kanrisuru/core/dmi/types.rb +361 -0
  44. data/lib/kanrisuru/core/dmi.rb +4 -522
  45. data/lib/kanrisuru/core/file/commands/chmod.rb +29 -0
  46. data/lib/kanrisuru/core/file/commands/chown.rb +35 -0
  47. data/lib/kanrisuru/core/file/commands/copy.rb +56 -0
  48. data/lib/kanrisuru/core/file/commands/link.rb +25 -0
  49. data/lib/kanrisuru/core/file/commands/mkdir.rb +37 -0
  50. data/lib/kanrisuru/core/file/commands/move.rb +48 -0
  51. data/lib/kanrisuru/core/file/commands/rm.rb +38 -0
  52. data/lib/kanrisuru/core/file/commands/symlink.rb +46 -0
  53. data/lib/kanrisuru/core/file/commands/touch.rb +33 -0
  54. data/lib/kanrisuru/core/file/commands/unlink.rb +13 -0
  55. data/lib/kanrisuru/core/file/commands/wc.rb +16 -0
  56. data/lib/kanrisuru/core/file/commands.rb +29 -0
  57. data/lib/kanrisuru/core/file/parser.rb +3 -0
  58. data/lib/kanrisuru/core/file/parsers/wc.rb +16 -0
  59. data/lib/kanrisuru/core/file/types.rb +9 -0
  60. data/lib/kanrisuru/core/file.rb +4 -312
  61. data/lib/kanrisuru/core/find/commands/find.rb +97 -0
  62. data/lib/kanrisuru/core/find/commands.rb +3 -0
  63. data/lib/kanrisuru/core/find/constants.rb +9 -0
  64. data/lib/kanrisuru/core/find/parser.rb +3 -0
  65. data/lib/kanrisuru/core/find/parsers/find.rb +18 -0
  66. data/lib/kanrisuru/core/find/types.rb +9 -0
  67. data/lib/kanrisuru/core/find.rb +5 -97
  68. data/lib/kanrisuru/core/group/commands/create_group.rb +21 -0
  69. data/lib/kanrisuru/core/group/commands/delete_group.rb +16 -0
  70. data/lib/kanrisuru/core/group/commands/get_gid.rb +17 -0
  71. data/lib/kanrisuru/core/group/commands/get_group.rb +29 -0
  72. data/lib/kanrisuru/core/group/commands/is_group.rb +14 -0
  73. data/lib/kanrisuru/core/group/commands/update_group.rb +23 -0
  74. data/lib/kanrisuru/core/group/commands.rb +8 -0
  75. data/lib/kanrisuru/core/group/parser.rb +4 -0
  76. data/lib/kanrisuru/core/group/parsers/gid.rb +15 -0
  77. data/lib/kanrisuru/core/group/parsers/group.rb +15 -0
  78. data/lib/kanrisuru/core/group/types.rb +10 -0
  79. data/lib/kanrisuru/core/group.rb +4 -81
  80. data/lib/kanrisuru/core/ip/commands/address.rb +69 -0
  81. data/lib/kanrisuru/core/ip/commands/address_label.rb +38 -0
  82. data/lib/kanrisuru/core/ip/commands/link.rb +74 -0
  83. data/lib/kanrisuru/core/ip/commands/maddress.rb +33 -0
  84. data/lib/kanrisuru/core/ip/commands/neighbour.rb +55 -0
  85. data/lib/kanrisuru/core/ip/commands/route.rb +115 -0
  86. data/lib/kanrisuru/core/ip/commands/rule.rb +42 -0
  87. data/lib/kanrisuru/core/ip/commands.rb +24 -0
  88. data/lib/kanrisuru/core/ip/constants.rb +13 -0
  89. data/lib/kanrisuru/core/ip/parser.rb +10 -0
  90. data/lib/kanrisuru/core/ip/parsers/address.rb +104 -0
  91. data/lib/kanrisuru/core/ip/parsers/address_label.rb +27 -0
  92. data/lib/kanrisuru/core/ip/parsers/base.rb +166 -0
  93. data/lib/kanrisuru/core/ip/parsers/link.rb +86 -0
  94. data/lib/kanrisuru/core/ip/parsers/maddress.rb +85 -0
  95. data/lib/kanrisuru/core/ip/parsers/neighbour.rb +89 -0
  96. data/lib/kanrisuru/core/ip/parsers/route.rb +76 -0
  97. data/lib/kanrisuru/core/ip/parsers/rule.rb +60 -0
  98. data/lib/kanrisuru/core/ip/types.rb +46 -0
  99. data/lib/kanrisuru/core/ip.rb +5 -995
  100. data/lib/kanrisuru/core/mount/commands/mount.rb +65 -0
  101. data/lib/kanrisuru/core/mount/commands/umount.rb +35 -0
  102. data/lib/kanrisuru/core/mount/commands.rb +49 -0
  103. data/lib/kanrisuru/core/mount.rb +2 -125
  104. data/lib/kanrisuru/core/path/commands/ls.rb +26 -0
  105. data/lib/kanrisuru/core/path/commands/pwd.rb +16 -0
  106. data/lib/kanrisuru/core/path/commands/readlink.rb +22 -0
  107. data/lib/kanrisuru/core/path/commands/realpath.rb +18 -0
  108. data/lib/kanrisuru/core/path/commands/which.rb +19 -0
  109. data/lib/kanrisuru/core/path/commands/whoami.rb +16 -0
  110. data/lib/kanrisuru/core/path/commands.rb +8 -0
  111. data/lib/kanrisuru/core/path/parser.rb +4 -0
  112. data/lib/kanrisuru/core/path/parsers/ls.rb +53 -0
  113. data/lib/kanrisuru/core/path/parsers/which.rb +18 -0
  114. data/lib/kanrisuru/core/path/types.rb +12 -0
  115. data/lib/kanrisuru/core/path.rb +4 -121
  116. data/lib/kanrisuru/core/socket/commands/ss.rb +44 -0
  117. data/lib/kanrisuru/core/socket/commands.rb +3 -0
  118. data/lib/kanrisuru/core/socket/constants.rb +29 -0
  119. data/lib/kanrisuru/core/socket/parser.rb +3 -0
  120. data/lib/kanrisuru/core/socket/parsers/ss.rb +109 -0
  121. data/lib/kanrisuru/core/socket/types.rb +18 -0
  122. data/lib/kanrisuru/core/socket.rb +5 -164
  123. data/lib/kanrisuru/core/stat/commands/stat.rb +62 -0
  124. data/lib/kanrisuru/core/stat/commands.rb +3 -0
  125. data/lib/kanrisuru/core/stat/parser.rb +3 -0
  126. data/lib/kanrisuru/core/stat/parsers/stat.rb +34 -0
  127. data/lib/kanrisuru/core/stat/types.rb +14 -0
  128. data/lib/kanrisuru/core/stat.rb +4 -81
  129. data/lib/kanrisuru/core/stream/commands/cat.rb +25 -0
  130. data/lib/kanrisuru/core/stream/commands/echo.rb +20 -0
  131. data/lib/kanrisuru/core/stream/commands/head.rb +18 -0
  132. data/lib/kanrisuru/core/stream/commands/read_file_chunk.rb +21 -0
  133. data/lib/kanrisuru/core/stream/commands/sed.rb +23 -0
  134. data/lib/kanrisuru/core/stream/commands/tail.rb +18 -0
  135. data/lib/kanrisuru/core/stream/commands.rb +27 -0
  136. data/lib/kanrisuru/core/stream/parser.rb +4 -0
  137. data/lib/kanrisuru/core/stream/parsers/echo.rb +17 -0
  138. data/lib/kanrisuru/core/stream/parsers/sed.rb +17 -0
  139. data/lib/kanrisuru/core/stream.rb +3 -104
  140. data/lib/kanrisuru/core/system/commands/cpu_info.rb +32 -0
  141. data/lib/kanrisuru/core/system/commands/free.rb +25 -0
  142. data/lib/kanrisuru/core/system/commands/kernel_statistics.rb +21 -0
  143. data/lib/kanrisuru/core/system/commands/kill.rb +22 -0
  144. data/lib/kanrisuru/core/system/commands/last.rb +31 -0
  145. data/lib/kanrisuru/core/system/commands/load_average.rb +16 -0
  146. data/lib/kanrisuru/core/system/commands/load_env.rb +16 -0
  147. data/lib/kanrisuru/core/system/commands/lscpu.rb +17 -0
  148. data/lib/kanrisuru/core/system/commands/lsof.rb +16 -0
  149. data/lib/kanrisuru/core/system/commands/poweroff.rb +42 -0
  150. data/lib/kanrisuru/core/system/commands/ps.rb +35 -0
  151. data/lib/kanrisuru/core/system/commands/reboot.rb +42 -0
  152. data/lib/kanrisuru/core/system/commands/uptime.rb +18 -0
  153. data/lib/kanrisuru/core/system/commands/w.rb +24 -0
  154. data/lib/kanrisuru/core/system/commands.rb +34 -0
  155. data/lib/kanrisuru/core/system/parser.rb +11 -0
  156. data/lib/kanrisuru/core/system/parsers/kernel_statistics.rb +56 -0
  157. data/lib/kanrisuru/core/system/parsers/last.rb +64 -0
  158. data/lib/kanrisuru/core/system/parsers/load_average.rb +15 -0
  159. data/lib/kanrisuru/core/system/parsers/load_env.rb +24 -0
  160. data/lib/kanrisuru/core/system/parsers/lscpu.rb +90 -0
  161. data/lib/kanrisuru/core/system/parsers/lsof.rb +62 -0
  162. data/lib/kanrisuru/core/system/parsers/ps.rb +71 -0
  163. data/lib/kanrisuru/core/system/parsers/uptime.rb +38 -0
  164. data/lib/kanrisuru/core/system/parsers/w.rb +30 -0
  165. data/lib/kanrisuru/core/system/types.rb +108 -0
  166. data/lib/kanrisuru/core/system.rb +4 -687
  167. data/lib/kanrisuru/core/transfer/commands/download.rb +26 -0
  168. data/lib/kanrisuru/core/transfer/commands/upload.rb +23 -0
  169. data/lib/kanrisuru/core/transfer/commands/wget.rb +168 -0
  170. data/lib/kanrisuru/core/transfer/commands.rb +5 -0
  171. data/lib/kanrisuru/core/transfer/constants.rb +10 -0
  172. data/lib/kanrisuru/core/transfer.rb +3 -199
  173. data/lib/kanrisuru/core/user/commands/create_user.rb +57 -0
  174. data/lib/kanrisuru/core/user/commands/delete_user.rb +20 -0
  175. data/lib/kanrisuru/core/user/commands/get_uid.rb +15 -0
  176. data/lib/kanrisuru/core/user/commands/get_user.rb +48 -0
  177. data/lib/kanrisuru/core/user/commands/is_user.rb +14 -0
  178. data/lib/kanrisuru/core/user/commands/update_user.rb +63 -0
  179. data/lib/kanrisuru/core/user/commands.rb +8 -0
  180. data/lib/kanrisuru/core/user/parser.rb +4 -0
  181. data/lib/kanrisuru/core/user/parsers/getent.rb +17 -0
  182. data/lib/kanrisuru/core/user/parsers/groups.rb +17 -0
  183. data/lib/kanrisuru/core/user/types.rb +11 -0
  184. data/lib/kanrisuru/core/user.rb +4 -182
  185. data/lib/kanrisuru/core/yum/commands/autoremove.rb +14 -0
  186. data/lib/kanrisuru/core/yum/commands/clean.rb +23 -0
  187. data/lib/kanrisuru/core/yum/commands/erase.rb +20 -0
  188. data/lib/kanrisuru/core/yum/commands/info.rb +21 -0
  189. data/lib/kanrisuru/core/yum/commands/install.rb +18 -0
  190. data/lib/kanrisuru/core/yum/commands/list.rb +30 -0
  191. data/lib/kanrisuru/core/yum/commands/localinstall.rb +22 -0
  192. data/lib/kanrisuru/core/yum/commands/remove.rb +20 -0
  193. data/lib/kanrisuru/core/yum/commands/repolist.rb +20 -0
  194. data/lib/kanrisuru/core/yum/commands/search.rb +21 -0
  195. data/lib/kanrisuru/core/yum/commands/update.rb +14 -0
  196. data/lib/kanrisuru/core/yum/commands/upgrade.rb +14 -0
  197. data/lib/kanrisuru/core/yum/commands.rb +39 -0
  198. data/lib/kanrisuru/core/yum/parser.rb +7 -0
  199. data/lib/kanrisuru/core/yum/parsers/base.rb +31 -0
  200. data/lib/kanrisuru/core/yum/parsers/info.rb +69 -0
  201. data/lib/kanrisuru/core/yum/parsers/list.rb +24 -0
  202. data/lib/kanrisuru/core/yum/parsers/repolist.rb +60 -0
  203. data/lib/kanrisuru/core/yum/parsers/search.rb +30 -0
  204. data/lib/kanrisuru/core/yum/types.rb +41 -0
  205. data/lib/kanrisuru/core/yum.rb +4 -347
  206. data/lib/kanrisuru/core/zypper/commands/add_lock.rb +22 -0
  207. data/lib/kanrisuru/core/zypper/commands/add_repo.rb +38 -0
  208. data/lib/kanrisuru/core/zypper/commands/add_service.rb +26 -0
  209. data/lib/kanrisuru/core/zypper/commands/clean_cache.rb +22 -0
  210. data/lib/kanrisuru/core/zypper/commands/clean_locks.rb +17 -0
  211. data/lib/kanrisuru/core/zypper/commands/dist_upgrade.rb +25 -0
  212. data/lib/kanrisuru/core/zypper/commands/info.rb +25 -0
  213. data/lib/kanrisuru/core/zypper/commands/install.rb +39 -0
  214. data/lib/kanrisuru/core/zypper/commands/install_new_recommends.rb +23 -0
  215. data/lib/kanrisuru/core/zypper/commands/list_locks.rb +22 -0
  216. data/lib/kanrisuru/core/zypper/commands/list_patches.rb +34 -0
  217. data/lib/kanrisuru/core/zypper/commands/list_repos.rb +21 -0
  218. data/lib/kanrisuru/core/zypper/commands/list_services.rb +21 -0
  219. data/lib/kanrisuru/core/zypper/commands/list_updates.rb +30 -0
  220. data/lib/kanrisuru/core/zypper/commands/modify_repo.rb +48 -0
  221. data/lib/kanrisuru/core/zypper/commands/modify_service.rb +40 -0
  222. data/lib/kanrisuru/core/zypper/commands/patch.rb +40 -0
  223. data/lib/kanrisuru/core/zypper/commands/patch_check.rb +29 -0
  224. data/lib/kanrisuru/core/zypper/commands/purge_kernels.rb +18 -0
  225. data/lib/kanrisuru/core/zypper/commands/refresh_repos.rb +24 -0
  226. data/lib/kanrisuru/core/zypper/commands/refresh_services.rb +21 -0
  227. data/lib/kanrisuru/core/zypper/commands/remove.rb +26 -0
  228. data/lib/kanrisuru/core/zypper/commands/remove_lock.rb +22 -0
  229. data/lib/kanrisuru/core/zypper/commands/remove_repo.rb +32 -0
  230. data/lib/kanrisuru/core/zypper/commands/remove_service.rb +21 -0
  231. data/lib/kanrisuru/core/zypper/commands/rename_repo.rb +20 -0
  232. data/lib/kanrisuru/core/zypper/commands/search.rb +52 -0
  233. data/lib/kanrisuru/core/zypper/commands/source_install.rb +22 -0
  234. data/lib/kanrisuru/core/zypper/commands/update.rb +27 -0
  235. data/lib/kanrisuru/core/zypper/commands/verify.rb +23 -0
  236. data/lib/kanrisuru/core/zypper/commands.rb +109 -0
  237. data/lib/kanrisuru/core/zypper/constants.rb +19 -0
  238. data/lib/kanrisuru/core/zypper/parser.rb +11 -0
  239. data/lib/kanrisuru/core/zypper/parsers/base.rb +18 -0
  240. data/lib/kanrisuru/core/zypper/parsers/info.rb +77 -0
  241. data/lib/kanrisuru/core/zypper/parsers/list_locks.rb +36 -0
  242. data/lib/kanrisuru/core/zypper/parsers/list_patches.rb +40 -0
  243. data/lib/kanrisuru/core/zypper/parsers/list_repos.rb +37 -0
  244. data/lib/kanrisuru/core/zypper/parsers/list_services.rb +36 -0
  245. data/lib/kanrisuru/core/zypper/parsers/list_updates.rb +32 -0
  246. data/lib/kanrisuru/core/zypper/parsers/patch_check.rb +34 -0
  247. data/lib/kanrisuru/core/zypper/parsers/search.rb +37 -0
  248. data/lib/kanrisuru/core/zypper/types.rb +88 -0
  249. data/lib/kanrisuru/core/zypper.rb +5 -998
  250. data/lib/kanrisuru/version.rb +1 -1
  251. data/spec/unit/util_spec.rb +16 -0
  252. metadata +231 -2
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module Stream
6
+ def read_file_chunk(path, start_line, end_line)
7
+ if !start_line.instance_of?(Integer) || !end_line.instance_of?(Integer) ||
8
+ start_line > end_line || start_line.negative? || end_line.negative?
9
+ raise ArgumentError, 'Invalid start or end'
10
+ end
11
+
12
+ end_line = end_line - start_line + 1
13
+ command = Kanrisuru::Command.new("tail -n +#{start_line} #{path} | head -n #{end_line}")
14
+
15
+ execute_shell(command)
16
+
17
+ Kanrisuru::Result.new(command, &:to_a)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module Stream
6
+ def sed(path, expression, replacement, opts = {})
7
+ command = Kanrisuru::Command.new('sed')
8
+ command.append_flag('-i', opts[:in_place])
9
+ command.append_flag('-r', opts[:regexp_extended])
10
+
11
+ command << "'s/#{expression}/#{replacement}/g'"
12
+ command << "'#{path}'"
13
+
14
+ append_file(command, opts)
15
+ execute_shell(command)
16
+
17
+ Kanrisuru::Result.new(command) do |cmd|
18
+ Parser::Sed.parse(cmd, opts)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module Stream
6
+ def tail(path, opts = {})
7
+ command = Kanrisuru::Command.new('tail')
8
+ command.append_arg('-c', opts[:bytes])
9
+ command.append_arg('-n', opts[:lines])
10
+ command << path
11
+
12
+ execute_shell(command)
13
+
14
+ Kanrisuru::Result.new(command, &:to_a)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'commands/cat'
4
+ require_relative 'commands/echo'
5
+ require_relative 'commands/head'
6
+ require_relative 'commands/read_file_chunk'
7
+ require_relative 'commands/sed'
8
+ require_relative 'commands/tail'
9
+
10
+ module Kanrisuru
11
+ module Core
12
+ module Stream
13
+ private
14
+
15
+ def append_file(command, opts)
16
+ return unless Kanrisuru::Util.present?(opts[:new_file])
17
+
18
+ case opts[:mode]
19
+ when 'append'
20
+ command << ">> #{opts[:new_file]}"
21
+ when 'write'
22
+ command << "> #{opts[:new_file]}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'parsers/echo'
4
+ require_relative 'parsers/sed'
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module Stream
6
+ module Parser
7
+ class Echo
8
+ def self.parse(command, opts)
9
+ return if Kanrisuru::Util.present?(opts[:new_file])
10
+
11
+ command.to_s
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module Stream
6
+ module Parser
7
+ class Sed
8
+ def self.parse(command, opts)
9
+ return if Kanrisuru::Util.present?(opts[:new_file])
10
+
11
+ command.to_a.join("\n")
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'stream/parser'
4
+ require_relative 'stream/commands'
5
+
3
6
  module Kanrisuru
4
7
  module Core
5
8
  module Stream
@@ -11,110 +14,6 @@ module Kanrisuru
11
14
  os_define :linux, :sed
12
15
  os_define :linux, :echo
13
16
  os_define :linux, :cat
14
-
15
- def head(path, opts = {})
16
- command = Kanrisuru::Command.new('head')
17
- command.append_arg('-c', opts[:bytes])
18
- command.append_arg('-n', opts[:lines])
19
- command << path
20
-
21
- execute_shell(command)
22
-
23
- Kanrisuru::Result.new(command, &:to_a)
24
- end
25
-
26
- def tail(path, opts = {})
27
- command = Kanrisuru::Command.new('tail')
28
- command.append_arg('-c', opts[:bytes])
29
- command.append_arg('-n', opts[:lines])
30
- command << path
31
-
32
- execute_shell(command)
33
-
34
- Kanrisuru::Result.new(command, &:to_a)
35
- end
36
-
37
- def read_file_chunk(path, start_line, end_line)
38
- if !start_line.instance_of?(Integer) || !end_line.instance_of?(Integer) ||
39
- start_line > end_line || start_line.negative? || end_line.negative?
40
- raise ArgumentError, 'Invalid start or end'
41
- end
42
-
43
- end_line = end_line - start_line + 1
44
- command = Kanrisuru::Command.new("tail -n +#{start_line} #{path} | head -n #{end_line}")
45
-
46
- execute_shell(command)
47
-
48
- Kanrisuru::Result.new(command, &:to_a)
49
- end
50
-
51
- def sed(path, expression, replacement, opts = {})
52
- command = Kanrisuru::Command.new('sed')
53
- command.append_flag('-i', opts[:in_place])
54
- command.append_flag('-r', opts[:regexp_extended])
55
-
56
- command << "'s/#{expression}/#{replacement}/g'"
57
- command << "'#{path}'"
58
-
59
- append_file(command, opts)
60
- execute_shell(command)
61
-
62
- Kanrisuru::Result.new(command) do |cmd|
63
- if Kanrisuru::Util.present?(opts[:new_file])
64
- nil
65
- else
66
- cmd.to_a.join("\n")
67
- end
68
- end
69
- end
70
-
71
- def echo(text, opts = {})
72
- command = Kanrisuru::Command.new('echo')
73
- command.append_flag('-e', opts[:backslash])
74
- command << "'#{text}'"
75
-
76
- append_file(command, opts)
77
- execute_shell(command)
78
-
79
- Kanrisuru::Result.new(command) do |cmd|
80
- if Kanrisuru::Util.present?(opts[:new_file])
81
- nil
82
- else
83
- cmd.to_s
84
- end
85
- end
86
- end
87
-
88
- def cat(files, opts = {})
89
- command = Kanrisuru::Command.new('cat')
90
- command.append_flag('-T', opts[:show_tabs])
91
- command.append_flag('-n', opts[:number])
92
- command.append_flag('-s', opts[:squeeze_blank])
93
- command.append_flag('-v', opts[:show_nonprinting])
94
- command.append_flag('-E', opts[:show_ends])
95
- command.append_flag('-b', opts[:number_nonblank])
96
- command.append_flag('-A', opts[:show_all])
97
-
98
- command.append_array(files)
99
-
100
- append_file(command, opts)
101
- execute_shell(command)
102
-
103
- Kanrisuru::Result.new(command, &:to_a)
104
- end
105
-
106
- private
107
-
108
- def append_file(command, opts)
109
- return unless Kanrisuru::Util.present?(opts[:new_file])
110
-
111
- case opts[:mode]
112
- when 'append'
113
- command << ">> #{opts[:new_file]}"
114
- when 'write'
115
- command << "> #{opts[:new_file]}"
116
- end
117
- end
118
17
  end
119
18
  end
120
19
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def cpu_info(spec)
7
+ Kanrisuru.logger.info do
8
+ 'DEPRECATION WARNING: cpu_info will be removed in the upcoming major release. Use lscpu instead.'
9
+ end
10
+
11
+ name =
12
+ case spec
13
+ when 'sockets'
14
+ '^Socket'
15
+ when 'cores_per_socket'
16
+ '^Core'
17
+ when 'threads'
18
+ '^Thread'
19
+ when 'cores'
20
+ '^CPU('
21
+ else
22
+ return
23
+ end
24
+
25
+ command = Kanrisuru::Command.new("lscpu | grep -i '#{name}' | awk '{print $NF}'")
26
+ execute(command)
27
+
28
+ Kanrisuru::Result.new(command, &:to_i)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def free(type)
7
+ conversions = {
8
+ total: 'MemTotal',
9
+ free: 'MemFree',
10
+ swap: 'SwapTotal',
11
+ swap_free: 'SwapFree'
12
+ }
13
+
14
+ option = conversions[type.to_sym]
15
+ raise ArgumentError, 'Invalid mem type' unless option
16
+
17
+ command = Kanrisuru::Command.new("cat /proc/meminfo | grep -i '^#{option}' | awk '{print $2}'")
18
+ execute(command)
19
+
20
+ ## In kB
21
+ Kanrisuru::Result.new(command, &:to_i)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def kstat
7
+ kernel_statistics
8
+ end
9
+
10
+ def kernel_statistics
11
+ command = Kanrisuru::Command.new('cat /proc/stat')
12
+
13
+ execute_shell(command)
14
+
15
+ Kanrisuru::Result.new(command) do |cmd|
16
+ Parser::KernelStatistics.parse(cmd)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def kill(signal, pids)
7
+ raise ArgumentError, 'Invalid signal' unless Kanrisuru::Util::Signal.valid?(signal)
8
+
9
+ ## Use named signals for readabilitiy
10
+ signal = Kanrisuru::Util::Signal[signal] if signal.instance_of?(Integer)
11
+
12
+ command = Kanrisuru::Command.new('kill')
13
+ command << "-#{signal}"
14
+ command << (pids.instance_of?(Array) ? pids.join(' ') : pids)
15
+
16
+ execute_shell(command)
17
+
18
+ Kanrisuru::Result.new(command)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def last(opts = {})
7
+ command =
8
+ if opts[:failed_attempts]
9
+ Kanrisuru::Command.new('lastb')
10
+ else
11
+ Kanrisuru::Command.new('last')
12
+ end
13
+
14
+ command.append_flag('-i')
15
+ command.append_flag('-F')
16
+ command.append_arg('-f', opts[:file])
17
+
18
+ ## Some systems only use 1 space between user and TTY field
19
+ ## Add an additional space in output formatting for simple parsing
20
+ ## logic.
21
+ command | "sed 's/ / /'"
22
+
23
+ execute_shell(command)
24
+
25
+ Kanrisuru::Result.new(command) do |cmd|
26
+ Parser::Last.parse(cmd, opts)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def load_average
7
+ command = Kanrisuru::Command.new("cat /proc/loadavg | awk '{print $1,$2,$3}'")
8
+ execute(command)
9
+
10
+ Kanrisuru::Result.new(command) do |cmd|
11
+ Parser::LoadAverage.parse(cmd)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def load_env
7
+ command = Kanrisuru::Command.new('env')
8
+ execute_shell(command)
9
+
10
+ Kanrisuru::Result.new(command) do |cmd|
11
+ Parser::LoadEnv.parse(cmd)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def lscpu
7
+ command = Kanrisuru::Command.new('lscpu')
8
+
9
+ execute_shell(command)
10
+
11
+ Kanrisuru::Result.new(command) do |cmd|
12
+ Parser::Lscpu.parse(cmd)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def lsof(_opts = {})
7
+ command = Kanrisuru::Command.new('lsof -F pcuftDsin')
8
+ execute_shell(command)
9
+
10
+ Kanrisuru::Result.new(command) do |cmd|
11
+ Parser::Lsof.parse(cmd)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def poweroff(opts = {})
7
+ time = opts[:time]
8
+ cancel = opts[:cancel]
9
+ message = opts[:message]
10
+ no_wall = opts[:no_wall]
11
+
12
+ command = Kanrisuru::Command.new('shutdown')
13
+
14
+ if Kanrisuru::Util.present?(cancel)
15
+ command.append_flag('-c')
16
+ command << message if message
17
+ else
18
+ time = format_shutdown_time(time) if Kanrisuru::Util.present?(time)
19
+
20
+ if Kanrisuru::Util.present?(no_wall)
21
+ command.append_flag('--no-wall')
22
+ else
23
+ command << time if time
24
+ command << message if message
25
+ end
26
+ end
27
+
28
+ begin
29
+ execute_shell(command)
30
+
31
+ Kanrisuru::Result.new(command)
32
+ rescue IOError
33
+ ## When powering off 'now', ssh io stream closes
34
+ ## Set exit status to 0
35
+ command.handle_status(0)
36
+
37
+ Kanrisuru::Result.new(command)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def ps(opts = {})
7
+ group = opts[:group]
8
+ user = opts[:user]
9
+ pid = opts[:pid]
10
+ ppid = opts[:ppid]
11
+ # tree = opts[:tree]
12
+
13
+ command = Kanrisuru::Command.new('ps ww')
14
+
15
+ if !user.nil? || !group.nil? || !pid.nil? || !ppid.nil?
16
+ command.append_arg('--user', user.instance_of?(Array) ? user.join(',') : user)
17
+ command.append_arg('--group', group.instance_of?(Array) ? group.join(',') : group)
18
+ command.append_arg('--pid', pid.instance_of?(Array) ? pid.join(',') : pid)
19
+ command.append_arg('--ppid', ppid.instance_of?(Array) ? ppid.join(',') : ppid)
20
+ else
21
+ command.append_flag('ax')
22
+ end
23
+
24
+ command.append_arg('-o', 'uid,user,gid,group,ppid,pid,pcpu,pmem,stat,pri,flags,policy,time,cmd')
25
+ command.append_flag('--no-headers')
26
+
27
+ execute(command)
28
+
29
+ Kanrisuru::Result.new(command) do |cmd|
30
+ Parser::Ps.parse(cmd)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def reboot(opts = {})
7
+ time = opts[:time]
8
+ cancel = opts[:cancel]
9
+ message = opts[:message]
10
+ no_wall = opts[:no_wall]
11
+
12
+ command = Kanrisuru::Command.new('shutdown')
13
+
14
+ if Kanrisuru::Util.present?(cancel)
15
+ command.append_flag('-c')
16
+ command << message if message
17
+ else
18
+ command.append_flag('-r')
19
+
20
+ time = format_shutdown_time(time) if Kanrisuru::Util.present?(time)
21
+
22
+ if Kanrisuru::Util.present?(no_wall)
23
+ command.append_flag('--no-wall')
24
+ else
25
+ command << time if time
26
+ command << message if message
27
+ end
28
+ end
29
+
30
+ begin
31
+ execute_shell(command)
32
+ Kanrisuru::Result.new(command)
33
+ rescue IOError
34
+ ## When rebooting 'now', ssh io stream closes
35
+ ## Set exit status to 0
36
+ command.handle_status(0)
37
+ Kanrisuru::Result.new(command)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def uptime
7
+ ## Ported from https://github.com/djberg96/sys-uptime
8
+ command = Kanrisuru::Command.new('cat /proc/uptime')
9
+
10
+ execute_shell(command)
11
+
12
+ Kanrisuru::Result.new(command) do |cmd|
13
+ Parser::Uptime.parse(cmd)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def who(opts = {})
7
+ w(opts)
8
+ end
9
+
10
+ def w(opts = {})
11
+ users = opts[:users]
12
+ command = Kanrisuru::Command.new('w -hi')
13
+
14
+ command << users if Kanrisuru::Util.present?(users)
15
+
16
+ execute(command)
17
+
18
+ Kanrisuru::Result.new(command) do |cmd|
19
+ Parser::W.parse(cmd)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'commands/cpu_info'
4
+ require_relative 'commands/free'
5
+ require_relative 'commands/kernel_statistics'
6
+ require_relative 'commands/kill'
7
+ require_relative 'commands/last'
8
+ require_relative 'commands/load_average'
9
+ require_relative 'commands/load_env'
10
+ require_relative 'commands/lscpu'
11
+ require_relative 'commands/lsof'
12
+ require_relative 'commands/poweroff'
13
+ require_relative 'commands/ps'
14
+ require_relative 'commands/reboot'
15
+ require_relative 'commands/uptime'
16
+ require_relative 'commands/w'
17
+
18
+ module Kanrisuru
19
+ module Core
20
+ module System
21
+ private
22
+
23
+ def format_shutdown_time(time)
24
+ if time.instance_of?(Integer) || !/^[0-9]+$/.match(time).nil?
25
+ "+#{time}"
26
+ elsif !/^[0-9]{0,2}:[0-9]{0,2}$/.match(time).nil? || time == 'now'
27
+ time
28
+ else
29
+ raise ArgumentError, 'Invalid time format'
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'parsers/kernel_statistics'
4
+ require_relative 'parsers/last'
5
+ require_relative 'parsers/load_average'
6
+ require_relative 'parsers/load_env'
7
+ require_relative 'parsers/lscpu'
8
+ require_relative 'parsers/lsof'
9
+ require_relative 'parsers/ps'
10
+ require_relative 'parsers/uptime'
11
+ require_relative 'parsers/w'