lhj-tools 0.1.97 → 0.1.99

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3141a25ce2e0c2b88124dd148417f856317982f1f4da81b9bd43c90ee858b820
4
- data.tar.gz: 70f7ffc027cbcb03cb1c4b613a2e132cd46e57dbd2833d36ff88aacc395ed636
3
+ metadata.gz: 9c1cb1e36ca0a061c4d3e53f5c0af5c0fb43f5c3dbb044fdd512c44f806a585d
4
+ data.tar.gz: 1ea501e7dde6f267da843ba2aa0f304e2ec038d0ed16f9a4b7348988d982ffab
5
5
  SHA512:
6
- metadata.gz: ab33b726b8306c881f3ecbced49a801d1d217cb7a340d88b759e82a4d283f2c18f0db89584296edb8e00ca9abb3c8d212ea82d1ee3cf423b56637b507fa9ba14
7
- data.tar.gz: b172f1fbe29b4475b777a30dd646d0e01bf83015406cfeb22a161010edb2f62a299cee0db36a7a3a2ee9e4ab40b3583f2a5317da749ef0d1b96bc826b837ff4b
6
+ metadata.gz: 04faa27361e9e260282b8ed9eaaec6d11336c7f5b4af80c069fc6f37956a485a178880ce6397aca1452d14fad32a47059b699bed3e6a67b75e98a5e57bc0921a
7
+ data.tar.gz: 6411de6c950b82eafcf5727c57cbd758a6d3350c1682171ba4bc589ad0ddde95b4a53344cf21b03148834af823f35f27bc0ce98585bfb682d744594257aa5ea8
@@ -14,15 +14,19 @@ module Lhj
14
14
 
15
15
  def handle
16
16
  objects = Lhj::OSS::Helper.instance.list
17
- obj_keys = []
18
- objects.each_with_index do |o, i|
19
- obj_keys << o.key
20
- puts "#{i}.#{o.key}".yellow
17
+ obj_keys = objects.map(&:key)
18
+ obj_keys.each_with_index { |k, i| puts "#{i}.#{k}".yellow }
19
+ idx = @cli.ask('请选择删除的key: '.green)
20
+ idx_arr = idx.split(',').map(&:strip).filter { |v| v.length.positive? }
21
+ result_keys = []
22
+ idx_arr.each do |i|
23
+ ma = /(?<begin>[0-9]*)\D*-\D*(?<end>[0-9]*)/.match(i)
24
+ result_keys << obj_keys[ma[:begin].to_i..ma[:end].to_i] if ma
25
+ result_keys << obj_keys[i.to_i] unless ma
21
26
  end
22
- idx = @cli.ask('请选择删除的key: '.green).strip.to_i
23
- oss_key = obj_keys[idx]
24
- puts "删除的key为:#{oss_key}".red
25
- Lhj::OSS::Helper.instance.delete(oss_key)
27
+ oss_keys = result_keys.flatten
28
+ oss_keys.each { |k| puts "删除的key为:#{k}".red }
29
+ Lhj::OSS::Helper.instance.batch_delete(oss_keys) if oss_keys.length > 0
26
30
  end
27
31
 
28
32
  end
@@ -8,30 +8,62 @@ module Lhj
8
8
  class OSS < Command
9
9
  class Download < OSS
10
10
 
11
+ def self.options
12
+ [
13
+ %w[--oss_key_file 本地下载的oss_key文件]
14
+ ]
15
+ end
16
+
11
17
  def initialize(argv)
12
18
  @current_path = argv.shift_argument || Dir.pwd
13
19
  @cli = HighLine.new
20
+ @oss_key_file = argv.option('oss_key_file')
14
21
  super
15
22
  end
16
23
 
17
24
  def handle
18
- objects = Lhj::OSS::Helper.instance.list
19
- obj_keys = []
20
- objects.each_with_index do |o, i|
21
- obj_keys << o.key
22
- puts "#{i}.#{o.key}".yellow
25
+ oss_keys = local_oss_keys if @oss_key_file
26
+ oss_keys ||= remote_oss_keys
27
+ Dir.chdir(@current_path) do
28
+ oss_keys.each { |k| download_with_key(k) }
23
29
  end
24
- idx = @cli.ask('请选择下载的序号: '.green).strip.to_i
25
- oss_key = obj_keys[idx]
26
- puts "下载的key为:#{oss_key}".yellow
30
+ end
31
+
32
+ def local_oss_keys
33
+ key_file = File.join(@current_path, @oss_key_file)
34
+ oss_keys = []
35
+ oss_keys = YAML.load_file(key_file) if File.exist?(key_file)
36
+ oss_keys
37
+ end
27
38
 
