Class: Octopolo::CLI
- Inherits:
-
Object
- Object
- Octopolo::CLI
- Defined in:
- lib/octopolo/cli.rb
Overview
Public: Class to perform cli-related tasks, like performing commands.
Class Method Summary collapse
- .ask(question, choices) ⇒ Object
-
.ask_boolean(question) ⇒ Object
Public: Ask a yes or no question.
- .copy_to_clipboard(input) ⇒ Object
-
.highline ⇒ Object
Public: Instantiate an instance of HighLine.
-
.open(path) ⇒ Object
Public: Open the given path with Mac OS X’s built-in ‘open` command.
-
.perform(command, say_command = true, ignore_non_zero = false) ⇒ Object
Public: Perform the given shell command.
-
.perform_and_exit(command) ⇒ Object
Public: Replace the current process with the given shell command.
-
.perform_in(dir) ⇒ Object
Public: Perform a set of commands in the given directory.
-
.perform_quietly(command) ⇒ Object
Public: Perform the command, but do not print out the command.
- .prompt(prompt_text = "> ") ⇒ Object
-
.prompt_multiline(prompt_text) ⇒ Object
Public: Prompt user for multiple lines of input.
-
.prompt_secret(prompt_text) ⇒ Object
Public: Prompt user for input, but do not display what they type.
-
.say(message) ⇒ Object
Public: Display the given message.
-
.spacer_line ⇒ Object
Public: Display a blank line.
Class Method Details
.ask(question, choices) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/octopolo/cli.rb', line 115 def self.ask(question, choices) return choices.first if choices.size == 1 say question choices.each_with_index do |choice, i| say "#{i+1}) #{choice}" end selection = nil while not choices.include?(selection) selection = prompt break if choices.include?(selection) # passed in the value of the choice selection_index = selection.to_i - 1 # passed in a 1-based index of the choice selection = choices[selection_index] if selection_index >= 0 # gather the value of the choice break if choices.include?(selection) say "Not a valid choice." end selection end |
.ask_boolean(question) ⇒ Object
Public: Ask a yes or no question
question - The question to display to the user in the prompt
Returns a Boolean
141 142 143 144 145 |
# File 'lib/octopolo/cli.rb', line 141 def self.ask_boolean(question) answer = prompt("#{question} (y/n)") # Return true if the answer starts with "Y" or "y"; else return false !!(answer =~ /^y/i) end |
.copy_to_clipboard(input) ⇒ Object
185 186 187 188 189 190 |
# File 'lib/octopolo/cli.rb', line 185 def self.copy_to_clipboard(input) say "Putting '#{input}' on the clipboard." # have to do this all roundy-abouty by passing to /bin/bash, because by default, ruby performs commands with /bin/sh, which doesn't respect the -n flag on echo # http://stackoverflow.com/questions/5059039/ruby-execute-shell-command-echo-with-n-option perform "/bin/bash -c 'echo -n #{input}' | pbcopy", false end |
.highline ⇒ Object
Public: Instantiate an instance of HighLine
This is likely a temporary method until we replace a lot of CLI’s guts with HighLine equivalents.
Returns an instance of HighLine
203 204 205 |
# File 'lib/octopolo/cli.rb', line 203 def self.highline HighLine.new end |
.open(path) ⇒ Object
Public: Open the given path with Mac OS X’s built-in ‘open` command
193 194 195 |
# File 'lib/octopolo/cli.rb', line 193 def self.open path perform_and_exit "open '#{path}'" end |
.perform(command, say_command = true, ignore_non_zero = false) ⇒ Object
Public: Perform the given shell command.
command - A String containing the command to perform. say_command - A Boolean determining whether to display the performed command to the screen. (default: true) ignore_non_zero - Ignore exception for non-zero exit status of command.
Examples
CLI.perform "git pull", false
git pull
Already up-to-date.
# => "Already up-to-date."
CLI.perform "git pull", false
# => "Already up-to-date."
Returns the output of the command as a String.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/octopolo/cli.rb', line 24 def self.perform(command, say_command = true, ignore_non_zero=false) # display the command say command if say_command # and then perform it if Open3.respond_to?(:capture3) output, error, status = Open3.capture3(command) raise "command=#{command}; exit_status=#{status.exitstatus}; stderr=#{error}" unless status.success? || ignore_non_zero else # Only necessary as long as we use 1.8.7, which doesn't have Open3.capture3 output = `#{command}` end # speak the output say output if say_command # return the output of the command output end |
.perform_and_exit(command) ⇒ Object
Public: Replace the current process with the given shell command.
command - A String containing the command to perform.
Returns nothing and exits the current Ruby process.
60 61 62 63 64 65 66 67 |
# File 'lib/octopolo/cli.rb', line 60 def self.perform_and_exit(command) say command # Kernel#exec replaces the ruby process with the new bash process # executing the command. This is useful for us mainly for things like # calling `ssh` which will be interactive or `hub` which will open a text # editor. Those commands don't play well with Kernel#` or Open3.capture3. exec command end |
.perform_in(dir) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/octopolo/cli.rb', line 108 def self.perform_in(dir) say "Performing in #{dir}:" Dir.chdir(dir) do yield end end |
.perform_quietly(command) ⇒ Object
Public: Perform the command, but do not print out the command
command - A String containing the command to perform.
Examples
CLI.perform_quietly "git pull"
Already up-to-date.
# => "Already up-to-date."
51 52 53 |
# File 'lib/octopolo/cli.rb', line 51 def self.perform_quietly command perform command, false end |
.prompt(prompt_text = "> ") ⇒ Object
147 148 149 150 151 |
# File 'lib/octopolo/cli.rb', line 147 def self.prompt(prompt_text="> ") highline.ask prompt_text do |conf| conf.readline = true end.to_s end |
.prompt_multiline(prompt_text) ⇒ Object
Public: Prompt user for multiple lines of input
prompt_text - The text to display before the prompt
Example:
# Accept multiple lines of text, with the prompt "QA Plan:"
plan = CLI.prompt_multiline "QA Plan:"
Returns a String containing the value the user entered
163 164 165 166 167 168 169 170 |
# File 'lib/octopolo/cli.rb', line 163 def self.prompt_multiline(prompt_text) highline.ask(prompt_text) do |conf| # accept text until the first blank line (instead of stopping at the # first newline), to allow multiple lines of input conf.gather = "" conf.readline = true end end |
.prompt_secret(prompt_text) ⇒ Object
Public: Prompt user for input, but do not display what they type
prompt_text - The text to display before the prompt; e.g., “Password: ”
Returns a String containing the value the user entered
177 178 179 180 181 182 183 |
# File 'lib/octopolo/cli.rb', line 177 def self.prompt_secret(prompt_text) highline.ask(prompt_text) do |conf| # do not display the text input conf.echo = false conf.readline = true end end |
.say(message) ⇒ Object
80 81 82 83 84 |
# File 'lib/octopolo/cli.rb', line 80 def self.say() unless .nil? || .empty? puts end end |
.spacer_line ⇒ Object
Public: Display a blank line
87 88 89 |
# File 'lib/octopolo/cli.rb', line 87 def self.spacer_line say " " end |