From 7b82ba7ca0c6bba68c6652738eb7e4ffb3061ac3 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 19 Jan 2022 10:08:30 -0800 Subject: [PATCH] Add ruby local command arguments type and mapper --- plugins/commands/serve/mappers/command.rb | 24 +++++++ plugins/commands/serve/type.rb | 1 + .../commands/serve/type/command_arguments.rb | 64 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 plugins/commands/serve/type/command_arguments.rb diff --git a/plugins/commands/serve/mappers/command.rb b/plugins/commands/serve/mappers/command.rb index 10bdc995c..efd779e3c 100644 --- a/plugins/commands/serve/mappers/command.rb +++ b/plugins/commands/serve/mappers/command.rb @@ -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( diff --git a/plugins/commands/serve/type.rb b/plugins/commands/serve/type.rb index bcdd8cd37..b9b3948ec 100644 --- a/plugins/commands/serve/type.rb +++ b/plugins/commands/serve/type.rb @@ -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 diff --git a/plugins/commands/serve/type/command_arguments.rb b/plugins/commands/serve/type/command_arguments.rb new file mode 100644 index 000000000..a4fa4037e --- /dev/null +++ b/plugins/commands/serve/type/command_arguments.rb @@ -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