Module: OpenRemote::Browser

Extended by:
OS
Included in:
OpenRemote
Defined in:
lib/or-browser.rb

Overview

Web browser opening commands

Class Method Summary collapse

Methods included from OS

os_name

Class Method Details

.browse(remote) ⇒ Object

Generate and open approprate website from ssh/git link



23
24
25
# File 'lib/or-browser.rb', line 23

def browse(remote)
  open_url prepare remote
end

.browserObject

Return the right command for opening a website from the terminal



29
30
31
32
33
34
35
36
37
38
# File 'lib/or-browser.rb', line 29

def browser
  case os_name
  when "mac"
    "open "
  when "dos"
    "start "
  when "nix"
    "xdg-open "
  end
end

.git_at_to_https(base, url) ⇒ Object

Helper transformations



82
83
84
85
86
87
88
# File 'lib/or-browser.rb', line 82

def git_at_to_https(base, url)
  info = url.partition("@").last
  host = info.partition(":").first
  user_repo = info.partition(":").last
  user_repo.sub!(/\.git$/, "")
  "#{base}#{host}/#{user_repo}"
end

.git_colon_to_https(base, url) ⇒ Object



90
91
92
93
94
# File 'lib/or-browser.rb', line 90

def git_colon_to_https(base, url)
  info = url.partition("://").last
  info.sub!(/\.git$/, "")
  base << info
end

.open_url(url) ⇒ Object

Make the system call to open up a website



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/or-browser.rb', line 42

def open_url(url)
  puts "Opening: ".green + url

  # Use spawn for better control - detach browser process and suppress its output
  # This allows the script to exit cleanly while hiding verbose GTK warnings
  begin
    pid = spawn("#{browser}#{url}", out: "/dev/null", err: "/dev/null")
    Process.detach(pid)
  rescue Errno::ENOENT
    puts "Error: Could not find browser command '#{browser.strip}'".red
    exit 1
  rescue => e
    puts "Error opening browser: #{e.message}".red
    exit 1
  end
end

.prepare(url) ⇒ Object

Parse remote to determine whether it is https/ssh, give link



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/or-browser.rb', line 61

def prepare(url)
  hb = "https://" # https base url
  if /^https/.match?(url) # is website, return w/o .git ending
    url.sub(/\.git$/, "")

  elsif /^git@/.match?(url) # git remote w/ @
    git_at_to_https hb, url

  elsif /^git:/.match?(url) # git remote w/ :
    git_colon_to_https hb, url

  elsif /^ssh/.match?(url) # is ssh link, change to website
    ssh_to_https hb, url

  else # unknown, return a generic link
    raise "Malformed remote url: " + url
  end
end

.ssh_to_https(base, url) ⇒ Object



96
97
98
99
100
# File 'lib/or-browser.rb', line 96

def ssh_to_https(base, url)
  info = url.partition("@").last
  info.sub!(/\.git$/, "")
  base << info
end