Class: Envo::CmdShow

Inherits:
Object
  • Object
show all
Defined in:
lib/envo/cmd_show.rb

Constant Summary collapse

Name =
'show'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names, show_names) ⇒ CmdShow

Returns a new instance of CmdShow.



54
55
56
57
58
# File 'lib/envo/cmd_show.rb', line 54

def initialize(names, show_names)
  raise Error.new 'show: no names provided' if names.empty?
  @names = names
  @show_names = show_names
end

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



60
61
62
# File 'lib/envo/cmd_show.rb', line 60

def names
  @names
end

#show_namesObject (readonly)

Returns the value of attribute show_names.



60
61
62
# File 'lib/envo/cmd_show.rb', line 60

def show_names
  @show_names
end

Class Method Details

.parse_cli(args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/envo/cmd_show.rb', line 27

def self.parse_cli(args)
  opts = CliParser.filter_opts(args)
  show_names = false
  opts.filter! do |opt|
    if opt == '--name'
      show_names = true
      false
    else
      true
    end
  end
  ParsedCmd.new(CmdShow.new(args, show_names), opts)
end

.parse_script(tokens, opts) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/envo/cmd_show.rb', line 41

def self.parse_script(tokens, opts)
  show_names = false
  opts.filter! do |opt|
    if opt == 'name'
      show_names = true
      false
    else
      true
    end
  end
  ParsedCmd.new(CmdShow.new(tokens, show_names), opts)
end

.register_cli_parser(parser) ⇒ Object



17
18
19
20
21
# File 'lib/envo/cmd_show.rb', line 17

def self.register_cli_parser(parser)
  parser.add_cmd('show', ->(cmd, args) { parse_cli(args) })
  parser.add_cmd('s', ->(cmd, args) { parse_cli(args) })
  parser.add_cmd('rshow', ->(cmd, args) { parse_cli(args + ['--raw']) })
end

.register_help(help) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/envo/cmd_show.rb', line 4

def self.register_help(help)
  help.add_cmd "show <name> ...", <<~EOF
    show values of environment variables
      --name - display the name of the variable along with the value
      shorhand: 's'
  EOF

  help.add_cmd 'rshow <name> ...', <<~EOF
    show the *raw* value of environment variables with no pretty prints
    a shortcut to 'show --raw <name> ...'
  EOF
end

.register_script_parser(parser) ⇒ Object



23
24
25
# File 'lib/envo/cmd_show.rb', line 23

def self.register_script_parser(parser)
  parser.add_cmd(Name, ->(cmd, tokens, opts) { parse_script(tokens, opts) })
end

Instance Method Details

#execute(ctx) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/envo/cmd_show.rb', line 62

def execute(ctx)
  @names.each do |name|
    ename = ctx.expand_name(name)

    pname = show_names ? "#{ename}=" : ''

    if ctx.raw?
      ctx.puts("#{pname}#{ctx.raw_get(ename)}")
    else
      val = ctx.smart_get(ename)
      if val.type == :empty
        ctx.puts("No var with name #{ename}")
      else
        ctx.print(pname)
        val.pretty_print(ctx)
      end
    end
  end
end