Add ruby local command arguments type and mapper
This commit is contained in:
parent
bb2601586d
commit
7b82ba7ca0
@ -18,6 +18,30 @@ module VagrantPlugins
|
||||
end
|
||||
end
|
||||
|
||||
class CommandArgumentsFromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type: SDK::Command::Arguments)],
|
||||
output: Type::CommandArguments,
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
|
||||
def converter(proto)
|
||||
args = proto.args.to_a
|
||||
flags = Hash.new.tap do |flgs|
|
||||
proto.flags.each do |f|
|
||||
if f.type == :BOOL
|
||||
flgs[f.name] = f.bool
|
||||
else
|
||||
flgs[f.name] = f.string
|
||||
end
|
||||
end
|
||||
end
|
||||
Type::CommandArguments.new(args: args, flags: flags)
|
||||
end
|
||||
end
|
||||
|
||||
class CommandProtoFromSpec < Mapper
|
||||
def initialize
|
||||
super(
|
||||
|
||||
@ -2,6 +2,7 @@ module VagrantPlugins
|
||||
module CommandServe
|
||||
class Type
|
||||
autoload :Boolean, Vagrant.source_root.join("plugins/commands/serve/type/boolean").to_s
|
||||
autoload :CommandArguments, Vagrant.source_root.join("plugins/commands/serve/type/command_arguments").to_s
|
||||
autoload :Direct, Vagrant.source_root.join("plugins/commands/serve/type/direct").to_s
|
||||
|
||||
attr_accessor :value
|
||||
|
||||
64
plugins/commands/serve/type/command_arguments.rb
Normal file
64
plugins/commands/serve/type/command_arguments.rb
Normal file
@ -0,0 +1,64 @@
|
||||
module VagrantPlugins
|
||||
module CommandServe
|
||||
class Type
|
||||
class CommandArguments < Type
|
||||
|
||||
attr_reader :arguments, :flags
|
||||
|
||||
def initialize(args: nil, flags: nil, value: nil)
|
||||
if args.nil? && flags.nil? && value.nil?
|
||||
raise ArgumentError,
|
||||
"Args and flags or value is required"
|
||||
end
|
||||
if value && (!value.respond_to?(:flags) || !value.respond_to?(:args))
|
||||
raise TypeError,
|
||||
"Value provided must respond to #flags and #args"
|
||||
end
|
||||
if value
|
||||
@arguments = Array(value.args)
|
||||
@flags = value.flags || {}
|
||||
else
|
||||
@arguments = Array(args)
|
||||
@flags = flags || {}
|
||||
end
|
||||
|
||||
@arguments.each do |v|
|
||||
if !v.is_a?(String)
|
||||
raise TypeError,
|
||||
"Expecting `String' type for argument, received `#{v.class}'"
|
||||
end
|
||||
end
|
||||
|
||||
if !@flags.is_a?(Hash)
|
||||
raise TypeError,
|
||||
"Expecting `Hash' type for flags, received `#{@flags.class}'"
|
||||
end
|
||||
|
||||
@flags.each_pair do |k,v|
|
||||
if !k.is_a?(String) && !k.is_a?(Symbol)
|
||||
raise TypeError,
|
||||
"Expecting `String' or `Symbol' for flag key, received `#{k.class}'"
|
||||
end
|
||||
if !v.is_a?(String) && !v.is_a?(TrueClass) && !v.is_a?(FalseClass) && !v.is_a?(Symbol)
|
||||
raise TypeError,
|
||||
"Expecting `String' or `Boolean' for flag value, received `#{v.class}'"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def value
|
||||
arguments +
|
||||
flags.map { |k,v|
|
||||
if v == true
|
||||
"--#{k}"
|
||||
elsif v == false
|
||||
"--no-#{k}"
|
||||
else
|
||||
"--#{k}=#{v}"
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user