Class: Kontena::Cli::Stacks::YAML::Opto::Resolvers::ServiceLink
- Inherits:
-
Opto::Resolver
- Object
- Opto::Resolver
- Kontena::Cli::Stacks::YAML::Opto::Resolvers::ServiceLink
show all
- Includes:
- Common
- Defined in:
- lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb
Instance Method Summary
collapse
Methods included from Common
#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning
Instance Method Details
#default_index(services) ⇒ Integer
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 34
def default_index(services)
index = services.index {|s| service_link(s) == option.default }
if index && !option.required?
index + 2 elsif index
index + 1 else
0 end
end
|
#filter_by_image(services, image) ⇒ Object
64
65
66
67
68
|
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 64
def filter_by_image(services, image)
services.select { |s|
s['image'].include?(image)
}
end
|
#filter_by_name(services, name) ⇒ Object
70
71
72
73
74
|
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 70
def filter_by_name(services, name)
services.select { |s|
s['name'].include?(name)
}
end
|
#get_services ⇒ Array<Hash>
26
27
28
29
30
|
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 26
def get_services
client.get("grids/#{current_grid}/services")['services']
rescue
[]
end
|
#resolve ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 5
def resolve
return nil unless current_master && current_grid
message = hint['prompt']
name_filter = hint['name']
image_filter = hint['image']
raise "prompt missing" unless message
services = get_services
services = filter_by_image(services, image_filter) if image_filter
services = filter_by_name(services, name_filter) if name_filter
return nil if services.size == 0
prompt.select(message) do ||
.default(default_index(services)) if option.default
.choice "<none>", nil unless option.required?
services.each do |s|
.choice service_name(s), service_link(s)
end
end
end
|
#service_link(service) ⇒ String
48
49
50
51
|
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 48
def service_link(service)
grid, stack, service = service['id'].split('/')
"#{stack}/#{service}"
end
|
#service_name(service) ⇒ String
55
56
57
58
59
60
61
62
|
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 55
def service_name(service)
grid, stack, service = service['id'].split('/')
if stack == 'null'.freeze
service
else
"#{stack}/#{service}"
end
end
|
#stack ⇒ Object
76
77
78
|
# File 'lib/kontena/cli/stacks/yaml/opto/service_link_resolver.rb', line 76
def stack
ENV['STACK']
end
|