39
+ def remote_oss_keys
40
+ obj_keys = request_oss_keys
41
+ idx = @cli.ask('请选择下载的序号: '.green)
42
+ idx_arr = idx.split(',').map(&:strip).filter { |v| v.length.positive? }
43
+ result_keys = []
44
+ idx_arr.each do |i|
45
+ ma = /(?<begin>[0-9]*)\D*-\D*(?<end>[0-9]*)/.match(i)
46
+ result_keys << obj_keys[ma[:begin].to_i..ma[:end].to_i] if ma
47
+ result_keys << obj_keys[i.to_i] unless ma
48
+ end
49
+ result_keys.flatten
50
+ end
51
+
52
+ def request_oss_keys
53
+ objects = Lhj::OSS::Helper.instance.list
54
+ obj_keys = objects.map(&:key)
55
+ obj_keys.each_with_index { |k, i| puts "#{i}.#{k}".yellow }
56
+ end
57
+
58
+ def download_with_key(oss_key)
59
+ puts "下载的key为:#{oss_key}".yellow
28
60
  file_key = oss_key.split('/').last
29
61
  file = File.join(@current_path, file_key)
62
+ File.rename(file_key, "#{file_key}.bak") if File.exist?(file_key)
30
63
  Lhj::OSS::Helper.instance.down_load(oss_key, file)
31
64
  puts "下载文件到目录: #{file}\n".yellow
32
65
  end
33
-
34
66
  end
35
67
  end
36
68
  end
37
- end
69
+ end
@@ -0,0 +1,33 @@
1
+ require 'lhj/helper/oss_helper'
2
+ require 'terminal-table'
3
+
4
+ module Lhj
5
+ class Command
6
+ class OSS < Command
7
+ class Info < OSS
8
+
9
+ def handle
10
+ puts '-------base-----------'
11
+ base_info
12
+ puts '-------alc-----------'
13
+ puts Lhj::OSS::Helper.instance.alc
14
+ puts '-------logging-----------'
15
+ puts Lhj::OSS::Helper.instance.logging
16
+ puts '-------lifecycle----------'
17
+ puts Lhj::OSS::Helper.instance.lifecycle
18
+ puts '-------cors-----------'
19
+ puts Lhj::OSS::Helper.instance.cors
20
+ puts '--------website----------'
21
+ puts Lhj::OSS::Helper.instance.website
22
+ puts '--------referer----------'
23
+ puts Lhj::OSS::Helper.instance.referer
24
+ end
25
+
26
+ def base_info
27
+ puts Lhj::OSSConfig.oss_endpoint
28
+ puts Lhj::OSSConfig.oss_bucket
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -6,11 +6,39 @@ module Lhj
6
6
  class OSS < Command
7
7
  class List < OSS
8
8
 
9
+ def self.options
10
+ [
11
+ %w[--save 保存文件],
12
+ %w[--clear 清空控制台输出]
13
+ ]
14
+ end
15
+
16
+ def initialize(argv)
17
+ @save = argv.flag?('save', false)
18
+ @clear = argv.flag?('clear', false)
19
+ @current_path = argv.shift_argument || Dir.pwd
20
+ super
21
+ end
22
+
9
23
  def handle
10
24
  objects = Lhj::OSS::Helper.instance.list
25
+ obj_keys = objects.map(&:key)
26
+ save(obj_keys) if @save
27
+ print(obj_keys) unless @clear
28
+ end
29
+
30
+ def save(obj_keys)
31
+ obj_urls = obj_keys.map { |k| "#{Lhj::OSS::Helper.instance.url_path}/#{k}" }
32
+ FileUtils.chdir(@current_path) do
33
+ File.write('oss_key.yml', obj_keys.to_yaml)
34
+ File.write('oss_url.yml', obj_urls.to_yaml)
35
+ end
36
+ end
37
+
38
+ def print(obj_keys)
11
39
  rows = []
12
- objects.each_with_index do |o, i|
13
- path = "#{i}.#{Lhj::OSS::Helper.instance.url_path}/#{o.key}"
40
+ obj_keys.each_with_index do |k, i|
41
+ path = "#{i}.#{Lhj::OSS::Helper.instance.url_path}/#{k}"
14
42
  rows << [path]
15
43
  end
16
44
  table = Terminal::Table.new title: 'OSS List', headings: ['URL'], rows: rows
