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

检查环境是否配置

Returns:

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

.initObject

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

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


170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/qcloud_cos/cli.rb', line 170

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)
  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]

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


83
84
85
86
87
88
# File 'lib/qcloud_cos/cli.rb', line 83

def info(args, options)
  path = args.shift || '/'
  opts = parse(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/


107
108
109
110
111
112
113
114
115
116
# File 'lib/qcloud_cos/cli.rb', line 107

def list(args, options)
  path = args.shift || '/'
  opts = parse(options)
  opts[:num] = options.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

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 download --bucket bucket2 /data/test/


199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/qcloud_cos/cli.rb', line 199

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)

  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/


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, 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?('/')

  opts = parse(options)
  opts[:slice_size] = options.size || QcloudCos::DEFAULT_SLICE_SIZE
  opts[:min] = options.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