-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_example.rb
197 lines (165 loc) · 5.72 KB
/
cli_example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require './lib/dropbox_sdk'
require 'pp'
####
# An example app using the Dropbox API Ruby Client
# This ruby script sets up a basic command line interface (CLI)
# that prompts a user to authenticate on the web, then
# allows them to type commands to manipulate their dropbox.
####
# You must use your Dropbox App key and secret to use the API.
# Find this at https://www.dropbox.com/developers
APP_KEY = ""
APP_SECRET = ""
ACCESS_TYPE = :app_folder #The two valid values here are :app_folder and :dropbox
#The default is :app_folder, but your application might be
#set to have full :dropbox access. Check your app at
#https://www.dropbox.com/developers/apps
class DropboxCLI
LOGIN_REQUIRED = %w{put get cp mv rm ls mkdir info logout search}
def initialize
if APP_KEY == '' or APP_SECRET == ''
puts "You must set your APP_KEY and APP_SECRET in cli_example.rb!"
puts "Find this in your apps page at https://www.dropbox.com/developers/"
exit
end
@session = DropboxSession.new(APP_KEY, APP_SECRET)
@client = nil
end
def login
########
# Instead of going to a authorize URL, you can set a access token key and secret
# from a previous session
########
# @session.set_access_token('key', 'secret')
if @session.authorized?
puts "already logged in!"
else
# grab the request token for session
@session.get_request_token
authorize_url = @session.get_authorize_url
puts "Got a request token. Your request token key is #{@session.token} and your token secret is #{@session.secret}"
# make the user log in and authorize this token
puts "AUTHORIZING", authorize_url, "Please visit that web page and hit 'Allow', then hit Enter here."
gets
# get the access token from the server. Its then stored in the session.
@session.get_access_token
end
puts "You are logged in. Your access token key is #{@session.token} your secret is #{@session.secret}"
@client = DropboxClient.new(@session, ACCESS_TYPE)
end
def command_loop
puts "Enter a command or 'help' or 'exit'"
command_line = ''
while command_line.strip != 'exit'
begin
execute_dropbox_command(command_line)
rescue RuntimeError => e
puts "Command Line Error! #{e.class}: #{e}"
puts e.backtrace
end
print '> '
command_line = gets.strip
end
puts 'goodbye'
exit(0)
end
def execute_dropbox_command(cmd_line)
command = cmd_line.split
method = command.first
if LOGIN_REQUIRED.include? method
if @client
send(method.to_sym, command)
else
puts 'must be logged in; type \'login\' to get started.'
end
elsif ['login', 'help'].include? method
send(method.to_sym)
else
if command.first && !command.first.strip.empty?
puts 'invalid command. type \'help\' to see commands.'
end
end
end
def logout
@session.clear_access_token
puts "You are logged out."
@client = nil
end
def put(command)
fname = command[1]
#If the user didn't specifiy the file name, just use the name of the file on disk
if command[2]
new_name = command[2]
else
new_name = File.basename(fname)
end
if fname && !fname.empty? && File.exists?(fname) && (File.ftype(fname) == 'file') && File.stat(fname).readable?
#This is where we call the the Dropbox Client
pp @client.put_file(new_name, open(fname))
else
puts "couldn't find the file #{ fname }"
end
end
def get(command)
dest = command[2]
if !command[1] || command[1].empty?
puts "please specify item to get"
elsif !dest || dest.empty?
puts "please specify full local path to dest, i.e. the file to write to"
elsif File.exists?(dest)
puts "error: File #{dest} already exists."
else
src = clean_up(command[1])
out = @client.get_file('/' + src)
open(dest, 'w'){|f| f.puts out }
puts "wrote file #{dest}."
end
end
def mkdir(command)
pp @client.file_create_folder(command[1])
end
def thumbnail(command)
command[2] ||= 'small'
pp @client.thumbnail(command[1], command[2])
end
def cp(command)
src = clean_up(command[1])
dest = clean_up(command[2])
pp @client.file_copy(src, dest)
end
def mv(command)
src = clean_up(command[1])
dest = clean_up(command[2])
pp @client.file_move(src, dest)
end
def rm(command)
pp @client.file_delete(clean_up(command[1]))
end
def search(command)
resp = @client.search('/',clean_up(command[1]))
for item in resp
puts item['path']
end
end
def info(command)
pp @client.account_info
end
def ls(command)
command[1] = '/' + clean_up(command[1] || '')
resp = @client.metadata(command[1])
if resp['contents'].length > 0
for item in resp['contents']
puts item['path']
end
end
end
def help
puts "commands are: login #{LOGIN_REQUIRED.join(' ')} help exit"
end
def clean_up(str)
return str.gsub(/^\/+/, '') if str
str
end
end
cli = DropboxCLI.new
cli.command_loop