Module: CLIHelper

Defined in:
lib/cli_helper.rb

Overview

CLI Helper

Defined Under Namespace

Modules: HashWithSearch Classes: ShowTable

Constant Summary collapse

FILTER_OPS =

Available operators for filtering operations

%w[= != < <= > >= ~]
LIST =

CLI general options

{
    :name => 'list',
    :short => '-l x,y,z',
    :large => '--list x,y,z',
    :format => Array,
    :description => 'Selects columns to display with list command'
}
LISTCONF =
{
    :name => 'listconf',
    :short => '-c conf',
    :large => '--listconf conf',
    :format => String,
    :description => 'Selects a predefined column list'
}
CSV_OPT =
{
    :name => 'csv',
    :large => '--csv',
    :description => 'Write table in csv format'
}
CSV_DEL =
{
    :name => 'csv_del',
    :large => '--csv-del del',
    :format => String,
    :description => 'Set delimiter for csv output'
}
FILTER =
{
    :name => 'filter',
    :short => '-f x,y,z',
    :large => '--filter x,y,z',
    :format => Array,
    :description => "Filter data. An array is specified with\n" <<
                    ' ' * 31 << 'column=value pairs.' <<
                    ' ' * 31 << "Valid operators #{FILTER_OPS.join(',')}" <<
                    ' ' * 31 << 'e.g. NAME=test (match name with test)' <<
                    ' ' * 31 << 'NAME~test (match every NAME containing' <<
                    ' ' * 31 << 'the substring \'test\')'
}
OPERATOR =
{
    :name => 'operator',
    :large => '--operator operator',
    :format => String,
    :description => 'Logical operator used on filters: AND, OR. ' \
                    'Default: AND.'
}
NO_HEADER =
{
    :name => 'no_header',
    :large => '--no-header',
    :description => 'Hides the header of the table'
}
DELAY =
{
    :name => 'delay',
    :short => '-d x',
    :large => '--delay x',
    :format => Integer,
    :description => 'Sets the delay in seconds for top command'
}
NO_PAGER =
{
    :name => 'no_pager',
    :large => '--no-pager',
    :description => 'Disable pagination',
    :proc => lambda {|_o, _options|
        ENV['ONE_PAGER'] = 'cat' if File.exist?('/bin/cat')
    }
}
ADJUST =
{
    :name => 'adjust',
    :large => '--adjust x,y,z',
    :format => Array,
    :description => 'Adjust size to not truncate selected columns'
}
SIZE =
{
    :name => 'size',
    :short => '-s x=size,y=size',
    :large => '--size x=size,y=size',
    :format => Array,
    :description => 'Change the size of selected columns. ' \
                    'For example: ' \
                    '$ onevm list --size "name=20" ' \
                    'will make column name size 20.'
}
EXPAND =
{
    :name => 'expand',
    :large => '--expand [x=prop,y=prop]',
    :format => Array,
    :description => 'Expands the columns size to fill the terminal. ' \
                    'For example: ' \
                    '$onevm list --expand name=0.4,group=0.6 ' \
                    'will expand name 40% and group 60%. ' \
                    '$onevm list --expand name,group ' \
                    'will expand name and group based on its size.' \
                    '$onevm list --expand will expand all columns.'
}
NO_EXPAND =
{
    :name => 'no_expand',
    :large => '--no-expand',
    :description => 'Disable expand'
}
OPTIONS =
[LIST,
LISTCONF,
DELAY,
FILTER,
OPERATOR,
CSV_OPT,
CSV_DEL,
NO_PAGER,
NO_HEADER,
ADJUST,
SIZE,
EXPAND,
NO_EXPAND]
ANSI_RED =

CLI state colors

"\33[31m"
ANSI_GREEN =
"\33[32m"
ANSI_RESET =
"\33[0m"
ANSI_YELLOW =
"\33[33m"
OK_STATES =

CLI states

%w[runn rdy on SUCCESS RUNNING]
BAD_STATES =
%w[fail
err
error
ERROR
FAILED_DEPLOYING
FAILED_DEPLOYING_NETS
FAILED_UNDEPLOYING
FAILED_UNDEPLOYING_NETS
FAILED_SCALING]
REGULAR_STATES =
%w[PENDING
DEPLOYING
DEPLOYING_NETS
UNDEPLOYING
UNDEPLOYING_NETS
CONFIGURING
WARNING]

Class Method Summary collapse

Class Method Details

.base64?(value) ⇒ Boolean

Check if value is in base64

Parameters:

  • value (String)

    Value to check

Returns:

  • (Boolean)

    True if it’s base64



273
274
275
276
277
# File 'lib/cli_helper.rb', line 273

def self.base64?(value)
    re = %r(^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)?$)

    !value.match(re).nil?
end

.color_state(state) ⇒ Object

Set state color

Parameters:

  • stat (String)

    Current state



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/cli_helper.rb', line 206

def self.color_state(state)
    if $stdout.tty?
        case state.strip
        when *OK_STATES
            ANSI_GREEN + state + ANSI_RESET
        when *BAD_STATES
            ANSI_RED + state + ANSI_RESET
        when *REGULAR_STATES
            ANSI_YELLOW + state + ANSI_RESET
        else
            state
        end
    else
        state
    end
end

.fail(message) ⇒ Object

Show error message and exit with error

Parameters:

  • message (String)

    Error message to show



262
263
264
265
266
# File 'lib/cli_helper.rb', line 262

def self.fail(message)
    STDERR.puts message

    exit(-1)
end

.green(text) ⇒ Object

Get text in green colour

Parameters:

  • text (String)

    String to print



226
227
228
229
230
231
232
# File 'lib/cli_helper.rb', line 226

def self.green(text)
    if $stdout.tty?
        ANSI_GREEN + text + ANSI_RESET
    else
        text
    end
end

Print header

Parameters:

  • str (String)

    String with header content

  • underline (Boolean) (defaults to: true)

    True to underline the header



238
239
240
241
242
243
244
245
246
# File 'lib/cli_helper.rb', line 238

def self.print_header(str, underline = true)
    if $stdout.tty?
        print_tty_header(str, underline)
    else
        print str
    end

    puts
end

Print pretty header

Parameters:

  • str (String)

    String with header content

  • underline (Boolean) (defaults to: true)

    True to underline the header



252
253
254
255
256
257
# File 'lib/cli_helper.rb', line 252

def self.print_tty_header(str, underline = true)
    scr_bold
    scr_underline if underline
    print str
    scr_restore
end

.scr_boldObject

Sets bold font



151
152
153
# File 'lib/cli_helper.rb', line 151

def self.scr_bold
    print "\33[1m"
end

.scr_clsObject

Clears screen



166
167
168
# File 'lib/cli_helper.rb', line 166

def self.scr_cls
    print "\33[2J\33[H"
end

.scr_move(cord_x, cord_y) ⇒ Object

Moves the cursor

Parameters:

  • cord_x (Integer)

    Coordinate x

  • cord_y (Integer)

    Coordinate y



174
175
176
# File 'lib/cli_helper.rb', line 174

def self.scr_move(cord_x, cord_y)
    print "\33[#{cord_x};#{cord_y}H"
end

.scr_restoreObject

Restore normal font



161
162
163
# File 'lib/cli_helper.rb', line 161

def self.scr_restore
    print "\33[0m"
end

.scr_underlineObject

Sets underline



156
157
158
# File 'lib/cli_helper.rb', line 156

def self.scr_underline
    print "\33[4m"
end