aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/util.rb
blob: ad4f01c5d66db6018f65418b92a33f04f8c98fc9 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module LeapCli
  module Commands
    extend self
    extend LeapCli::Util
#     #
#     # 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.
#     #
    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.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

#     #
#     # read a file, exit if the file doesn't exist.
#     #
#     def read_file!(file_path)
#       if !File.exists?(file_path)
#         bail!("File '%s' does not exist." % file_path)
#       else
#         File.readfile(file_path)
#       end
#     end

#     ##
#     ## LOGGING
#     ##

#     def log0(message=nil, &block)
#       if message
#         puts message
#       elsif block
#         puts yield(block)
#       end
#     end

#     def log1(message=nil, &block)
#       if LeapCli.log_level > 0
#         if message
#           puts message
#         elsif block
#           puts yield(block)
#         end
#       end
#     end

#     def log2(message=nil, &block)
#       if LeapCli.log_level > 1
#         if message
#           puts message
#         elsif block
#           puts yield(block)
#         end
#       end
#     end

#     def progress(message)
#       log1(" * " + message)
#     end

#     ##
#     ## QUITTING
#     ##

#     #
#     # quit and print help
#     #
#     def help!(message=nil)
#       ENV['GLI_DEBUG'] = "false"
#       help_now!(message)
#       #say("ERROR: " + message)
#     end

#     #
#     # quit with a message that we are bailing out.
#     #
#     def bail!(message="")
#       say(message)
#       say("Bailing out.")
#       raise SystemExit.new
#       #ENV['GLI_DEBUG'] = "false"
#       #exit_now!(message)
#     end

#     #
#     # quit with no message
#     #
#     def quit!(message='')
#       say(message)
#       raise SystemExit.new
#     end

#     #
#     # bails out with message if assertion is false.
#     #
#     def assert!(boolean, message)
#       bail!(message) unless boolean
#     end

#     #
#     # assert that the command is available
#     #
#     def assert_bin!(cmd_name)
#       assert! `which #{cmd_name}`.strip.any?, "Sorry, bailing out, the command '%s' is not installed." % cmd_name
#     end

  end
end