Class: QcloudCos::Cli

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

检查环境是否配置

Returns:

  • (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

.initObject

检查环境,初始化 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]

Examples:


# 把 /data/production.log 下载到当前目录
qcloud-cos download /data/production.log

# 把 /data/production.log 下载到 ./data/ 下
qcloud-cos download /data/production.log ./data

# 把 /data/test/ 整个目录下载并保存到 ./data/ 目录下面
qcloud-cos download /data/test/ ./data

# 把 bucket2 下的 /data/test/ 整个目录下载并保存到 ./data/ 下面
qcloud-cos download --bucket bucket2 /data/test/ ./data


158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/qcloud_cos/cli.rb', line 158

def download(args, options)
  path = args.shift
  return Commander::UI.say_error('missing path, see example: $ qcloud-cos download -h') unless path
  opts = parse_options(options)
  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]

Examples:

# 查看 Bucket 信息
qcloud-cos info

# 查看 /production.log 信息
qcloud-cos info /production.log

# 查看 /test/ 信息
qcloud-cos info /test/

# 查看 bucket2 上的 /production.log 信息
qcloud-cos info --bucket bucket2 /production.log


76
77
78
79
80
81
# File 'lib/qcloud_cos/cli.rb', line 76

def info(args, options)
  path = args.shift || '/'
  opts = parse_options(options)

  QcloudCos.stat(path, opts)['data']
end

#list(args, options) ⇒ Object

列出文件或者文件夹使用: $ qcloud-cos list [options] [dest_path]

Examples:


# 列出 / 下面的所有对象
qcloud-cos list

# 列出 /test/ 下面的所有对象
qcloud-cos list /test/

# 列出 /test/ 下面的前 10 个对象
qcloud-cos list --num 10 /test/

# 列出 bucket2 的 /test/ 下面的所有对象
qcloud-cos list --bucket bucket2 /test/


100
101
102
103
104
105
106
107
108
# File 'lib/qcloud_cos/cli.rb', line 100

def list(args, options)
  path = args.shift || '/'
  opts = parse_options(options)

  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

Examples:


# 删除文件/data/production.log
qcloud-cos remove /data/production.log

# 删除目录 /data/test/, 目录非空会失败
qcloud-cos remove /data/test/

# 级联删除目录 /data/test/
qcloud-cos remove --recursive /data/test/

# 删除 bucket2 下面的目录 /data/test/
qcloud-cos remove --bucket bucket2 /data/test/


187
188
189
190
191
192
193
194
195
196
197
# File 'lib/qcloud_cos/cli.rb', line 187

def remove(args, options)
  path = args.shift
  return Commander::UI.say_error('missing dest_path, see example: $ qcloud-cos remove -h') unless path
  opts = parse_options(options)

  if path.end_with?('/')
    QcloudCos.delete_folder(path, opts.merge(recursive: !!options.recursive))
  else
    QcloudCos.delete_file(path, opts)
  end
end

#upload(args, options) ⇒ Object

上传文件或者目录到 COS 命令: qcloud-cos upload [options] file [dest_path]

Examples:


# 把 production.log 上传到 /
qcloud-cos upload production.log

# 把 production.log 上传到 /data/ 下面
qcloud-cos upload production.log /data/

# 把 ./test/ 整个文件夹上传到 /data/ 下面
qcloud-cos upload test/ /data/

# 把 ./test/ 整个文件夹上传到 bucket2 的 /data/ 下面
qcloud-cos upload --bucket bucket2 test/ /data/


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, options)
  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, parse_options(options))
  else
    upload_file(path, dest_path, parse_options(options))
  end
end