Class: Bard::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/bard/target.rb,
lib/bard/plugins/deploy.rb,
lib/bard/plugins/github_pages.rb,
lib/bard/plugins/ssh/target_methods.rb,
lib/bard/plugins/url/target_methods.rb,
lib/bard/plugins/ping/target_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, config) ⇒ Target

Returns a new instance of Target.



7
8
9
10
11
12
# File 'lib/bard/target.rb', line 7

def initialize(key, config)
  @key = key
  @config = config
  @capabilities = []
  @path = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/bard/target.rb', line 5

def config
  @config
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/bard/target.rb', line 5

def key
  @key
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



63
64
65
66
# File 'lib/bard/target.rb', line 63

def ==(other)
  return false unless other.is_a?(Bard::Target)
  comparable_state == other.comparable_state
end

#deploy_strategyObject



221
222
223
# File 'lib/bard/plugins/deploy.rb', line 221

def deploy_strategy
  @deploy_strategy
end

#deploy_strategy_instanceObject



225
226
227
228
229
230
231
232
233
234
# File 'lib/bard/plugins/deploy.rb', line 225

def deploy_strategy_instance
  strategy = @deploy_strategy
  strategy ||= :ssh if has_capability?(:ssh)
  raise "No deployment strategy configured for target #{key}" unless strategy

  strategy_class = Bard::DeployStrategy[strategy]
  raise "Unknown deployment strategy: #{strategy}" unless strategy_class

  strategy_class.new(self)
end

#enable_capability(capability) ⇒ Object

Capability tracking



15
16
17
# File 'lib/bard/target.rb', line 15

def enable_capability(capability)
  @capabilities << capability unless @capabilities.include?(capability)
end

#exec!(command, home: false) ⇒ Object



42
43
44
# File 'lib/bard/target.rb', line 42

def exec!(command, home: false)
  Command.exec!(command)
end

#github_pages(url = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/bard/plugins/github_pages.rb', line 21

def github_pages(url = nil)
  if url.nil?
    @github_pages_url
  else
    @deploy_strategy = :github_pages
    @github_pages_url = url
    enable_capability(:github_pages)
  end
end

#has_capability?(capability) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/bard/target.rb', line 19

def has_capability?(capability)
  @capabilities.include?(capability)
end

#hashObject



69
70
71
# File 'lib/bard/target.rb', line 69

def hash
  comparable_state.hash
end

#pathObject



29
30
31
# File 'lib/bard/target.rb', line 29

def path
  @path || config.project_name
end

#ping(*urls) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/bard/plugins/ping/target_methods.rb', line 6

def ping(*urls)
  if urls.empty?
    @ping_urls || [url].compact
  elsif urls.first == false
    @ping_urls = []
  else
    @ping_urls = urls.flatten.map { |u| normalize_url(u) }
  end
end

#ping!Object



16
17
18
19
20
21
22
# File 'lib/bard/plugins/ping/target_methods.rb', line 16

def ping!
  require_capability!(:url)
  failed_urls = Bard::Ping.call(self)
  if failed_urls.any?
    raise "Ping failed for: #{failed_urls.join(", ")}"
  end
end

#require_capability!(capability) ⇒ Object



23
24
25
26
27
# File 'lib/bard/target.rb', line 23

def require_capability!(capability)
  unless has_capability?(capability)
    raise "#{capability} capability not configured for this target"
  end
end

#run(command, home: false, verbose: false, quiet: false) ⇒ Object



38
39
40
# File 'lib/bard/target.rb', line 38

def run(command, home: false, verbose: false, quiet: false)
  Command.run(command, verbose:, quiet:)
end

#run!(command, home: false, verbose: false, quiet: false, capture: false) ⇒ Object



33
34
35
36
# File 'lib/bard/target.rb', line 33

def run!(command, home: false, verbose: false, quiet: false, capture: false)
  result = Command.run!(command, verbose:, quiet:)
  result if capture
end

#ssh(uri = nil, **options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bard/plugins/ssh/target_methods.rb', line 6

def ssh(uri = nil, **options)
  if uri.nil?
    return @server
  else
    extend Bard::SSH

    @server = Bard::SSHServer.new(uri, **options)
    @path = options[:path] if options[:path]
    enable_capability(:ssh)

    hostname = @server.hostname
    url("https://#{hostname}") if hostname
  end
end

#strategy_options(strategy_name) ⇒ Object



236
237
238
239
# File 'lib/bard/plugins/deploy.rb', line 236

def strategy_options(strategy_name)
  @strategy_options_hash ||= {}
  @strategy_options_hash[strategy_name] || {}
end

#to_sObject

Utility methods



47
48
49
# File 'lib/bard/target.rb', line 47

def to_s
  key.to_s
end

#to_symObject



51
52
53
# File 'lib/bard/target.rb', line 51

def to_sym
  key
end

#url(value = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/bard/plugins/url/target_methods.rb', line 4

def url(value = nil)
  if value.nil?
    @url
  elsif value == false
    @url = nil
    @capabilities.delete(:url)
  else
    @url = normalize_url(value)
    enable_capability(:url)
  end
end

#with(attrs) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/bard/target.rb', line 55

def with(attrs)
  dup.tap do |t|
    attrs.each do |key, value|
      t.send(key, value)
    end
  end
end