Class: Envo::CmdSwap
- Inherits:
-
Object
- Object
- Envo::CmdSwap
- Defined in:
- lib/envo/cmd_swap.rb
Constant Summary collapse
- Name =
'swap'
Instance Attribute Summary collapse
-
#name_a ⇒ Object
Returns the value of attribute name_a.
-
#name_b ⇒ Object
Returns the value of attribute name_b.
Class Method Summary collapse
- .parse_cli(args) ⇒ Object
- .parse_tokens(args, opts) ⇒ Object
- .register_cli_parser(parser) ⇒ Object
- .register_help(help) ⇒ Object
- .register_script_parser(parser) ⇒ Object
Instance Method Summary collapse
- #execute(ctx) ⇒ Object
-
#initialize(name_a, name_b) ⇒ CmdSwap
constructor
A new instance of CmdSwap.
Constructor Details
#initialize(name_a, name_b) ⇒ CmdSwap
Returns a new instance of CmdSwap.
26 27 28 29 |
# File 'lib/envo/cmd_swap.rb', line 26 def initialize(name_a, name_b) @name_a = name_a @name_b = name_b end |
Instance Attribute Details
#name_a ⇒ Object
Returns the value of attribute name_a.
31 32 33 |
# File 'lib/envo/cmd_swap.rb', line 31 def name_a @name_a end |
#name_b ⇒ Object
Returns the value of attribute name_b.
31 32 33 |
# File 'lib/envo/cmd_swap.rb', line 31 def name_b @name_b end |
Class Method Details
.parse_cli(args) ⇒ Object
16 17 18 19 |
# File 'lib/envo/cmd_swap.rb', line 16 def self.parse_cli(args) opts = CliParser.filter_opts(args) parse_tokens(args, opts) end |
.parse_tokens(args, opts) ⇒ Object
21 22 23 24 |
# File 'lib/envo/cmd_swap.rb', line 21 def self.parse_tokens(args, opts) raise Envo::Error.new "swap: provide two names to swap. Use 'swap <name1> <name2>'" if args.size != 2 ParsedCmd.new(CmdSwap.new(args[0], args[1]), opts) end |
.register_cli_parser(parser) ⇒ Object
8 9 10 |
# File 'lib/envo/cmd_swap.rb', line 8 def self.register_cli_parser(parser) parser.add_cmd(Name, ->(cmd, args) { parse_cli(args) }) end |
.register_help(help) ⇒ Object
4 5 6 |
# File 'lib/envo/cmd_swap.rb', line 4 def self.register_help(help) help.add_cmd 'swap <name1> <name2>', "swap values of two environment variables" end |
.register_script_parser(parser) ⇒ Object
12 13 14 |
# File 'lib/envo/cmd_swap.rb', line 12 def self.register_script_parser(parser) parser.add_cmd(Name, ->(cmd, tokens, opts) { parse_tokens(tokens, opts) }) end |
Instance Method Details
#execute(ctx) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/envo/cmd_swap.rb', line 33 def execute(ctx) ea = ctx.(@name_a) raw_a = ctx.raw_get(ea) raise Envo::Error.new "swap: no such var '#{ea}'" if !raw_a && !ctx.force? eb = ctx.(@name_b) raw_b = ctx.raw_get(eb) raise Envo::Error.new "swap: no such var '#{eb}'" if !raw_b && !ctx.force? return if ea == eb # swap something with itself... return if !raw_a && !raw_b # no point in doing anything if they don't exist ctx.raw_set(ea, raw_b) ctx.raw_set(eb, raw_a) end |