/
command.rb
62 lines (49 loc) · 1.17 KB
/
command.rb
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
require "colored"
require "active_support/core_ext/module/delegation"
require "highline"
require "tty-prompt"
require "os"
module Cp8Cli
class Command
class << self
delegate :open_url, :say, :success, :ask, :title, :error, :run, :read, to: :client
attr_accessor :client
end
self.client = new
def open_url(url)
return title(url) if ENV['BROWSER'] == 'echo'
`#{OS.open_file_command} \"#{url}\"`
end
def title(message)
highline.say(message.bold)
end
def say(*args)
highline.say(*args)
end
def success(message)
highline.say(message.green.bold)
end
def ask(message, required: true, default: nil)
tty.ask(message, required: required, default: default)
end
def error(message)
say(message.red.bold)
exit(false)
end
def run(command, title: nil)
title(title) if title
say(command)
system(command) || error("Error running: #{command}")
end
def read(command)
`#{command}`.strip.presence
end
private
def highline
@_highline ||= HighLine.new
end
def tty
@_tty ||= TTY::Prompt.new
end
end
end