Class: Kontena::Cli::BrowserLauncher

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/cli/browser_launcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ BrowserLauncher

Returns a new instance of BrowserLauncher.



12
13
14
# File 'lib/kontena/cli/browser_launcher.rb', line 12

def initialize(url)
  @url = url
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/kontena/cli/browser_launcher.rb', line 10

def url
  @url
end

Class Method Details

.open(url) ⇒ Object



6
7
8
# File 'lib/kontena/cli/browser_launcher.rb', line 6

def self.open(url)
  Kontena::Cli::BrowserLauncher.new(url).launch
end

Instance Method Details

#commandObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kontena/cli/browser_launcher.rb', line 20

def command
  cmd = if Kontena.on_windows?
    ['cmd', '/c', 'start', '/b', url.gsub(/&/, '^&')]
  elsif RUBY_PLATFORM =~ /darwin/
    ["open", url]
  elsif Kontena.browserless?
    raise RuntimeError, "Environment variable DISPLAY not set, assuming non-desktop session, unable to open browser. Try using '--remote' option."
  else
    [detect_unixlike_command, url]
  end

  Kontena.logger.debug { "Using %p to launch browser" % cmd }

  cmd
end

#detect_unixlike_commandObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kontena/cli/browser_launcher.rb', line 36

def detect_unixlike_command
  Kontena.logger.debug { "Assuming unix-like environment, looking for browser" }

  cmd = %w(
    xdg-open
    sensible-browser
    x-www-browser
  ).find { |c| !which(c).nil? }

  if cmd.nil?
    if ENV['BROWSER']
      cmd = which(ENV['BROWSER'])
      return cmd unless cmd.nil?
    end
    raise RuntimeError, "Unable to launch a local browser. Try installing xdg-utils or sensible-utils package, setting BROWSER environment variable or using the --remote option"
  end

  cmd
end

#launchObject



16
17
18
# File 'lib/kontena/cli/browser_launcher.rb', line 16

def launch
  system(*command)
end

#which(cmd) ⇒ Object



56
57
58
# File 'lib/kontena/cli/browser_launcher.rb', line 56

def which(cmd)
  Kontena::Util.which(cmd)
end