kscript 1.0.6 → 1.0.7

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 (27) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +39 -47
  3. data/lib/kscript/base.rb +1 -1
  4. data/lib/kscript/cli.rb +2 -2
  5. data/lib/kscript/logger.rb +9 -4
  6. data/lib/kscript/plugins/{kk_apnic_utils.rb → kk_apnic_ip_utils.rb} +2 -2
  7. data/lib/kscript/plugins/kk_aws_s3_utils.rb +5 -5
  8. data/lib/kscript/plugins/{kk_es_fingerprint_utils.rb → kk_elastic_cert_finger_utils.rb} +3 -3
  9. data/lib/kscript/plugins/{kk_ffmpeg_utils.rb → kk_ffmpeg_install_utils.rb} +3 -3
  10. data/lib/kscript/plugins/{kk_rename_utils.rb → kk_file_rename_utils.rb} +3 -3
  11. data/lib/kscript/plugins/{kk_ip_utils.rb → kk_ip_lookup_utils.rb} +31 -16
  12. data/lib/kscript/plugins/{kk_jenkins_utils.rb → kk_jenkins_manage_utils.rb} +3 -3
  13. data/lib/kscript/plugins/{kk_kibana_utils.rb → kk_kibana_manage_utils.rb} +18 -18
  14. data/lib/kscript/plugins/{kk_lvm_utils.rb → kk_lvm_manage_utils.rb} +3 -3
  15. data/lib/kscript/plugins/{kk_optimize_utils.rb → kk_mac_optimize_utils.rb} +3 -3
  16. data/lib/kscript/plugins/{kk_syscheck_utils.rb → kk_mac_status_utils.rb} +28 -16
  17. data/lib/kscript/plugins/{kk_portscan_utils.rb → kk_port_scan_utils.rb} +3 -3
  18. data/lib/kscript/plugins/{kk_projscan_utils.rb → kk_project_scan_utils.rb} +4 -4
  19. data/lib/kscript/plugins/{kk_sh_utils.rb → kk_shell_helper_utils.rb} +11 -6
  20. data/lib/kscript/plugins/{kk_usd_utils.rb → kk_usd_rate_utils.rb} +2 -2
  21. data/lib/kscript/plugins/{kk_cleaner_utils.rb → kk_vcs_cleaner_utils.rb} +3 -3
  22. data/lib/kscript/plugins/kk_wg_acl_utils.rb +1 -1
  23. data/lib/kscript/plugins/kk_wg_pass_utils.rb +1 -1
  24. data/lib/kscript/plugins.rb +1 -1
  25. data/lib/kscript/version.rb +1 -1
  26. metadata +16 -17
  27. data/lib/kscript/plugins/kk_top_utils.rb +0 -71
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29c49db2c8133bbc61a27f182bd4aa2920a3f6ba44aa957e989c81ea1ebde95b
4
- data.tar.gz: f5bb31a125f117daffa3b4613c694e09982d396810e9ca089772e562115d6b38
3
+ metadata.gz: 4c903c89ab57a0f91270b2b320b60981ba56ba5d6e4bb4096eb6bd26ab7b5a61
4
+ data.tar.gz: 17a2a9d64078442493704bb60065bcc14a4b5728dcf1b7caf2440f0ce80c0008
5
5
  SHA512:
