Class: Baidupan::Cmd::Base

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/baidupan/cmd/base.rb

Direct Known Subclasses

FsCmd

Instance Method Summary collapse

Instance Method Details

#configObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/baidupan/cmd/base.rb', line 28

def config
  url = "https://openapi.baidu.com/oauth/2.0/authorize?response_type=token&client_id=#{Baidupan::Config.api_key}&redirect_uri=oob&scope=netdisk"
  say "请在浏览器中完成授权操作并获取最终成功url!\n将下面的链接粘入浏览器获取access_token"
  say '*'*60
  say url
  say '*'*60
  
  say "将浏览器的url输入到这里:" 
  atoken = STDIN.gets.chomp

  atoken =~ /access_token=([^&]*)/
  atoken = $1 if $1

  raise "Invalid token: #{atoken}!" if atoken !~ /^[\da-f\.\-]*$/
  File.open(Baidupan::CONF_FILE, "a"){|f| f.puts ":access_token: #{atoken}" }

  say '-'*60
  say "Have append access token into file: #{Baidupan::CONF_FILE}"
end

#setup(app_name, api_key, secret_key) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/baidupan/cmd/base.rb', line 12

def setup(app_name, api_key, secret_key)
  require 'erb'
  content = (ERB.new <<-EOF).result(binding)
:app_name: <%=app_name||'<_app_name>'%>
:app_root: /apps/<%=app_name||'<_app_name_or_you_set_in_baidu>'%>
:api_key: <%=api_key||'<_api_key>'%>
:secret_key: <%=secret_key||'<_secret_key>'%>
:base_url:  https://pcs.baidu.com/rest/2.0/pcs
  EOF

  config_path = Baidupan::CONF_FILE
  File.write(config_path, content)
  say "Has wrote #{config_path} for app settings."
end

#show_configObject



50
51
52
53
54
# File 'lib/baidupan/cmd/base.rb', line 50

def show_config
  puts "Using config file: #{Baidupan::CONF_FILE}"
  puts "With content:"
  puts File.read(Baidupan::CONF_FILE)
end