lib/command_mapper/command.rb in command_mapper-0.3.1 vs lib/command_mapper/command.rb in command_mapper-0.3.2

- old
+ new

@@ -1,8 +1,8 @@ -require 'command_mapper/types' -require 'command_mapper/argument' -require 'command_mapper/option' +require_relative 'types' +require_relative 'argument' +require_relative 'option' require 'shellwords' module CommandMapper # @@ -48,12 +48,12 @@ # # @param [Hash{String => String}] command_env # Custom environment variables to pass to the command. # # @param [Hash{Symbol => Object}] kwargs - # Additional keywords arguments. These will be used to populate - # {#options} and {#arguments}, along with `params`. + # Additional keywords arguments. These will be used to populate the + # command's `params`. # # @yield [self] # The newly initialized command. # # @yieldparam [Command] self @@ -90,12 +90,12 @@ # # @param [Hash{Symbol => Object}] params # The option and argument values. # # @param [Hash{Symbol => Object}] kwargs - # Additional keywords arguments. These will be used to populate - # {#options} and {#arguments}, along with `params`. + # Additional keywords arguments. These will be used to populate the + # command's `params`. # # @yield [command] # The newly initialized command. # # @yieldparam [Command] command @@ -113,12 +113,12 @@ # # @param [Hash{Symbol => Object}] params # The option and argument values. # # @param [Hash{Symbol => Object}] kwargs - # Additional keywords arguments. These will be used to populate - # {#options} and {#arguments}, along with `params`. + # Additional keywords arguments. These will be used to populate the + # command's `params`. # # @yield [command] # The newly initialized command. # # @yieldparam [Command] command @@ -142,12 +142,12 @@ # # @param [Hash{Symbol => Object}] params # The option and argument values. # # @param [Hash{Symbol => Object}] kwargs - # Additional keywords arguments. These will be used to populate - # {#options} and {#arguments}, along with `params`. + # Additional keywords arguments. These will be used to populate the + # command's `params`. # # @yield [command] # The newly initialized command. # # @yieldparam [Command] command @@ -168,12 +168,12 @@ # # @param [String] mode # The IO "mode" to open the IO pipe in. # # @param [Hash{Symbol => Object}] kwargs - # Additional keywords arguments. These will be used to populate - # {#options} and {#arguments}, along with `params`. + # Additional keywords arguments. These will be used to populate the + # command's `params`. # # @yield [command] # The newly initialized command. # # @yieldparam [Command] command @@ -189,10 +189,82 @@ # Initializes and runs the command through `sudo`. # # @param [Hash{Symbol => Object}] params # The option and argument values. # + # @param [Hash{Symbol => Object}] sudo + # Additional `sudo` options. + # + # @option sudo [Boolean] :askpass + # Enables the `--askpass` `sudo` option. + # + # @option sudo [Boolean] :background + # Enables the `--background` `sudo` option + # + # @option sudo [Boolean] :bell + # Enables the `--bell` `sudo` option + # + # @option sudo [Integer] :close_from + # Enables the `--close-from=...` `sudo` option + # + # @option sudo [String] :chdir + # Enables the `--chdir=...` `sudo` option + # + # @option sudo [String] :preserve_env + # Enables the `--preseve-env=...` `sudo` option + # + # @option sudo [String, Boolean] :group + # Enables the `--preseve-env=...` `sudo` option + # + # @option sudo [Boolean] :set_home + # Enables the `--set-home` `sudo` option + # + # @option sudo [String] :host + # Enables the `--host=...` `sudo` option + # + # @option sudo [Boolean] :login + # Enables the `--login` `sudo` option + # + # @option sudo [Boolean] :remove_timestamp + # Enables the `--remove-timestamp` `sudo` option + # + # @option sudo [Boolean] :reset_timestamp + # Enables the `--reset-timestamp` `sudo` option + # + # @option sudo [Boolean] :non_interactive + # Enables the `--non-interactive` `sudo` option + # + # @option sudo [Boolean] :preserve_groups + # Enables the `--preserve-groups` `sudo` option + # + # @option sudo [String] :prompt + # Enables the `--prompt=...` `sudo` option + # + # @option sudo [String] :chroot + # Enables the `--chroot=...` `sudo` option + # + # @option sudo [String] :role + # Enables the `--role=...` `sudo` option + # + # @option sudo [Boolean] :stdin + # Enables the `--stdin` `sudo` option + # + # @option sudo [Boolean] :shell + # Enables the `--shell` `sudo` option + # + # @option sudo [String] :type + # Enables the `--type=...` `sudo` option + # + # @option sudo [Integer] :command_timeout + # Enables the `--command-timeout=...` `sudo` option + # + # @option sudo [String] :other_user + # Enables the `--other-user=...` `sudo` option + # + # @option sudo [String] :user + # Enables the `--user=...` `sudo` option + # # @param [Hash{Symbol => Object}] kwargs # Additional keyword arguments for {#initialize}. # # @yield [command] # The newly initialized command. @@ -207,13 +279,10 @@ end # # Gets or sets the command name. # - # @param [#to_s] new_name - # The optional new command name. - # # @return [String] # The command name. # # @raise [NotImplementedError] # The command class did not call {command}. @@ -709,18 +778,130 @@ end # # Runs the command through `sudo`. # - # @param [Hash{Symbol => Object}] sudo_params - # Additional keyword arguments for {Sudo#initialize}. + # @param [Boolean] askpass + # Enables the `--askpass` `sudo` option. # + # @param [Boolean] background + # Enables the `--background` `sudo` option + # + # @param [Boolean] bell + # Enables the `--bell` `sudo` option + # + # @param [Integer] close_from + # Enables the `--close-from=...` `sudo` option + # + # @param [String] chdir + # Enables the `--chdir=...` `sudo` option + # + # @param [String] preserve_env + # Enables the `--preseve-env=...` `sudo` option + # + # @param [String, Boolean] group + # Enables the `--preseve-env=...` `sudo` option + # + # @param [Boolean] set_home + # Enables the `--set-home` `sudo` option + # + # @param [String] host + # Enables the `--host=...` `sudo` option + # + # @param [Boolean] login + # Enables the `--login` `sudo` option + # + # @param [Boolean] remove_timestamp + # Enables the `--remove-timestamp` `sudo` option + # + # @param [Boolean] reset_timestamp + # Enables the `--reset-timestamp` `sudo` option + # + # @param [Boolean] non_interactive + # Enables the `--non-interactive` `sudo` option + # + # @param [Boolean] preserve_groups + # Enables the `--preserve-groups` `sudo` option + # + # @param [String] prompt + # Enables the `--prompt=...` `sudo` option + # + # @param [String] chroot + # Enables the `--chroot=...` `sudo` option + # + # @param [String] role + # Enables the `--role=...` `sudo` option + # + # @param [Boolean] stdin + # Enables the `--stdin` `sudo` option + # + # @param [Boolean] shell + # Enables the `--shell` `sudo` option + # + # @param [String] type + # Enables the `--type=...` `sudo` option + # + # @param [Integer] command_timeout + # Enables the `--command-timeout=...` `sudo` option + # + # @param [String] other_user + # Enables the `--other-user=...` `sudo` option + # + # @param [String] user + # Enables the `--user=...` `sudo` option + # # @return [Boolean, nil] # Indicates whether the command exited successfully or not. # `nil` indicates the command could not be found. # - def sudo_command(**sudo_kwargs,&block) - sudo_params = sudo_kwargs.merge(command: command_argv) + def sudo_command(askpass: nil, + background: nil, + bell: nil, + close_from: nil, + chdir: nil, + preserve_env: nil, + group: nil, + set_home: nil, + host: nil, + login: nil, + remove_timestamp: nil, + reset_timestamp: nil, + non_interactive: nil, + preserve_groups: nil, + prompt: nil, + chroot: nil, + role: nil, + stdin: nil, + shell: nil, + type: nil, + command_timeout: nil, + other_user: nil, + user: nil, &block) + sudo_params = {command: command_argv} + + sudo_params[:askpass] = askpass unless askpass.nil? + sudo_params[:background] = background unless background.nil? + sudo_params[:bell] = bell unless bell.nil? + sudo_params[:close_from] = close_from unless close_from.nil? + sudo_params[:chdir] = chdir unless chdir.nil? + sudo_params[:preserve_env] = preserve_env unless preserve_env.nil? + sudo_params[:group] = group unless group.nil? + sudo_params[:set_home] = set_home unless set_home.nil? + sudo_params[:host] = host unless host.nil? + sudo_params[:login] = login unless login.nil? + sudo_params[:remove_timestamp] = remove_timestamp unless remove_timestamp.nil? + sudo_params[:reset_timestamp] = reset_timestamp unless reset_timestamp.nil? + sudo_params[:non_interactive] = non_interactive unless non_interactive.nil? + sudo_params[:preserve_groups] = preserve_groups unless preserve_groups.nil? + sudo_params[:prompt] = prompt unless prompt.nil? + sudo_params[:chroot] = chroot unless chroot.nil? + sudo_params[:role] = role unless role.nil? + sudo_params[:stdin] = stdin unless stdin.nil? + sudo_params[:shell] = shell unless shell.nil? + sudo_params[:type] = type unless type.nil? + sudo_params[:command_timeout] = command_timeout unless command_timeout.nil? + sudo_params[:other_user] = other_user unless other_user.nil? + sudo_params[:user] = user unless user.nil? Sudo.run(sudo_params, command_env: @command_env, &block) end #