6
- metadata.gz: 7f1c01ad2eb82ed4dc323ed9e926a4fd89ac9e9ad2c204fec077938bde8f98d50698e2f68cc08bcc3210947feb2d11e8408b567eea2aee95c0c9d800eb5384a8
7
- data.tar.gz: 9c1f9ac7c23b94611ee489304ec32ee5e77c8d2c643acc107bab41e0c8476760d17f0c3728a6ee6310cca7806989e6e7f0f8faf0a79a033171073e9abf6758f3
6
+ metadata.gz: f27cd8c019f492bd37359c01e0ca710ecc3ec99818b795efc3fd7a688ff4b06b47892421e01c7ceda79066c44184f60d86cfa07ed0c79df9db97dc367cb358cb
7
+ data.tar.gz: e83ee610a5684dea38c12a68933e23224dee8b2654e9c689ed81ae57e4ee6c7f52620fec51347d6b1f9288075b10e1a42fa041ae8a8c4c4c61614e04ce8dfc51
data/README.md CHANGED
@@ -12,20 +12,20 @@ _ ,< _(__ )/ /__ _ / _ / __ /_/ / /_
12
12
  [![Gem Version](https://img.shields.io/gem/v/kscript?style=flat-square)](https://rubygems.org/gems/kscript)
13
13
  [![CI Status](https://github.com/kevin197011/kscript/actions/workflows/gem-push.yml/badge.svg?branch=main)](https://github.com/kevin197011/kscript/actions/workflows/gem-push.yml)
14
14
 
15
- > Ruby CLI 工具集,专为系统运维、开发自动化、网络与项目管理场景设计。支持插件化、极致美化输出、自动补全、结构化日志、CI/CD 自动发布等现代特性。
15
+ > Ruby CLI 工具集,专为系统运维、开发自动化、网络与项目管理场景设计。支持插件化、美化输出、自动补全、结构化日志、CI/CD 自动发布等现代特性。
16
16
 
17
17
  ---
18
18
 
19
19
  ## ✨ 特性亮点
20
- - **统一 CLI 框架**:所有命令一键调用,美化输出
21
- - **插件化架构**:业务命令即插件,易扩展、易维护
22
- - **极致美化**:ASCII banner、彩色分组、对齐、分隔线,极客体验
23
- - **人性化/结构化日志双模式**:`--log`/`--log-level` 切换,支持 JSON/终端友好输出
24
- - **自动补全**:zsh/bash 补全脚本自动部署,命令一键补全
25
- - **参数健壮**:所有插件兼容多余参数,支持全局参数过滤
26
- - **CI/CD 自动发布**:GitHub Actions 自动构建并推送 RubyGem
27
- - **多平台支持**:macOS、LinuxWindows(部分工具)
28
- - **自动生成配置**:首次安装自动生成 `~/.kscript/.env` 配置示例
20
+ - 统一 CLI 框架,所有命令一键调用,美化输出
21
+ - 插件化架构,业务命令即插件,易扩展、易维护
22
+ - 美化输出:ASCII banner、彩色分组、分隔线
23
+ - 人性化/结构化日志双模式:`--log`/`--log-level` 切换,支持 JSON/终端友好输出
24
+ - 自动补全:zsh/bash 补全脚本自动部署
25
+ - 参数健壮:所有插件兼容多余参数,支持全局参数过滤
26
+ - CI/CD 自动发布:GitHub Actions 自动构建并推送 RubyGem
27
+ - 多平台支持:macOS、Linux,部分工具支持 Windows
28
+ - 自动生成配置:首次安装自动生成 `~/.kscript/.env` 配置示例
29
29
 
30
30
  ---
31
31
 
@@ -67,17 +67,17 @@ kscript version
67
67
  ```bash
68
68
  kscript <command> [args...]
69
69
  # 例如
70
- kscript apnic CN
71
- kscript portscan 192.168.1.1
72
- kscript sh 'ls -l'
73
- kscript projscan ~/projects
70
+ kscript apnic_ip CN
71
+ kscript port_scan 192.168.1.1
72
+ kscript shell_helper 'ls -l'
73
+ kscript project_scan ~/projects
74
74
  kscript aws_s3 --file local.txt --bucket my-bucket --key test.txt --region ap-northeast-1 --access_key xxx --secret_key yyy
75
75
  ```
76
76
 
77
77
  ### 结构化日志模式
78
78
  ```bash
79
- kscript apnic CN --log
80
- kscript portscan 192.168.1.1 --log-level=debug
79
+ kscript apnic_ip CN --log
80
+ kscript port_scan 192.168.1.1 --log-level=debug
81
81
  ```
82
82
 
83
83
  ---
@@ -86,40 +86,32 @@ kscript portscan 192.168.1.1 --log-level=debug
86
86
 
87
87
  > 运行 `kscript list` 可分组美化展示所有插件命令
88
88
 
89
- - **network**
90
- - `apnic`:获取国家 IPv4 段
91
- - `portscan`:端口扫描
92
- - `ip`:IP 工具
93
- - `wg_acl`:WireGuard 防火墙 ACL
94
- - `wg_pass`:WireGuard 密码工具
95
- - **project**
96
- - `projscan`:扫描目录下所有 git 项目
97
- - `cleaner`:源码多版本清理
98
- - `rename`:批量重命名文件
99
- - **system/macos**
100
- - `syscheck`:macOS 系统健康检查
101
- - `top`:macOS 资源占用排行
102
- - `optimize`:macOS 性能优化
103
- - `sh`:命令行速查/cheatsheet
104
- - **media**
105
- - `ffmpeg`:FFmpeg 安装与检测
106
- - **elastic**
107
- - `es_fingerprint`:Elasticsearch 证书指纹
108
- - `kibana`:Kibana 空间/索引/用户/角色自动化
109
- - **ci**
110
- - `jenkins`:Jenkins Job 导入导出
111
- - **cloud**
112
- - `aws_s3`:AWS S3 文件上传测试
113
- - **其它**
114
- - `usd`:美元汇率工具
115
- - `lvm`:LVM 卷管理
116
- - `windows_font_enhancer`:Windows 字体增强
89
+ | 分组 | 命令 | 用法示例 | 参数说明 | 描述 |
90
+ |-----------|------------------------|------------------------------------------------------------------|--------------------------------------------------------|--------------------------------------------------------------|
91
+ | network | `apnic_ip` | `kscript apnic_ip CN` | `[country_code]` | Get APNIC IPv4 ranges for a country. |
92
+ | network | `port_scan` | `kscript port_scan 192.168.1.1` | `<target_host> [ports] [thread_count]` | Scan open ports on a target host. |
93
+ | network | `ip_lookup` | `kscript ip_lookup 8.8.8.8` | `<ip_address>` | Query IP geolocation and ISP info. |
94
+ | network | `wg_acl` | `kscript wg_acl add --ip=10.0.0.2` | `[subcommand] [options]` | Manage WireGuard firewall ACL rules. |
95
+ | network | `wg_pass` | `kscript wg_pass 32` | `[length]` | Generate a random password for WireGuard. |
96
+ | project | `project_scan` | `kscript project_scan ~/projects/src` | `[src_path]` | Scan and list all git projects in a directory. |
97
+ | project | `vcs_cleaner` | `kscript vcs_cleaner ~/projects/src` | `[src_path]` | Clean old source code versions, keep N latest. |
98
+ | project | `file_rename` | `kscript file_rename foo bar ./src` | `<pattern> <replacement> [path]` | Batch rename files by pattern. |
99
+ | system | `shell_helper` | `kscript shell_helper 'ls'` | `[subcommand] [args...]` | Query shell command usage and cheatsheets. |
100
+ | system | `lvm_manage` | `kscript lvm_manage /dev/sda2 /mnt/data` | `<device> <mount_point>` | Mount and manage Linux LVM volumes. |
101
+ | macos | `mac_status` | `kscript mac_status` | | Show macOS system resource monitor report. |
102
+ | macos | `mac_optimize` | `kscript mac_optimize` | | Optimize macOS system performance. |
103
+ | media | `ffmpeg_install` | `kscript ffmpeg_install 6.0` | `[version]` | Install and verify FFmpeg on Linux. |
104
+ | elastic | `elastic_cert_finger` | `kscript elastic_cert_finger ./ca.crt` | `<cert_file>` | Generate Elasticsearch certificate SHA256 fingerprint. |
105
+ | elastic | `kibana_manage` | `kscript kibana_manage export --host=localhost --index=log-*` | `[subcommand] [options]` | Kibana automation: space, index, user, role management. |
106
+ | ci | `jenkins_manage` | `kscript jenkins_manage list --host=jenkins.local` | `[subcommand] [options]` | Jenkins job export/import automation. |
107
+ | cloud | `aws_s3` | `kscript aws_s3 --file local.txt --bucket my-bucket ...` | `--file --bucket --key --region --access_key --secret_key` | Upload a file to AWS S3 for testing. |
108
+ | finance | `usd_rate` | `kscript usd_rate CNY` | `[currency_code]` | Get latest USD exchange rates. |
117
109
 
118
110
  ---
119
111
 
120
112
  ## ⚡ Shell 自动补全 & 配置示例
121
113
 
122
- - **首次安装/升级自动为 zsh/bash 部署补全脚本,并生成 `~/.kscript/.env` 配置示例**
114
+ - 首次安装/升级自动为 zsh/bash 部署补全脚本,并生成 `~/.kscript/.env` 配置示例
123
115
  - 补全脚本路径:
124
116
  - zsh: `~/.zsh/completions/_kscript`
125
117
  - bash: `~/.bash_completion.d/kscript`
@@ -153,7 +145,7 @@ LOG=1
153
145
 
154
146
  ## 🧑‍💻 插件开发规范
155
147
  - 插件文件统一放在 `lib/kscript/plugins/kk_xxx_utils.rb`
156
- - 类名如 `KkApnicUtils`,自动注册为 `apnic` 命令
148
+ - 类名如 `KkApnicIpUtils`,自动注册为 `apnic_ip` 命令
157
149
  - 支持 `self.description`、`self.usage`、`self.arguments`、`self.group`、`self.author`
158
150
  - 输出统一用 `logger.kinfo`/`logger.kerror`,支持结构化日志
159
151
  - 兼容多余参数,避免 ArgumentError
@@ -172,7 +164,7 @@ LOG=1
172
164
  ## 📦 依赖与兼容性
173
165
 
174
166
  - Ruby >= 3.0
175
- - 依赖:bcrypt, http, nokogiri, thor, aws-sdk-s3 等
167
+ - 依赖:bcrypt, http, nokogiri, thor, aws-sdk-s3, httpx
176
168
  - 支持 macOS、Linux,部分工具支持 Windows
177
169
 
178
170
  ---
data/lib/kscript/base.rb CHANGED
@@ -37,7 +37,7 @@ module Kscript
37
37
 
38
38
  # 判断是否为人性化输出模式(无 --log/--log-level 参数且 ENV['LOG'] 未设置)
39
39
  def human_output?
40
- !(ARGV.include?('--log') || ARGV.include?('--log-level') || ENV['LOG'])
40
+ !(ARGV.include?('--log') || ARGV.include?('--log-level') || ENV.fetch('LOG', nil))
41
41
  end
42
42
  end
43
43
  end
data/lib/kscript/cli.rb CHANGED
@@ -70,7 +70,7 @@ module Kscript
70
70
  orig_command = info[:name].to_s.sub(/^kk_/, '')
71
71
  # shell -> sh
72
72
  command = orig_command == 'shell' ? 'sh' : orig_command
73
- command = reserved.include?(command) ? "#{command}_cmd" : command
73
+ command = "#{command}_cmd" if reserved.include?(command)
74
74
  klass = info[:class]
75
75
  desc command,
76
76
  (info[:description] || 'No description') + (reserved.include?(orig_command) ? " (original: #{orig_command})" : '')
@@ -113,7 +113,7 @@ module Kscript
113
113
  else
114
114
  puts bold('Loaded environment variables:')
115
115
  keys.sort.each do |k|
116
- v = ENV[k]
116
+ v = ENV.fetch(k, nil)
117
117
  puts green(k.ljust(28)) + gray('=') + cyan(v)
118
118
  end
119
119
  end
@@ -5,23 +5,27 @@ module Kscript
5
5
  class Logger
6
6
  LEVELS = %i[debug info warn error fatal unknown].freeze
7
7
 
8
- # 极客风格终端输出
8
+ # Colorful output
9
9
  COLORS = {
10
10
  info: "\e[32m", # green
11
11
  warn: "\e[33m", # yellow
12
12
  error: "\e[31m", # red
13
13
  debug: "\e[90m", # gray
14
+ fatal: "\e[35m", # magenta
15
+ unknown: "\e[36m", # cyan
14
16
  reset: "\e[0m"
15
17
  }.freeze
16
18
 
17
19
  def initialize(service: 'kscript', level: :info, out: $stdout, human_output: nil)
20
+ require 'json'
21
+ require 'time'
18
22
  @service = service
19
23
  @logger = ::Logger.new(out)
20
24
  @logger.level = ::Logger.const_get(level.to_s.upcase)
21
25
  @human_output = human_output
22
26
  end
23
27
 
24
- # 允许外部设置输出模式
28
+ # 设置人类可读输出模式
25
29
  def set_human_output(val)
26
30
  @human_output = val
27
31
  end
@@ -44,6 +48,7 @@ module Kscript
44
48
  end
45
49
  end
46
50
 
51
+ # 结构化日志输出
47
52
  def log(level, message, context = {})
48
53
  entry = {
49
54
  timestamp: Time.now.utc.iso8601,
@@ -55,7 +60,7 @@ module Kscript
55
60
  @logger.send(level, entry.to_json)
56
61
  end
57
62
 
58
- # 终端输出
63
+ # 终端输出(带颜色、trace、时间等)
59
64
  def klog(level, message, context = {})
60
65
  if human_output?
61
66
  puts "[#{level.to_s.upcase}] #{message} #{context.map { |k, v| "#{k}=#{v}" }.join(' ')}".strip
@@ -72,7 +77,7 @@ module Kscript
72
77
  end
73
78
  end
74
79
 
75
- # 便捷方法
80
+ # 便捷方法(info/warn/error/debug)
76
81
  def kinfo(msg, ctx = {})
77
82
  klog(:info, msg, ctx)
78
83
  end
@@ -8,7 +8,7 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkApnicUtils < Base
11
+ class KkApnicIpUtils < Base
12
12
  attr_reader :country_sn, :cache_file
13
13
 
14
14
  # Initialize class instance, set country code and cache file path
@@ -69,7 +69,7 @@ module Kscript
69
69
  end
70
70
 
71
71
  def self.usage
72
- "kscript apnic CN\nkscript apnic US"
72
+ "kscript apnic_ip CN\nkscript apnic_ip US"
73
73
  end
74
74
 
75
75
  def self.group
@@ -17,14 +17,14 @@ module Kscript
17
17
  class KkAwsS3Utils < Base
18
18
  # 初始化,支持所有参数通过 CLI 传递
19
19
  def initialize(*args, **opts)
20
- super(*args, **opts)
20
+ super
21
21
  # 兼容 --file test.txt 以及 --file=test.txt
22
22
  @file = opts[:file] || args[0]
23
- @bucket = opts[:bucket] || args[1] || ENV['AWS_BUCKET']
23
+ @bucket = opts[:bucket] || args[1] || ENV.fetch('AWS_BUCKET', nil)
24
24
  @key = opts[:key] || args[2]
25
- @region = opts[:region] || ENV['AWS_REGION']
26
- @access_key = opts[:access_key] || ENV['AWS_ACCESS_KEY_ID']
27
- @secret_key = opts[:secret_key] || ENV['AWS_SECRET_ACCESS_KEY']
25
+ @region = opts[:region] || ENV.fetch('AWS_REGION', nil)
26
+ @access_key = opts[:access_key] || ENV.fetch('AWS_ACCESS_KEY_ID', nil)
27
+ @secret_key = opts[:secret_key] || ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)
28
28
  end
29
29
 
30
30
  # 主入口
@@ -8,7 +8,7 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkEsFingerprintUtils < Base
11
+ class KkElasticCertFingerUtils < Base
12
12
  DEFAULT_CERT_PATH = 'elasticsearch.crt'
13
13
 
14
14
  attr_reader :cert_path
@@ -16,7 +16,7 @@ module Kscript
16
16
  # Initialize with certificate path
17
17
  # @param cert_path [String] path to the certificate file
18
18
  def initialize(*args, **opts)
19
- super(*args, **opts)
19
+ super
20
20
  @cert_path = opts[:cert_path] || self.class::DEFAULT_CERT_PATH
21
21
  end
22
22
 
@@ -31,7 +31,7 @@ module Kscript
31
31
  end
32
32
 
33
33
  def self.usage
34
- "kscript es_fingerprint <cert_file>\nkscript es_fingerprint ./ca.crt"
34
+ "kscript elastic_cert_finger <cert_file>\nkscript elastic_cert_finger ./ca.crt"
35
35
  end
36
36
 
37
37
  def self.group
@@ -8,9 +8,9 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkFfmpegUtils < Base
11
+ class KkFfmpegInstallUtils < Base
12
12
  def initialize(*args, **opts)
13
- super(*args, **opts)
13
+ super
14
14
  end
15
15
 
16
16
  def run
@@ -28,7 +28,7 @@ module Kscript
28
28
  end
29
29
 
30
30
  def self.usage
31
- "kscript ffmpeg 6.0\nkscript ffmpeg latest"
31
+ "kscript ffmpeg_install 6.0\nkscript ffmpeg_install latest"
32
32
  end
33
33
 
34
34
  def self.group
@@ -8,11 +8,11 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkRenameUtils < Base
11
+ class KkFileRenameUtils < Base
12
12
  attr_reader :source_pattern, :target_pattern, :directory
13
13
 
14
14
  def initialize(*args, **opts)
15
- super(*args, **opts)
15
+ super
16
16
  @source_pattern = args[0]
17
17
  @target_pattern = args[1]
18
18
  @directory = args[2] || Dir.pwd
@@ -35,7 +35,7 @@ module Kscript
35
35
  end
36
36
 
37
37
  def self.usage
38
- "kscript rename foo bar ./src\nkscript rename 'test' 'prod' ~/projects"
38
+ "kscript file_rename foo bar ./src\nkscript file_rename 'test' 'prod' ~/projects"
39
39
  end
40
40
 
41
41
  def self.group
@@ -8,20 +8,23 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkIpUtils < Base
11
+ class KkIpLookupUtils < Base
12
12
  IP_API_BASE_URL = 'http://ip-api.com/json'
13
13
  IP_CHECK_URL = 'https://api.ipify.org?format=json'
14
14
 
15
15
  attr_reader :ip_address
16
16
 
17
17
  def initialize(*args, **opts)
18
- super(*args, **opts)
18
+ super
19
19
  @ip_address = args.first || fetch_public_ip
20
20
  end
21
21
 
22
- def run
22
+ def run(*args, **_opts)
23
23
  with_error_handling do
24
- fetch_location
24
+ ip = args.first || @ip_address
25
+ validate_ip_address!(ip)
26
+ response = make_api_request(ip)
27
+ handle_response(response)
25
28
  end
26
29
  end
27
30
 
@@ -36,7 +39,7 @@ module Kscript
36
39
  end
37
40
 
38
41
  def self.usage
39
- "kscript ip <ip_address>\nkscript ip 8.8.8.8"
42
+ "kscript ip_lookup <ip_address>\nkscript ip_lookup 8.8.8.8"
40
43
  end
41
44
 
42
45
  def self.group
@@ -54,31 +57,43 @@ module Kscript
54
57
  private
55
58
 
56
59
  def fetch_public_ip
57
- response = HTTP.get(IP_CHECK_URL)
58
- raise "Failed to detect public IP: #{response.status}" unless response.status.success?
60
+ begin
61
+ require 'httpx'
62
+ rescue LoadError
63
+ abort 'Missing dependency: httpx. Please run: gem install httpx'
64
+ end
65
+ response = HTTPX.get(IP_CHECK_URL)
66
+ response = response.first if response.is_a?(Array)
67
+ raise "Failed to detect public IP: #{response.status}" unless response.status == 200
59
68
 
60
69
  data = JSON.parse(response.body.to_s)
61
- logger.info("Detected public IP: #{data['ip']}")
70
+ logger.kinfo("Detected public IP: #{data['ip']}")
62
71
  data['ip']
63
72
  end
64
73
 
65
- def validate_ip_address!
66
- return if valid_ip_format?
74
+ def validate_ip_address!(ip)
75
+ return if valid_ip_format?(ip)
67
76
 
68
77
  raise ArgumentError, 'Invalid IP address format'
69
78
  end
70
79
 
71
- def valid_ip_format?
72
- /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match?(@ip_address)
80
+ def valid_ip_format?(ip)
81
+ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match?(ip)
73
82
  end
74
83
 
75
- def make_api_request
76
- HTTP.get("#{IP_API_BASE_URL}/#{@ip_address}")
84
+ def make_api_request(ip)
85
+ begin
86
+ require 'httpx'
87
+ rescue LoadError
88
+ abort 'Missing dependency: httpx. Please run: gem install httpx'
89
+ end
90
+ response = HTTPX.get("#{IP_API_BASE_URL}/#{ip}")
91
+ response.is_a?(Array) ? response.first : response
77
92
  end
78
93
 
79
94
  def handle_response(response)
80
- if response.status.success?
81
- logger.kinfo('IP location result', data: response.parse(:json))
95
+ if response.status == 200
96
+ logger.kinfo('IP location result', data: JSON.parse(response.body.to_s))
82
97
  else
83
98
  logger.kerror("API request failed: #{response.status}")
84
99
  end
@@ -8,9 +8,9 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkJenkinsUtils < Base
11
+ class KkJenkinsManageUtils < Base
12
12
  def initialize(*args, **opts)
13
- super(*args, **opts)
13
+ super
14
14
  jenkins_url, user, password = args
15
15
  @jenkins_url = jenkins_url
16
16
  @user = user
@@ -106,7 +106,7 @@ module Kscript
106
106
  end
107
107
 
108
108
  def self.usage
109
- "kscript jenkins list --host=jenkins.local\nkscript jenkins trigger --job=build"
109
+ "kscript jenkins_manage list --host=jenkins.local\nkscript jenkins_manage trigger --job=build"
110
110
  end
111
111
 
112
112
  def self.group
@@ -8,9 +8,9 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkKibanaUtils < Base
11
+ class KkKibanaManageUtils < Base
12
12
  def initialize(*args, **opts)
13
- super(*args, **opts)
13
+ super
14
14
  project_name, project_env, base_url, username, password = args
15
15
  @base_url = base_url
16
16
  @username = username
@@ -158,7 +158,7 @@ module Kscript
158
158
  end
159
159
 
160
160
  def self.usage
161
- "kscript kibana export --host=localhost --index=log-*\nkscript kibana import --file=dashboard.json"
161
+ "kscript kibana_manage export --host=localhost --index=log-*\nkscript kibana_manage import --file=dashboard.json"
162
162
  end
163
163
 
164
164
  def self.group
@@ -191,23 +191,23 @@ module Kscript
191
191
  # Construct the body for creating an index
192
192
  def index_body(index_name, uuid)
193
193
  {
194
- "contentTypeId": 'index-pattern',
195
- "data": {
196
- "fieldAttrs": '{}',
197
- "title": "#{index_name}*",
198
- "timeFieldName": '@timestamp',
199
- "sourceFilters": '[]',
200
- "fields": '[]',
201
- "fieldFormatMap": '{}',
202
- "runtimeFieldMap": '{}',
203
- "name": index_name,
204
- "allowHidden": false
194
+ contentTypeId: 'index-pattern',
195
+ data: {
196
+ fieldAttrs: '{}',
197
+ title: "#{index_name}*",
198
+ timeFieldName: '@timestamp',
199
+ sourceFilters: '[]',
200
+ fields: '[]',
201
+ fieldFormatMap: '{}',
202
+ runtimeFieldMap: '{}',
203
+ name: index_name,
204
+ allowHidden: false
205
205
  },
206
- "options": {
207
- "id": uuid,
208
- "overwrite": false
206
+ options: {
207
+ id: uuid,
208
+ overwrite: false
209
209
  },
210
- "version": 1
210
+ version: 1
211
211
  }
212
212
  end
213
213
 
@@ -8,7 +8,7 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkLvmUtils < Base
11
+ class KkLvmManageUtils < Base
12
12
  DEFAULT_CONFIG = {
13
13
  device: '/dev/sdb',
14
14
  volume_group: 'vg_data',
@@ -21,7 +21,7 @@ module Kscript
21
21
  # Initialize the LVM mounter with configuration
22
22
  # @param config [Hash] configuration options
23
23
  def initialize(*args, **opts)
24
- super(*args, **opts)
24
+ super
25
25
  @config = DEFAULT_CONFIG.merge(opts)
26
26
  end
27
27
 
@@ -47,7 +47,7 @@ module Kscript
47
47
  end
48
48
 
49
49
  def self.usage
50
- "kscript lvm /dev/sda2 /mnt/data\nkscript lvm /dev/vg0/lv_home /mnt/home"
50
+ "kscript lvm_manage /dev/sda2 /mnt/data\nkscript lvm_manage /dev/vg0/lv_home /mnt/home"
51
51
  end
52
52
 
53
53
  def self.group
@@ -8,9 +8,9 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkOptimizeUtils < Base
11
+ class KkMacOptimizeUtils < Base
12
12
  def initialize(*args, **opts)
13
- super(*args, **opts)
13
+ super
14
14
  end
15
15
 
16
16
  def run(*args, **_opts)
@@ -74,7 +74,7 @@ module Kscript
74
74
  end
75
75
 
76
76
  def self.usage
77
- 'kscript optimize'
77
+ 'kscript mac_optimize'
78
78
  end
79
79
 
80
80
  def self.group
@@ -8,9 +8,9 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkSyscheckUtils < Base
11
+ class KkMacStatusUtils < Base
12
12
  def initialize(*_args, **opts)
13
- super(**opts.merge(service: 'kk_syscheck'))
13
+ super(**opts.merge(service: 'kk_mac_status'))
14
14
  end
15
15
 
16
16
  def run
@@ -24,19 +24,12 @@ module Kscript
24
24
  logger.kinfo("📅 Date Time: #{Time.now}")
25
25
  logger.kinfo('')
26
26
 
27
- # CPU Usage
28
- logger.kinfo('===============================')
29
- logger.kinfo(' CPU Usage (Top 10)')
30
- logger.kinfo('===============================')
31
- cpu_output = `ps aux | sort -nrk 3 | head -n 10`
32
- logger.kinfo(cpu_output)
33
-
34
- # Memory Usage
35
- logger.kinfo('===============================')
36
- logger.kinfo(' Memory Usage (Top 10)')
37
- logger.kinfo('===============================')
38
- mem_output = `ps aux | sort -nrk 4 | head -n 10`
39
- logger.kinfo(mem_output)
27
+ # Top 10 by CPU
28
+ print_header('Top 10 Processes by CPU Usage')
29
+ print_process_list(:cpu)
30
+ # Top 10 by Memory
31
+ print_header('Top 10 Processes by Memory Usage')
32
+ print_process_list(:mem)
40
33
 
41
34
  # GPU Usage
42
35
  if `which powermetrics`.strip.empty?
@@ -77,6 +70,25 @@ module Kscript
77
70
  logger.kinfo("\n✅ System resource check completed!")
78
71
  end
79
72
 
73
+ def print_header(title)
74
+ logger.kinfo('')
75
+ logger.kinfo('===============================')
76
+ logger.kinfo(" #{title}")
77
+ logger.kinfo('===============================')
78
+ end
79
+
80
+ def print_process_list(sort_field)
81
+ lines = `ps aux`.split("\n")
82
+ lines.shift
83
+ processes = lines.map { |line| line.split(/\s+/, 11) }
84
+ index = sort_field == :cpu ? 2 : 3
85
+ top = processes.sort_by { |p| -p[index].to_f }.first(10)
86
+ logger.kinfo('USER PID %CPU %MEM COMMAND ')
87
+ top.each do |p|
88
+ logger.kinfo(format('%-10s %-8s %-5s %-5s %-10s', p[0], p[1], p[2], p[3], p[10][0..30]))
89
+ end
90
+ end
91
+
80
92
  def self.description
81
93
  'Show macOS system resource monitor report.'
82
94
  end
@@ -86,7 +98,7 @@ module Kscript
86
98
  end
87
99
 
88
100
  def self.usage
89
- "kscript syscheck\nkscript syscheck --detail"
101
+ "kscript mac_status\nkscript mac_status --detail"
90
102
  end
91
103
 
92
104
  def self.group
@@ -8,7 +8,7 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkPortscanUtils < Base
11
+ class KkPortScanUtils < Base
12
12
  attr_reader :host, :ports, :thread_count
13
13
 
14
14
  # Initialize the scanner with target host and port range
@@ -16,7 +16,7 @@ module Kscript
16
16
  # @param ports [Array<Integer>] list of ports to scan
17
17
  # @param thread_count [Integer] number of concurrent threads
18
18
  def initialize(*args, **opts)
19
- super(*args, **opts)
19
+ super
20
20
  @target = args[0]
21
21
  @ports = parse_ports(args[1] || (1..1024))
22
22
  @thread_count = (opts[:thread_count] || 50).to_i
@@ -98,7 +98,7 @@ module Kscript
98
98
  end
99
99
 
100
100
  def self.usage
101
- "kscript portscan 192.168.1.1\nkscript portscan example.com 22,80,443 100\nkscript portscan 192.168.1.1 1..1024 200"
101
+ "kscript port_scan 192.168.1.1\nkscript port_scan example.com 22,80,443 100\nkscript port_scan 192.168.1.1 1..1024 200"
102
102
  end
103
103
 
104
104
  def self.group
@@ -8,9 +8,9 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkProjscanUtils < Base
11
+ class KkProjectScanUtils < Base
12
12
  def initialize(*args, **opts)
13
- super(*args, **opts)
13
+ super
14
14
  end
15
15
 
16
16
  def run
@@ -34,7 +34,7 @@ module Kscript
34
34
  end
35
35
 
36
36
  def self.usage
37
- "kscript projscan ~/projects/src\nkscript projscan /opt --type=go"
37
+ "kscript project_scan ~/projects/src\nkscript project_scan /opt --type=go"
38
38
  end
39
39
 
40
40
  def self.group
@@ -56,7 +56,7 @@ module Kscript
56
56
 
57
57
  def scan_projects
58
58
  projects = []
59
- Dir.glob(File.join(@src_path, '*')).sort.each do |path|
59
+ Dir.glob(File.join(@src_path, '*')).each do |path|
60
60
  next unless File.directory?(path)
61
61
  next unless git_project?(path)
62
62
 
@@ -6,10 +6,9 @@
6
6
  # https://opensource.org/licenses/MIT
7
7
 
8
8
  require 'kscript'
9
- require 'httpx'
10
9
 
11
10
  module Kscript
12
- class KkShUtils < Base
11
+ class KkShellHelperUtils < Base
13
12
  CHT_SH_URL = 'https://cht.sh'
14
13
 
15
14
  attr_reader :command
@@ -17,7 +16,7 @@ module Kscript
17
16
  # Initialize with shell command to look up
18
17
  # @param command [String] command to get help for
19
18
  def initialize(*args, **opts)
20
- super(*args, **opts)
19
+ super
21
20
  @command = args.join(' ').strip
22
21
  end
23
22
 
@@ -44,7 +43,7 @@ module Kscript
44
43
  # Fetch and display command documentation
45
44
  def fetch_help(command)
46
45
  response = make_request(command)
47
- response = response.is_a?(Array) ? response.first : response
46
+ response = response.first if response.is_a?(Array)
48
47
  puts response.body
49
48
  rescue StandardError => e
50
49
  display_error(e)
@@ -55,7 +54,7 @@ module Kscript
55
54
  end
56
55
 
57
56
  def self.usage
58
- "kscript sh 'ls'\nkscript sh 'echo hello'"
57
+ "kscript shell_helper 'ls'\nkscript shell_helper 'echo hello'"
59
58
  end
60
59
 
61
60
  def self.group
@@ -67,7 +66,7 @@ module Kscript
67
66
  end
68
67
 
69
68
  def self.description
70
- 'Query sh command usage and cheatsheets.'
69
+ 'Query shell command usage and cheatsheets.'
71
70
  end
72
71
 
73
72
  private
@@ -75,6 +74,12 @@ module Kscript
75
74
  # Make HTTP request to cheat.sh
76
75
  # @return [HTTPX::Response] response from cheat.sh
77
76
  def make_request(command)
77
+ begin
78
+ require 'httpx'
79
+ require 'uri'
80
+ rescue LoadError
81
+ abort 'Missing dependency: httpx. Please run: gem install httpx'
82
+ end
78
83
  HTTPX.with(
79
84
  headers: {
80
85
  'User-Agent' => 'curl/8.0.1',
@@ -11,7 +11,7 @@ require 'net/http'
11
11
  require 'json'
12
12
 
13
13
  module Kscript
14
- class KkUsdUtils < Base
14
+ class KkUsdRateUtils < Base
15
15
  API_URL = 'https://api.exchangerate-api.com/v4/latest/USD'
16
16
 
17
17
  def initialize(currency_code = 'CNY', *args, **opts)
@@ -44,7 +44,7 @@ module Kscript
44
44
  end
45
45
 
46
46
  def self.usage
47
- "kscript usd CNY\nkscript usd EUR"
47
+ "kscript usd_rate CNY\nkscript usd_rate EUR"
48
48
  end
49
49
 
50
50
  def self.group
@@ -8,7 +8,7 @@
8
8
  require 'kscript'
9
9
 
10
10
  module Kscript
11
- class KkCleanerUtils < Base
11
+ class KkVcsCleanerUtils < Base
12
12
  DEFAULT_RETAIN_VERSIONS = 10
13
13
 
14
14
  attr_reader :source_path, :retain_count
@@ -17,7 +17,7 @@ module Kscript
17
17
  # @param source_path [String] path to source code directory
18
18
  # @param retain_count [Integer] number of versions to keep
19
19
  def initialize(*args, **opts)
20
- super(*args, **opts)
20
+ super
21
21
  @source_path = args[0] || '/data/sources/*/**'
22
22
  @retain_count = args[1] || DEFAULT_RETAIN_VERSIONS
23
23
  end
@@ -40,7 +40,7 @@ module Kscript
40
40
  end
41
41
 
42
42
  def self.usage
43
- "kscript source_cleaner ~/projects/src\nkscript source_cleaner . --exclude=vendor"
43
+ "kscript vcs_cleaner ~/projects/src\nkscript vcs_cleaner . --exclude=vendor"
44
44
  end
45
45
 
46
46
  def self.group
@@ -13,7 +13,7 @@ module Kscript
13
13
  ALLOWED_IPS = %w[127.0.0.1].freeze
14
14
 
15
15
  def initialize(*args, **opts)
16
- super(*args, **opts)
16
+ super
17
17
  end
18
18
 
19
19
  def run
@@ -10,7 +10,7 @@ require 'kscript'
10
10
  module Kscript
11
11
  class KkWgPassUtils < Base
12
12
  def initialize(*args, **opts)
13
- super(*args, **opts)
13
+ super
14
14
  end
15
15
 
16
16
  def run
@@ -5,7 +5,7 @@ module Kscript
5
5
  PLUGIN_DIR = File.expand_path('plugins', __dir__)
6
6
 
7
7
  def self.load_all
8
- Dir.glob(File.join(PLUGIN_DIR, 'kk_*.rb')).sort.each do |file|
8
+ Dir.glob(File.join(PLUGIN_DIR, 'kk_*.rb')).each do |file|
9
9
  # 解析出类名
10
10
  basename = File.basename(file, '.rb')
11
11
  class_name = basename.split('_').map(&:capitalize).join
@@ -6,5 +6,5 @@
6
6
  # https://opensource.org/licenses/MIT
7
7
 
8
8
  module Kscript
9
- VERSION = '1.0.6'
9
+ VERSION = '1.0.7'
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kscript
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kk
@@ -156,23 +156,22 @@ files:
156
156
  - lib/kscript/config.rb
157
157
  - lib/kscript/logger.rb
158
158
  - lib/kscript/plugins.rb
159
- - lib/kscript/plugins/kk_apnic_utils.rb
159
+ - lib/kscript/plugins/kk_apnic_ip_utils.rb
160
160
  - lib/kscript/plugins/kk_aws_s3_utils.rb
161
- - lib/kscript/plugins/kk_cleaner_utils.rb
162
- - lib/kscript/plugins/kk_es_fingerprint_utils.rb
163
- - lib/kscript/plugins/kk_ffmpeg_utils.rb
164
- - lib/kscript/plugins/kk_ip_utils.rb
165
- - lib/kscript/plugins/kk_jenkins_utils.rb
166
- - lib/kscript/plugins/kk_kibana_utils.rb
167
- - lib/kscript/plugins/kk_lvm_utils.rb
168
- - lib/kscript/plugins/kk_optimize_utils.rb
169
- - lib/kscript/plugins/kk_portscan_utils.rb
170
- - lib/kscript/plugins/kk_projscan_utils.rb
171
- - lib/kscript/plugins/kk_rename_utils.rb
172
- - lib/kscript/plugins/kk_sh_utils.rb
173
- - lib/kscript/plugins/kk_syscheck_utils.rb
174
- - lib/kscript/plugins/kk_top_utils.rb
175
- - lib/kscript/plugins/kk_usd_utils.rb
161
+ - lib/kscript/plugins/kk_elastic_cert_finger_utils.rb
162
+ - lib/kscript/plugins/kk_ffmpeg_install_utils.rb
163
+ - lib/kscript/plugins/kk_file_rename_utils.rb
164
+ - lib/kscript/plugins/kk_ip_lookup_utils.rb
165
+ - lib/kscript/plugins/kk_jenkins_manage_utils.rb
166
+ - lib/kscript/plugins/kk_kibana_manage_utils.rb
167
+ - lib/kscript/plugins/kk_lvm_manage_utils.rb
168
+ - lib/kscript/plugins/kk_mac_optimize_utils.rb
169
+ - lib/kscript/plugins/kk_mac_status_utils.rb
170
+ - lib/kscript/plugins/kk_port_scan_utils.rb
171
+ - lib/kscript/plugins/kk_project_scan_utils.rb
172
+ - lib/kscript/plugins/kk_shell_helper_utils.rb
173
+ - lib/kscript/plugins/kk_usd_rate_utils.rb
174
+ - lib/kscript/plugins/kk_vcs_cleaner_utils.rb
176
175
  - lib/kscript/plugins/kk_wg_acl_utils.rb
177
176
  - lib/kscript/plugins/kk_wg_pass_utils.rb
178
177
  - lib/kscript/post_install.rb
@@ -1,71 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright (c) 2025 Kk
4
- #
5
- # This software is released under the MIT License.
6
- # https://opensource.org/licenses/MIT
7
-
8
- require 'kscript'
9
-
10
- module Kscript
11
- class KkTopUtils < Base
12
- def initialize(*args, **opts)
13
- super(*args, **opts)
14
- end
15
-
16
- def run
17
- with_error_handling do
18
- print_report
19
- end
20
- end
21
-
22
- def print_report
23
- logger.kinfo("System Resource Top Report - #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}")
24
- print_header('Top 10 Processes by CPU Usage')
25
- print_process_list(:cpu)
26
- print_header('Top 10 Processes by Memory Usage')
27
- print_process_list(:mem)
28
- end
29
-
30
- def print_header(title)
31
- logger.kinfo('')
32
- logger.kinfo('===============================')
33
- logger.kinfo(" #{title}")
34
- logger.kinfo('===============================')
35
- end
36
-
37
- def print_process_list(sort_field)
38
- lines = `ps aux`.split("\n")
39
- lines.shift
40
- processes = lines.map { |line| line.split(/\s+/, 11) }
41
- index = sort_field == :cpu ? 2 : 3
42
- top = processes.sort_by { |p| -p[index].to_f }.first(10)
43
- logger.kinfo(format('%-10s %-8s %-5s %-5s %-10s', 'USER', 'PID', '%CPU', '%MEM', 'COMMAND'))
44
- top.each do |p|
45
- logger.kinfo(format('%-10s %-8s %-5s %-5s %-10s', p[0], p[1], p[2], p[3], p[10][0..30]))
46
- end
47
- end
48
-
49
- def self.description
50
- 'Show top 10 processes by CPU/memory on macOS.'
51
- end
52
-
53
- def self.arguments
54
- ''
55
- end
56
-
57
- def self.usage
58
- 'kscript top'
59
- end
60
-
61
- def self.group
62
- 'macos'
63
- end
64
-
65
- def self.author
66
- 'kk'
67
- end
68
- end
69
- end
70
-
71
- Kscript::KkTopUtils.new.run if __FILE__ == $PROGRAM_NAME