Repository navigation
Expand file tree
/
Copy pathfog
More file actions
executable file
·129 lines (106 loc) · 3.26 KB
/
Copy pathfog
File metadata and controls
executable file
·129 lines (106 loc) · 3.26 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
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'fog'))
Fog.mock! if ENV['FOG_MOCK']
# fogcli provider:service:collection:command arguments flags
args = ARGV
if args.first.include?(':')
# assume first arg with colons is a command
command = args.shift
elsif args.empty? || args.all? {|arg| arg =~ /^--/}
# only flags implies interactive
unless args.include?('--interactive')
args << '--interactive'
end
end
# parse arguments and flags
# arguments are positional
# arguments containing = are split and added to a last argument hash
# flags begin with '--'
arguments, flags = [], []
args.each do |arg|
case arg
when /^--/
flags << arg
when /=/
unless arguments.last.is_a?(Hash)
arguments << {}
end
key, value = arg.split('=')
arguments.last[key] = value
else
arguments << arg
end
end
# parse flags
flags.each do |flag|
case flag
when '--mock'
Fog.mock!
when /=/
key, value = flag.split('=')
case key
when '--credential'
Fog.credential = value.to_sym
else
Formatador.display_line("[red]Error! Unknown flag #{key} with value #{value}.[/]")
return
end
end
end
# check for credentials and bomb out if they are missing
if Fog.credentials.empty?
begin
Fog::Errors.missing_credentials
rescue Fog::Errors::LoadError => error
abort error.message
end
end
# drop into interactive, raise error, or run command
if flags.include?('--interactive')
require 'fog/bin'
require 'irb'
ARGV.clear # Avoid passing args to IRB
IRB.setup(nil)
@irb = IRB::Irb.new(nil)
IRB.conf[:MAIN_CONTEXT] = @irb.context
IRB.conf[:PROMPT][:FOG] = IRB.conf[:PROMPT][:SIMPLE].dup
IRB.conf[:PROMPT][:FOG][:RETURN] = "%s\n"
@irb.context.prompt_mode = :FOG
@irb.context.workspace = IRB::WorkSpace.new(binding)
Formatador.display_line('Welcome to fog interactive!')
providers = Fog.available_providers
providers_listing = if providers.length > 1
providers[0...-1].join(', ') << ' and ' << providers[-1]
else
providers.first
end
Formatador.display_line(":#{Fog.credential} provides #{providers_listing}")
# FIXME: hacks until we can `include Fog` in bin
CDN = Fog::CDN
Compute = Fog::Compute
DNS = Fog::DNS
Storage = Fog::Storage
catch(:IRB_EXIT) { @irb.eval_input }
elsif command.nil?
Formatador.display_line('[red]Error! Expected either a `provider:service:collection:command` or `--interactive`.[/]')
else # CLI
provider_symbol, service_symbol, collection_symbol, method_symbol = command.split(':').map {|string| string.to_sym}
require "fog/#{provider_symbol}"
if [:cdn, :compute, :dns, :storage].include?(service_symbol)
service_string = service_symbol.to_s
service = Fog.const_get(service_string[0..0].upcase + service_string[1..-1])
connection = service[provider_symbol]
else
provider = Fog.const_get(Fog.providers[provider_symbol])
require "fog/#{provider_symbol}/#{service_symbol}"
connection = provider[service_symbol]
end
result = if method_symbol
collection = connection.send(collection_symbol)
collection.send(method_symbol, *arguments)
else
# must be a request, instead of a collection, or implied #all
connection.send(collection_symbol, *arguments)
end
p result
end