Module: LeapCli::Commands

Extended by:
Commands, LogCommand, Util
Included in:
Commands
Defined in:
lib/leap_cli.rb,
lib/leap_cli/commands/new.rb,
lib/leap_cli/commands/pre.rb,
lib/leap_cli/commands/common.rb

Overview

for commands in leap_platform/lib/leap_cli/commands

Instance Method Summary collapse

Methods included from LogCommand

assert!, bail!, log, log_level, log_raw, logger, new_logger

Methods included from Util

assert!, assert_bin!, assert_config!, assert_files_exist!, assert_files_missing!, assert_run!, bail!, cmd_exists?, current_git_branch, current_git_commit, dir_exists?, ensure_dir, erb_eval, exit_status, file_content_equals?, file_exists?, help!, is_git_directory?, is_git_subrepo?, log, long_running, pty_run, quit!, read_file, read_file!, relative_symlink, remove_directory!, remove_file!, rename_file!, replace_file!, write_file!

Instance Method Details

#agree(question, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/leap_cli/commands/common.rb', line 81

def agree(question, options={})
  while true
    response = ask(question, options)
    if response.nil?
      say('Please enter "yes" or "no".')
    elsif ["y","yes", "ye"].include?(response.downcase)
      return true
    elsif ["n", "no"].include?(response.downcase)
      return false
    else
      say('Please enter "yes" or "no".')
    end
  end
end

#ask(question, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/leap_cli/commands/common.rb', line 59

def ask(question, options={})
  default = options[:default]
  if default
    if ends_in_whitespace?(question)
      question = question + "|" + default + "| "
    else
      question = question + "|" + default + "|"
    end
  end
  response = Readline.readline(question, true) # set to false if ever reading passwords.
  if response
    response = response.strip
    if response.empty?
      return default
    else
      return response
    end
  else
    return default
  end
end

#numbered_choice_menu(msg, items, &block) ⇒ Object

keeps prompting the user for a numbered choice, until they pick a good one or bail out.

block is yielded and is responsible for rendering the choices.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/leap_cli/commands/common.rb', line 17

def numbered_choice_menu(msg, items, &block)
  while true
    say("\n" + msg + ':')
    items.each_with_index(&block)
    say("q. quit")
    index = ask("number 1-#{items.length}> ")
    if index.nil? || index.empty?
      next
    elsif index =~ /q/
      bail!
    else
      i = index.to_i - 1
      if i < 0 || i >= items.length
        bail!
      else
        return i
      end
    end
  end
end

#parse_node_list(nodes) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/leap_cli/commands/common.rb', line 38

def parse_node_list(nodes)
  if nodes.is_a? Config::Object
    Config::ObjectList.new(nodes)
  elsif nodes.is_a? Config::ObjectList
    nodes
  elsif nodes.is_a? String
    manager.filter!(nodes)
  else
    bail! "argument error"
  end
end

#path(name) ⇒ Object



8
9
10
# File 'lib/leap_cli/commands/common.rb', line 8

def path(name)
  Path.named_path(name)
end

#say(statement) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/leap_cli/commands/common.rb', line 50

def say(statement)
  if ends_in_whitespace?(statement)
    $stdout.print(statement)
    $stdout.flush
  else
    $stdout.puts(statement)
  end
end