Class: QcloudCos::Cli
- Inherits:
-
Object
- Object
- QcloudCos::Cli
- Includes:
- Commander::Methods
- Defined in:
- lib/qcloud_cos/cli.rb
Constant Summary collapse
- QCLOUD_COS_ENV_MAPPING =
{ app_id: 'QCLOUD_COS_APP_ID', secret_id: 'QCLOUD_COS_SECRET_ID', secret_key: 'QCLOUD_COS_SECRET_KEY', endpoint: 'QCLOUD_COS_ENDPOINT', bucket: 'QCLOUD_COS_BUCKET', ssl_ca_file: 'QCLOUD_COS_SSL_CA_FILE', max_retry_times: 'QCLOUD_COS_MAX_RETRY_TIMES' }
Class Method Summary collapse
-
.config(config_path = nil) ⇒ Object
交互模式配置环境 命令: $ qcloud-cos config.
-
.environment_configed? ⇒ Boolean
检查环境是否配置.
-
.init ⇒ Object
检查环境,初始化 Cli.
Instance Method Summary collapse
-
#download(args, options) ⇒ Object
下载文件或者目录 命令: $ qcloud-cos download [options] dest_path [save_path].
-
#info(args, options) ⇒ Object
查看信息 命令: $ qcloud-cos info [options] [dest_path].
-
#list(args, options) ⇒ Object
列出文件或者文件夹 使用: $ qcloud-cos list [options] [dest_path].
-
#remove(args, options) ⇒ Object
删除目录或者文件夹 命令: $ qcloud-cos remove [options] dest_path.
-
#upload(args, options) ⇒ Object
上传文件或者目录到 COS 命令: qcloud-cos upload [options] file [dest_path].
Class Method Details
.config(config_path = nil) ⇒ Object
交互模式配置环境命令: $ qcloud-cos config
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/qcloud_cos/cli.rb', line 23 def self.config(config_path = nil) config_path ||= QcloudCos::QCLOUD_COS_CONFIG return Commander::UI.say_error("#{config_path} already exist, remove it first or direct edit it!") if File.exist?(config_path) app_id = Commander::UI.ask 'Qcloud COS APP ID: ' return Commander::UI.say_error('Missing Qcloud COS APP ID') if app_id.empty? secret_id = Commander::UI.ask 'Qcloud COS Secret ID: ' return Commander::UI.say_error('Missing Qcloud COS Secret ID') if secret_id.empty? secret_key = Commander::UI.ask 'Qcloud COS Secret Key: ' return Commander::UI.say_error('Missing Qcloud COS Secret Key') if secret_key.empty? endpoint = Commander::UI.ask "Default Qcloud COS Endpoint [#{QcloudCos::DEFAULT_ENDPOINT}]: " endpoint = QcloudCos::DEFAULT_ENDPOINT if endpoint.empty? bucket = Commander::UI.ask 'Default Qcloud COS Bucket: ' write_config(config_path, app_id: app_id, secret_id: secret_id, secret_key: secret_key, endpoint: endpoint, bucket: bucket) end |
.environment_configed? ⇒ Boolean
检查环境是否配置
46 47 48 49 50 |
# File 'lib/qcloud_cos/cli.rb', line 46 def self.environment_configed? configed = File.exist?(QcloudCos::QCLOUD_COS_CONFIG) || !ENV['QCLOUD_COS_APP_ID'].to_s.empty? Commander::UI.say_error('Use `qcloud-cos config` first or export your environments') unless configed configed end |
.init ⇒ Object
检查环境,初始化 Cli
53 54 55 56 57 58 59 |
# File 'lib/qcloud_cos/cli.rb', line 53 def self.init QcloudCos.configure do |config| load_config.each { |k, v| config.send("#{k}=", v) } end new end |
Instance Method Details
#download(args, options) ⇒ Object
下载文件或者目录命令: $ qcloud-cos download [options] dest_path [save_path]
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/qcloud_cos/cli.rb', line 158 def download(args, ) path = args.shift return Commander::UI.say_error('missing path, see example: $ qcloud-cos download -h') unless path opts = () save_path = args.shift || '.' if path.end_with?('/') download_folder(path, save_path, opts) else download_file(path, save_path, opts) end end |
#info(args, options) ⇒ Object
查看信息命令: $ qcloud-cos info [options] [dest_path]
76 77 78 79 80 81 |
# File 'lib/qcloud_cos/cli.rb', line 76 def info(args, ) path = args.shift || '/' opts = () QcloudCos.stat(path, opts)['data'] end |
#list(args, options) ⇒ Object
列出文件或者文件夹使用: $ qcloud-cos list [options] [dest_path]
100 101 102 103 104 105 106 107 108 |
# File 'lib/qcloud_cos/cli.rb', line 100 def list(args, ) path = args.shift || '/' opts = () objects = QcloudCos.list(path, opts) objects.map do |object| File.join(path, object.is_a?(QcloudCos::FolderObject) ? "#{object.name}/" : object.name) end end |
#remove(args, options) ⇒ Object
删除目录或者文件夹命令: $ qcloud-cos remove [options] dest_path
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/qcloud_cos/cli.rb', line 187 def remove(args, ) path = args.shift return Commander::UI.say_error('missing dest_path, see example: $ qcloud-cos remove -h') unless path opts = () if path.end_with?('/') QcloudCos.delete_folder(path, opts.merge(recursive: !!.recursive)) else QcloudCos.delete_file(path, opts) end end |
#upload(args, options) ⇒ Object
上传文件或者目录到 COS 命令: qcloud-cos upload [options] file [dest_path]
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/qcloud_cos/cli.rb', line 126 def upload(args, ) path = args.shift return Commander::UI.say_error('file missing, see example: $ qcloud-cos upload -h') unless path return Commander::UI.say_error("file #{path} not exist") unless File.exist?(path) dest_path = args.shift || '/' return Commander::UI.say_error('dest_path must end with /, see example: $ qcloud-cos upload -h') unless dest_path.end_with?('/') if path.end_with?('/') upload_folder(path, dest_path, ()) else upload_file(path, dest_path, ()) end end |