@@ -0,0 +1,21 @@
1
+ require 'lhj/helper/oss_helper'
2
+
3
+ # lhj oss list-bucket
4
+ module Lhj
5
+ class Command
6
+ class OSS < Command
7
+ class ListBucket < OSS
8
+ def handle
9
+ helper = Lhj::OSS::Helper.instance
10
+ objects = helper.list_bucket
11
+ bucket_list = []
12
+ objects.each do |o|
13
+ bucket_list << { 'name' => o.name, 'location' => o.location, 'creation_time' => o.creation_time }
14
+ end
15
+ print_obj = { 'oss_bucket_list' => bucket_list }
16
+ puts print_obj.to_yaml
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ require 'lhj/helper/oss_helper'
2
+
3
+ # lhj oss update-bucket
4
+ module Lhj
5
+ class Command
6
+ class OSS < Command
7
+ class UpdateBucket < OSS
8
+
9
+ def initialize(argv)
10
+ @cli = HighLine.new
11
+ super
12
+ end
13
+
14
+ def handle
15
+ bucket_list = Lhj::OSSConfig.oss_bucket_list
16
+ return unless bucket_list.length.positive?
17
+
18
+ bucket_list.each_with_index { |k, i| puts "#{i}.#{k['name']}----#{k['location']}".yellow }
19
+ idx = @cli.ask('更新序号: '.green).strip.to_i
20
+ bucket = bucket_list[idx]
21
+ Lhj::OSSConfig.oss_endpoint = "#{bucket['location']}.aliyuncs.com"
22
+ Lhj::OSSConfig.oss_bucket = bucket['name']
23
+ Lhj::OSSConfig.save
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -22,33 +22,44 @@ module Lhj
22
22
  def initialize(argv)
23
23
  @current_path = argv.shift_argument || Dir.pwd
24
24
  @tag = argv.option('tag')
25
+ @oss_key = argv.option('oss_key')
25
26
  @cli = HighLine.new
26
27
  super
27
28
  end
28
29
 
29
30
  def handle
30
31
  file_list = []
31
- file_name_list = []
32
32
  Dir.glob("#{@current_path}/*").each_with_index do |f, i|
33
- file_base_name = File.basename(f)
34
33
  file_list << f
35
- file_name_list << file_base_name
36
- puts "#{i}.#{file_base_name}".yellow
34
+ puts "#{i}.#{File.basename(f)}".yellow
35
+ end
36
+ idx = @cli.ask('请选择上传文件序号: '.green)
37
+ idx_arr = idx.split(',').map(&:strip).filter { |v| v.length.positive? }
38
+ results = []
39
+ idx_arr.each do |i|
40
+ ma = /(?<begin>[0-9]*)\D*-\D*(?<end>[0-9]*)/.match(i)
41
+ results << file_list[ma[:begin].to_i..ma[:end].to_i] if ma
42
+ results << file_list[i.to_i] unless ma
43
+ end
44
+ results.flatten.each do |f|
45
+ file_name = File.basename(f)
46
+ puts "上传的文件名:#{file_name}".yellow
47
+ upload(f)
37
48
  end
38
- idx = @cli.ask('请选择上传文件序号: '.green).strip.to_i
39
- puts "上传的文件名:#{file_name_list[idx]}".yellow
40
- upload(file_list[idx], file_name_list[idx])
41
49
  end
42
50
 
43
51
  private
44
52
 
45
- def upload(file, file_name)
53
+ def upload(file)
54
+ file_name = File.basename(file)
55
+ file_name = @oss_key if @oss_key
46
56
  oss_key = file_name
47
57
  oss_key = "#{@tag}/#{file_name}" if @tag
48
58
  Lhj::OSS::Helper.instance.upload(oss_key, file)
49
59
  url = Lhj::OSS::Helper.instance.object_url(oss_key)
50
60
  puts "云端上传成功:#{url}\n".yellow
51
61
  end
62
+
52
63
  end
53
64
  end
54
65
  end
data/lib/lhj/command.rb CHANGED
@@ -17,6 +17,9 @@ module Lhj
17
17
  require 'lhj/command/oss/del'
18
18
  require 'lhj/command/oss/upload'
19
19
  require 'lhj/command/oss/list'
20
+ require 'lhj/command/oss/info'
21
+ require 'lhj/command/oss/list_bucket'
22
+ require 'lhj/command/oss/update_bucket'
20
23
  require 'lhj/command/oss/download'
21
24
  require 'lhj/command/code/view'
22
25
  require 'lhj/command/code/code_template'
@@ -10,26 +10,26 @@ module Lhj
10
10
  post_message_robot(robot_url, title, message)
11
11
  end
12
12
 
13
- def self.post_message_robot(robot_url, title, message)
14
- http_body = markdown_body_message(title, message)
15
- http_post(robot_url, http_body)
16
- end
17
-
18
13
  def self.post_card_message(title, message, btns)
19
14
  robot_url = Lhj::DingTalkConfig.dingtalk_robot
20
15
  post_card_message_robot(robot_url, title, message, btns)
21
16
  end
22
17
 
23
- def self.post_card_message_robot(robot_url, title, message, btns)
24
- http_body = action_card_body_message(title, message, btns, :vertical)
25
- http_post(robot_url, http_body)
26
- end
27
-
28
18
  def self.post_single_card_message(title, message, single_title, single_url)
29
19
  robot_url = Lhj::DingTalkConfig.dingtalk_robot
