Skip to content

Commit

Permalink
Clarify usage of --arguments in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianna-chang-shopify committed Jun 3, 2021
1 parent 8c3fc90 commit 7d6855a
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@ $ bundle exec maintenance_tasks perform Maintenance::ImportPostsTask --csv 'path
```

To run a Task that takes arguments from the command line, use the --arguments
option, ensuring that the argument value is passed as a string:
option, passing arguments as a set of <key>:<value> pairs:

```bash
$ bundle exec maintenance_tasks perform Maintenance::ParamsTask --arguments=post_ids:"1,2,3"
$ bundle exec maintenance_tasks perform Maintenance::ParamsTask --arguments post_ids:1,2,3 content:"Hello, World!"
```

You can also run a Task in Ruby by sending `run` with a Task name to Runner:
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/maintenance_tasks/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def run
task = Runner.run(
name: params.fetch(:id),
csv_file: params[:csv_file],
arguments: task_arguments.to_h,
arguments: params.fetch(:task_arguments, {}).permit!.to_h,
)
redirect_to(task_path(task))
rescue ActiveRecord::RecordInvalid => error
Expand All @@ -42,11 +42,6 @@ def run

private

def task_arguments
return {} unless params[:task_arguments].present?
params.require(:task_arguments).permit!
end

def set_refresh
@refresh = 3
end
Expand Down
5 changes: 2 additions & 3 deletions app/models/maintenance_tasks/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,9 @@ def arguments_match_task_attributes
invalid_argument_keys = arguments.keys - task.attribute_names
if invalid_argument_keys.any?
error_message = <<~MSG.squish
used not specified as parameters on Task:
#{invalid_argument_keys.join(", ")}
Unknown parameters: #{invalid_argument_keys.map(&:to_sym).join(", ")}
MSG
errors.add(:arguments, error_message)
errors.add(:base, error_message)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/maintenance_tasks/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def exit_on_failure?

# Specify arguments to supply to a Task supporting parameters
option :arguments, type: :hash, desc: "Supply arguments for a Task that "\
"accepts parameters as a set of key-value pairs. Ensure that argument
values are provided as strings."
"accepts parameters as a set of <key>:<value> pairs."

# Command to run a Task.
#
Expand Down
4 changes: 2 additions & 2 deletions test/models/maintenance_tasks/run_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RunTest < ActiveSupport::TestCase
)
refute_predicate run, :valid?
assert_equal run.errors.full_messages.first,
"Arguments used not specified as parameters on Task: bad_argument"
"Unknown parameters: bad_argument"
end

test "invalid if arguments are supplied but Task does not support parameters" do
Expand All @@ -45,7 +45,7 @@ class RunTest < ActiveSupport::TestCase
)
refute_predicate run, :valid?
assert_equal run.errors.full_messages.first,
"Arguments used not specified as parameters on Task: post_ids"
"Unknown parameters: post_ids"
end

test "#persist_progress persists increments to tick count and time_running" do
Expand Down

0 comments on commit 7d6855a

Please sign in to comment.