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 42 43 44 45 46 47 48 |
# File 'lib/qcloud_cos/cli.rb', line 23 def self.config(config_path = nil) config_path = 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
检查环境是否配置
53 54 55 56 57 |
# File 'lib/qcloud_cos/cli.rb', line 53 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
60 61 62 63 64 65 66 |
# File 'lib/qcloud_cos/cli.rb', line 60 def self.init QcloudCos.configure do |config| load_config.each { |k, v| config.send("#{k}=", v) } end self.new end |
Instance Method Details
#download(args, options) ⇒ Object
下载文件或者目录命令: $ qcloud-cos download [options] dest_path [save_path]
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/qcloud_cos/cli.rb', line 170 def download(args, ) path = args.shift return Commander::UI.say_error("missing path, see example: $ qcloud-cos download -h") unless path opts = parse() opts[:save_path] = args.shift || '.' if path.end_with?('/') download_folder(path, opts) else download_file(path, opts) end end |
#info(args, options) ⇒ Object
查看信息命令: $ qcloud-cos info [options] [dest_path]
83 84 85 86 87 88 |
# File 'lib/qcloud_cos/cli.rb', line 83 def info(args, ) path = args.shift || '/' opts = parse() QcloudCos.stat(path, opts)['data'] end |
#list(args, options) ⇒ Object
列出文件或者文件夹使用: $ qcloud-cos list [options] [dest_path]
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/qcloud_cos/cli.rb', line 107 def list(args, ) path = args.shift || '/' opts = parse() opts[:num] = .num || 100 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
199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/qcloud_cos/cli.rb', line 199 def remove(args, ) path = args.shift return Commander::UI.say_error("missing dest_path, see example: $ qcloud-cos remove -h") unless path opts = parse() 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]
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/qcloud_cos/cli.rb', line 134 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?('/') opts = parse() opts[:slice_size] = .size || QcloudCos::DEFAULT_SLICE_SIZE opts[:min] = .min || QcloudCos::MIN_SLICE_FILE_SIZE * 1024 * 1024 if path.end_with?('/') upload_folder(path, dest_path, opts) else upload_file(path, dest_path, opts) end end |