-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcloudapp
More file actions
executable file
·152 lines (139 loc) · 3.47 KB
/
cloudapp
File metadata and controls
executable file
·152 lines (139 loc) · 3.47 KB
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
#!/usr/bin/env ruby -rubygems
# -*- coding: utf-8 -*-
def usage
puts 'Configuration:'
puts ' cloudapp config [USERNAME] [PASSWORD]'
puts
puts 'Example usage:'
puts ' cloudapp list'
puts ' cloudapp stats'
puts ' cloudapp upload [-p] FILE...'
puts ' cloudapp download SLUG...'
puts ' cloudapp delete SLUG...'
puts ' cloudapp recover SLUG...'
puts ' cloudapp rename SLUG NAME'
puts ' cloudapp private [SLUG...]'
puts ' cloudapp public [SLUG...]'
puts ' cloudapp home'
puts ' cloudapp help'
puts
exit 0
end
CONFIG_FILE = ENV['HOME'] + '/.cloudapp-cli'
def load_config
require 'yaml'
YAML.load_file(CONFIG_FILE) rescue abort 'Please set up your user account first.'
end
def save_config(config)
require 'yaml'
File.open(CONFIG_FILE, 'w+') do |f|
f.write(YAML.dump(config))
end
end
case ARGV.first
when 'help', nil
usage
when 'home'
exec 'open', 'http://my.cl.ly/'
when 'config'
if ARGV[1] and ARGV[2]
save_config(:username => ARGV[1], :password => ARGV[2])
puts 'User account set.'
else
load_config.each do |k, v|
puts "#{k}: #{v}"
end
end
exit 0
end
config = load_config
require 'cloudapp_api'
@client = CloudApp::Client.new
@client.authenticate config[:username], config[:password]
begin
CloudApp::Account.find
rescue SocketError
abort 'Wooops, an error occured, check your network settings.'
rescue CloudApp::GenericError
abort 'Wooops, an error occured, check your credentials.'
end
def slug(drop)
drop.url.split(/\//).last if drop
end
def args?(n=2)
usage if ARGV.length < n
end
begin
case ARGV.first
when 'list'
CloudApp::Drop.all.each do |drop|
puts "#{slug(drop)}\t#{drop.content_url}"
end
when 'stats'
CloudApp::Account.stats.each do |key, value|
puts "#{key}\t#{value}"
end
when 'upload'
p = ARGV.delete('-p')
args?
ARGV[1..-1].each do |file|
drop = @client.upload file, :private => (not p.nil?)
puts "uploaded #{file} as #{slug(drop)}"
end
when 'private'
if ARGV[1]
ARGV[1..-1].each do |slug|
drop = @client.privacy slug, true
puts "#{drop.content_url} changed to private"
end
else
CloudApp::Account.update :private_items => true
puts 'all drops changed to private'
end
when 'public'
if ARGV[1]
ARGV[1..-1].each do |slug|
drop = @client.privacy slug, false
puts "#{drop.content_url} changed to public"
end
else
CloudApp::Account.update :private_items => false
puts 'all drops changed to public'
end
when 'delete'
args?
ARGV[1..-1].each do |slug|
drop = @client.delete slug
puts "deleted #{drop.content_url}"
end
when 'recover'
args?
ARGV[1..-1].each do |slug|
drop = @client.recover slug
puts "recovered #{drop.content_url}"
end
when 'rename'
args? 3
drop = @client.rename ARGV[1], ARGV[2]
puts "#{ARGV[1]} renamed to #{ARGV[2]}"
when 'download'
require 'httparty'
args?
ARGV[1..-1].each do |slug|
drop = @client.drop slug
File.open(drop.name, 'w') do |f|
f << HTTParty.get("#{drop.url}/#{drop.name}")
end
puts "downloaded #{drop.name}"
end
else
puts "unable to execute #{ARGV.first}\n\n"
usage
end
rescue SocketError
abort 'Wooops, an error occured, check your network settings.'
rescue CloudApp::ResponseError
abort "Not possble to execute #{ARGV.first} on #{ARGV[1]}."
rescue => e
abort "Report this as a bug:\n" + e.to_s
end