30
20
  post_single_card_message_robot(robot_url, title, message, single_title, single_url)
31
21
  end
32
22
 
23
+ def self.post_message_robot(robot_url, title, message)
24
+ http_body = markdown_body_message(title, message)
25
+ http_post(robot_url, http_body)
26
+ end
27
+
28
+ def self.post_card_message_robot(robot_url, title, message, btns)
29
+ http_body = action_card_body_message(title, message, btns, :vertical)
30
+ http_post(robot_url, http_body)
31
+ end
32
+
33
33
  def self.post_single_card_message_robot(robot_url, title, message, single_title, single_url)
34
34
  http_body = single_action_card_body_message(title, message, single_title, single_url)
35
35
  http_post(robot_url, http_body)
@@ -17,6 +17,10 @@ module Lhj
17
17
  config['oss_endpoint']
18
18
  end
19
19
 
20
+ def self.oss_endpoint=(endpoint)
21
+ config['oss_endpoint'] = endpoint
22
+ end
23
+
20
24
  def self.oss_access_key_id
21
25
  config['oss_access_key_id']
22
26
  end
@@ -28,5 +32,19 @@ module Lhj
28
32
  def self.oss_bucket
29
33
  config['oss_bucket']
30
34
  end
35
+
36
+ def self.oss_bucket=(bucket)
37
+ config['oss_bucket'] = bucket
38
+ end
39
+
40
+ def self.oss_bucket_list
41
+ config['oss_bucket_list']
42
+ end
43
+
44
+ def self.save
45
+ file_to_save = File.open(config_file, 'w+')
46
+ YAML.dump(config, file_to_save)
47
+ file_to_save.close
48
+ end
31
49
  end
32
50
  end
@@ -11,6 +11,10 @@ module Lhj
11
11
  @bucket = @client.get_bucket(Lhj::OSSConfig.oss_bucket)
12
12
  end
13
13
 
14
+ def get_bucket(name)
15
+ @bucket = @client.get_bucket(name)
16
+ end
17
+
14
18
  def url_path
15
19
  "http://#{Lhj::OSSConfig.oss_bucket}.#{Lhj::OSSConfig.oss_endpoint}"
16
20
  end
@@ -41,10 +45,58 @@ module Lhj
41
45
  @bucket.list_objects
42
46
  end
43
47
 
48
+ # {bucket.name} #{bucket.location}
49
+ def list_bucket
50
+ @client.list_buckets
51
+ end
52
+
44
53
  def delete(key)
45
54
  @bucket.delete_object(key)
46
55
  end
47
56
 
57
+ def batch_delete(keys)
58
+ @bucket.batch_delete_objects(keys)
59
+ end
60
+
61
+ def acl=(acl)
62
+ case acl
63
+ when :public_read_write
64
+ @bucket.acl = (Aliyun::OSS::ACL::PUBLIC_READ_WRITE)
65
+ when :public_read
66
+ @bucket.acl = (Aliyun::OSS::ACL::PUBLIC_READ)
67
+ when :private
68
+ @bucket.acl = (Aliyun::OSS::ACL::PRIVATE)
69
+ end
70
+ end
71
+
72
+ def set_object_acl(key, acl)
73
+ @bucket.set_object_acl(key, acl)
74
+ end
75
+
76
+ def alc
77
+ @bucket.acl
78
+ end
79
+
80
+ def logging
81
+ @bucket.logging
82
+ end
83
+
84
+ def lifecycle
85
+ @bucket.lifecycle
86
+ end
87
+
88
+ def cors
89
+ @bucket.cors
90
+ end
91
+
92
+ def website
93
+ @bucket.website
94
+ end
95
+
96
+ def referer
97
+ @bucket.referer
98
+ end
99
+
48
100
  def self.instance
49
101
  @instance ||= new
50
102
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lhj
4
4
  module Tools
5
- VERSION = "0.1.97"
5
+ VERSION = "0.1.99"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhj-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.97
4
+ version: 0.1.99
5
5
  platform: ruby
6
6
  authors:
7
7
  - lihaijian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-15 00:00:00.000000000 Z
11
+ date: 2023-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xcodeproj
@@ -348,7 +348,10 @@ files:
348
348
  - lib/lhj/command/oss.rb
349
349
  - lib/lhj/command/oss/del.rb
350
350
  - lib/lhj/command/oss/download.rb
351
+ - lib/lhj/command/oss/info.rb
351
352
  - lib/lhj/command/oss/list.rb
353
+ - lib/lhj/command/oss/list_bucket.rb
354
+ - lib/lhj/command/oss/update_bucket.rb
352
355
  - lib/lhj/command/oss/upload.rb
353
356
  - lib/lhj/command/pgyer_upload.rb
354
357
  - lib/lhj/command/refactor_rename.rb