Class: Envo::CmdClean

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

Constant Summary collapse

Name =
'clean'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names) ⇒ CmdClean

Returns a new instance of CmdClean.



29
30
31
32
# File 'lib/envo/cmd_clean.rb', line 29

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

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



34
35
36
# File 'lib/envo/cmd_clean.rb', line 34

def names
  @names
end

Class Method Details

.parse_cli(args) ⇒ Object



20
21
22
23
# File 'lib/envo/cmd_clean.rb', line 20

def self.parse_cli(args)
  opts = CliParser.filter_opts(args)
  parse_tokens(args, opts)
end

.parse_tokens(tokens, opts) ⇒ Object



25
26
27
# File 'lib/envo/cmd_clean.rb', line 25

def self.parse_tokens(tokens, opts)
  ParsedCmd.new(CmdClean.new(tokens), opts)
end

.register_cli_parser(parser) ⇒ Object



12
13
14
# File 'lib/envo/cmd_clean.rb', line 12

def self.register_cli_parser(parser)
  parser.add_cmd(Name, ->(cmd, args) { parse_cli(args) })
end

.register_help(help) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/envo/cmd_clean.rb', line 4

def self.register_help(help)
  help.add_cmd "clean <name> ...", "    cleans environent variables\n    unsets empty strings, non-existing paths, empty lists\n    removes duplicates from lists and non-existing paths from path lists\n  EOF\nend\n"

.register_script_parser(parser) ⇒ Object



16
17
18
# File 'lib/envo/cmd_clean.rb', line 16

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

Instance Method Details

#execute(ctx) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/envo/cmd_clean.rb', line 36

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

    if ctx.raw?
      val = ctx.raw_get(ename)
      ctx.unset(ename) if val && val.empty?
    else
      val = ctx.smart_get(ename)
      if val.type != :empty
        val.clean!
        ctx.smart_set(ename, val)
      end
    end
  end
end