{"id":18319781,"url":"https://github.com/redding/dk","last_synced_at":"2025-06-22T04:07:36.833Z","repository":{"id":59152943,"uuid":"61643525","full_name":"redding/dk","owner":"redding","description":"\"Why'd you name this repo Dk?\" ... \"I [D]on't [k]now\" (this is some automated task runner thingy ala cap/rake)","archived":false,"fork":false,"pushed_at":"2019-04-13T12:08:08.000Z","size":177,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-15T05:48:43.410Z","etag":null,"topics":["build-automation","deployer","ruby","task-framework"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/redding.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-21T15:04:34.000Z","updated_at":"2019-04-13T12:10:05.000Z","dependencies_parsed_at":"2022-09-13T11:01:27.037Z","dependency_job_id":null,"html_url":"https://github.com/redding/dk","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/redding/dk","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redding","download_url":"https://codeload.github.com/redding/dk/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fdk/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261233298,"owners_count":23128200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["build-automation","deployer","ruby","task-framework"],"created_at":"2024-11-05T18:14:14.024Z","updated_at":"2025-06-22T04:07:31.816Z","avatar_url":"https://github.com/redding.png","language":"Ruby","readme":"# Dk\n\n\"Why'd you name this repo dk?\" \"Don't know\"\n\nThis is some automated task runner thingy ala cap/rake (except without all the drama, breaking changes and difficult-to-test-ness).  You define tasks using classes; these tasks do stuff (maybe run some local or remote system commands?); you write tests for these tasks; you run them with a CLI.\n\n## Usage\n\nFirst define a task:\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  desc \"my task that does something great\"\n\n  def run!\n    log_info \"this task does something\"\n\n    # ... do something ...\n  end\n\nend\n```\n\nNow route this task with a name so it can be run from the CLI:\n\n```ruby\n# in config/dk.rb or whatever\nrequire 'dk'\nrequire 'my_task'\n\nDk.configure do\n\n  task 'my-task', MyTask\n\nend\n```\n\nNow run this task using the CLI:\n\n```\n$ dk -T\nmy-task # my task that does something great\n$ dk my-task\n```\n\nSee [dk-abdeploy](https://github.com/redding/dk-abdeploy#usage) for another example using Dk tasks to deploy code.\n\n### CLI\n\n```\n$ dk --help\nUsage: dk [TASKS] [options]\n\nTasks:\n    my-other-task # my other task that does something great\n    my-task       # my task that does something great\n\nOptions:\n    -T, --[no-]list-tasks            list all tasks available to run\n    -d, --[no-]dry-run               run the tasks without executing any local/remote cmds\n    -t, --[no-]tree                  print out the tree of tasks/sub-tasks that would be run\n    -v, --[no-]verbose               run tasks showing verbose (ie debug log level) details\n        --version\n        --help\n```\n\n##### `--list-tasks` option\n\nUse this option (or its `-T` abbrev) to list out all tasks that are available to run.  This is similar to the `-T` option on cap and rake.\n\n```\n$ dk -T\nmy-other-task # my other task that does something great\nmy-task       # my task that does something great\n```\n\n##### `--dry-run` option\n\nThis option runs the tasks and logs everything just like the live runner does.  However, this runner disables all system commands: both local commands and remote ssh commands.  Use this to see what a task would do do without running any of the system commands.\n\n##### `--tree` option\n\nThis option runs the tasks like the live runner does and disables all system commands like the `--dry-run` option does.  However, this option also disables all logging and tracks all tasks and sub-tasks that are run.  It then outputs the tree of tasks that was run:\n\nTODO: show task tree output example\n\nUse this to show the user all the tasks/sub-tasks that are run and which parent tasks are running them.\n\n##### `--verbose` option\n\nThis option runs tasks showing more verbose output/details (ie it sets the logger's stdout log level to 'debug').  All Task `log_debug` messages will be shown on stdout with this option.  This option also makes the stdout and file logs identical.\n\n### Config\n\nDk stores settings in a config.  To modify the settings add a configure block:\n\n```ruby\nrequire 'dk'\n\nDk.configure do\n\n  # settings go here...\n\nend\n```\n\nThere are a number of DSL settings that can be configured.  Use these to set param values, set default ssh settings, configure tasks that can be called from the CLI, etc.\n\n##### `set_param`\n\n```ruby\nrequire 'dk'\n\nDk.configure do\n\n  set_param 'app_name',         'myapp'\n  set_param 'number_of_things', 5\n  # ...\n\nend\n```\n\nUse the `set_param` method to set new global param values.  Any tasks that are run will have these param values available to them using the tasks's `params` helper method.\n\n##### `before`, `prepend_before`, `after`, `prepend_after`\n\nYou can configure tasks as callbacks on other tasks using these helper methods:\n\n```ruby\nrequire 'dk'\n\nDk.configure do\n\n  before         MyTask, MyBeforeTask\n  prepend_before MyTask, MyOtherBeforeTask, 'some_param' =\u003e 'some_value'\n  after          MyTask, MyAfterTask,       'some_param' =\u003e 'some_value'\n  prepend_after  MyTask, MyOtherAfterTask\n\nend\n```\n\nEach callback can be optionally configured with a set of params.  Callbacks can either be appended or prepended (this can be especially useful when you want to control the order 3rd-party tasks are run in).\n\nThe callback tasks will be run in the order they are added before/after the `run!` method of the task they are added to.  The [`halt` task helper](https://github.com/redding/dk#halt) does not stop these callbacks from running.\n\n##### `ssh_hosts`, `ssh_args`, `host_ssh_args`\n\n```ruby\nrequire 'dk'\n\nDk.configure do\n\n  ssh_hosts 'all_servers', '1.example.com',\n                           '2.example.com',\n                           '3.example.com',\n                           'user@4.example.com',\n                           'user@5.example.com'\n\n  ssh_hosts 'primary_server', '1.example.com'\n\n  ssh_hosts 'web_servers', '1.example.com',\n                           '2.example.com'\n\n  ssh_hosts 'db_server', '3.example.com'\n\n  ssh_hosts 'bg_servers', 'user@4.example.com',\n                          'user@5.example.com'\n\n  # these are custom args to use on all SSH cmds\n  ssh_args \"-o ForwardAgent=yes \"\\\n           \"-o ControlMaster=auto \"\\\n           \"-o ControlPersist=60s \"\\\n           \"-o UserKnownHostsFile=/dev/null \"\\\n           \"-o StrictHostKeyChecking=no \"\\\n           \"-o ConnectTimeout=10 \"\\\n           \"-o LogLevel=quiet \"\n\n  # these two hosts use custom SSH ports\n  host_ssh_args 'user@4.example.com', '-p 12345'\n  host_ssh_args 'user@5.example.com', '-p 12345'\n\nend\n```\n\nUse the `ssh_hosts` method to define a named set of hosts.  These hosts can now be referred to by name when running `ssh` cmds in tasks.\n\nUse the `ssh_args` and `host_ssh_args` methods to configure ssh cmd arguments to apply to all SSH cmds.  The host-specific args are only applied to SSH cmds run on the specific host.\n\n##### `log_pattern`, `log_file`, `log_file_pattern`\n\n```ruby\nrequire 'dk'\n\nDk.configure do\n\n  log_pattern '[%-5l] : %m\\n' # [INFO] : blah blah\\n\"\n\n  log_file         \"log/dk.log\"       # log all task run details to this file\n  log_file_pattern '[%d %-5l] : %m\\n' # [\u003cdatetime\u003e INFO] : blah blah\\n\"\n\nend\n```\n\nUse the `log_pattern` and `log_file_pattern` methods to override the default log entry format for stdout and file logging respectively.  By default, stdout logs just log the message while the file logs log the date level and message.  Uese these to tweak as desired.  Dk uses [Logsly](https://github.com/redding/logsly) for its loggers so check out its README for [details on using patterns](https://github.com/redding/logsly#patterns).\n\nUse the `log_file` method to turn on file logging.  If given a file path, Dk will log all task run details (verbosely) to the file.  This is handy when problems occur running tasks in non-verbose mode.  File logging is always done verbosely so you can use this to refere to the details of previous task runs even though you may not have seen these details in your stdout logs.\n\n##### `task`\n\n```ruby\nrequire 'dk'\n\nDk.configure do\n\n  task 'deploy',      MyDeployTask\n  task 'deploy:setup' MyDeploySetupTask\n\n  # ...\n\nend\n```\n\nUse the `task` method to configure tasks that are runnable via the CLI.  This also will display information about the task when using the `--help option in the CLI.\n\n**Note**: only tasks configured using this method will be runnable from the CLI.\n\n##### `stub_dry_tree_cmd`, `stub_dry_tree_ssh`\n\n```ruby\nrequire 'dk'\n\nDk.configure do\n\n  stub_dry_tree_cmd(\"ls -la\") do |spy| # spy is a Local::CmdSpy\n    spy.stdout = \"...\"\n  end\n\n  stub_dry_tree_ssh(\"ls -la\") do |spy| # spy is a Remote::CmdSpy\n    spy.exitstatus = 1 # simulare the ssh call failing\n  end\n\n  # ...\n\nend\n```\n\nUse the `stub_dry_tree_cmd` and `stub_dry_tree_ssh` methods to add cmd and ssh stubs when using `--dry-run` or `--tree`. This can be used to control the stdout, stderr or exitstatus of a command. This is useful when a task uses the output of one command for another command or when the task does different logic depending on if a command succeeds or fails.\n\n### Task\n\n#### Helper Methods\n\nThe `Dk::Task` mixin provides a bunch of helper methods to make writing tasks easier and for commonizing common functions.\n\n##### `params`, `set_param`, `param?`, `try_param`\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  def run!\n    param?('some_param')    #=\u003e true\n    params['some_param']    #=\u003e \"some value\"\n    try_param('some_param') #=\u003e \"some value\"\n\n    param?('some_other_param')    #=\u003e false\n    params['some_other_param']    #=\u003e Dk::NoParamError\n    try_param('some_other_param') #=\u003e nil\n\n    set_param('some_other_param', 'some other value')\n    param?('some_other_param')    #=\u003e true\n    params['some_other_param']    #=\u003e \"some other value\"\n    try_param('some_other_param') #=\u003e \"some other value\"\n  end\n\nend\n```\n\nUse the `param` helper to access named params that the task was run with.  Params can be added two ways: globally [from the main config](https://github.com/redding/dk#set_param) or from [callback definitions](https://github.com/redding/dk#task-callbacks) and [`run_task` calls](https://github.com/redding/dk#run_task).\n\nUse the `set_param` method to set new global param values like you would on the main config.  Any subsequent tasks that are run will have these param values available to them.\n\nUse the `param?` method to check whether a specific param has been set.  Prefer this over `params.key?` as the task params have special logic for interacting with any runner params that makes the traditional `key?` method unreliable.\n\nBy default, the params will raise an exception if you try and access a missing key (it will raise Dk::NoParamerror).  The idea is that Dk will loudly notify you if you are accessing a param you expect to be set and it is not set.  However, this may not be appropriate for every scenario, therefore you can use the `try_param` helper and it will just return `nil` if the param is not set.\n\n##### `before`, `prepend_before`, `after`, `prepend_after`\n\n[Like in the Config](https://github.com/redding/dk#before-prepend_before-after-prepend_after), you can add tasks as callbacks on other tasks using these helper methods:\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  def run!\n    before         SomeTask, MyBeforeTask\n    prepend_before SomeTask, MyOtherBeforeTask, 'some_param' =\u003e 'some_value'\n    after          SomeTask, MyAfterTask,       'some_param' =\u003e 'some_value'\n    prepend_after  SomeTask, MyOtherAfterTask\n  end\n\nend\n```\n\nEach callback can be optionally configured with a set of params.  Callbacks can either be appended or prepended (this can be especially useful when you want to control the order 3rd-party tasks are run in).  Once a callback is added, it works just as it would if added via the Config.\n\n##### `ssh_hosts`\n\nUse the `ssh_hosts` helper to set new ssh host lists values like you would on the main config:\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  def run!\n    ssh_hosts('my-app-servers') # =\u003e nil\n    ssh_hosts('my-web-servers') # =\u003e nil\n\n    ssh_hosts 'my-app-servers', '1.example.com'\n    ssh_hosts 'my-web-servers', '2.example.com', '3.example.com'\n\n    ssh_hosts('my-app-servers') # =\u003e ['1.example.com']\n    ssh_hosts('my-web-servers') # =\u003e ['2.example.com', '3.example.com']\n  end\n\nend\n```\n\nAny subsequent tasks that are run will have these ssh hosts available to their `ssh` commands.\n\n##### `run_task`\n\nUse the `run_task` helper to run other tasks:\n\n```ruby\nrequire 'dk/task'\nrequire 'my_other_task'\n\nclass MyTask\n  include Dk::Task\n\n  def run!\n    # ... do something before ...\n\n    run_task MyOtherTask, 'some_param' =\u003e 'some value'\n\n    # ... do something after ...\n  end\n\nend\n```\n\nThis method takes an optional set of param values.  Any params given will be merged onto the global config params and made available to just the task being run.\n\n##### `cmd`, `cmd!`\n\nUse the `cmd` and `cmd!` helpers to run local system cmds:\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  def run!\n    cmd \"ls -la\", :env =\u003e { 'PWD' =\u003e '/path/to/some/dir' }\n    cmd! \"test -d some_file\"\n  end\n\nend\n```\n\nPass them a string system command to run and it runs it using [Scmd](https://github.com/redding/scmd).  You can [optionally pass in an `:env` param](https://github.com/redding/scmd#environment-variables) with any ENV vars that need to be set.  A `Dk::Local::Cmd` object is returned so you can access data such as the `stdout`, `stderr` and whether the command was successful or not.\n\nThe `cmd!` helper is identical to the `cmd` helper except that it raises a `Dk::Task::CmdRunError` if the command was not successful.\n\n##### `start`\n\nUse the `start` helper to asynchronously run a local system cmd:\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  def run!\n    # run, but don't block waiting on an exitstatus\n    server_cmd = start \"bin/server\"\n\n    server_cmd.running?   # =\u003e true\n    server_cmd.pid        # =\u003e 12345\n    server_cmd.exitstatus # =\u003e nil\n\n    # do other stuff...\n\n    server_cmd.wait       # wait indefinitely until cmd exits\n    server_cmd.running?   # =\u003e false\n    server_cmd.pid        # =\u003e 12345\n    server_cmd.exitstatus # =\u003e 0\n  end\n\nend\n```\n\nOR, you can also asynchorously run with timeouts:\n\n```ruby\n# run, but don't block waiting on an exitstatus\nserver_cmd = start \"bin/server\"\n\nbegin\n  server_cmd.wait(10)\nrescue Dk::CmdTimeoutError =\u003e err\n  cmd.stop(10) # attempt to stop the cmd nicely, kill if doesn't stop in time\n  cmd.kill     # OR, just kill the cmd now\nend\n```\n\nThis helper behaves just like `cmd` and `cmd!`. The only difference is it won't block waiting for the system command to run. It uses [Scmd](https://github.com/redding/scmd) to run system commmands and proxies most of its command API - see [the Scmd usage docs](https://github.com/redding/scmd#usage) for additional reference.\n\n##### `ssh`, `ssh!`\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  def run!\n    hosts = ['1.example.com', 'user@2.example.com']\n    ssh \"ls -la\", :hosts =\u003e hosts\n    ssh! \"test -d some_file\", :hosts =\u003e hosts\n  end\n\nend\n```\n\nUse the `ssh` helper to run remote system cmds on hosts using ssh.  Like the normal `cmd*` helpers, pass it a string system command and it runs the command on each host using by creating a local system cmd that runs ssh.  Like `cmd*` you can optionally pass in an `:env` param with any ENV vars that need to be set locally.  Similare to `cmd*`, a `Dk::Remote::Cmd` object is returned so you can access whether the command was successful or not.\n\nThe `ssh!` helper is identical to the `ssh` helper except that it raises a `Dk::Task::SSHRunError` if the command was not successful.\n\n##### `:dry_tree_run` cmd/ssh opt\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  def run!\n    cmd! \"test -d some_file\", :dry_tree_run =\u003e true\n    ssh! \"test -d some_file\", :dry_tree_run =\u003e true\n  end\n\nend\n```\n\nUse this option to force the cmd/ssh to *always* run, even using the `--dry-run` and `--tree` CLI options (which put Scmd in test mode and don't run any cmds by default).  The idea is that some cmds are essential to the running of the task and if they don't actually run, the task will error out.  This is handy for tasks that don't change anything they just read data and that data is used to determine how a task should proceed.  This allows you to still get the advantages of the dry run and tree CLI options.\n\n##### `halt`\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  def run!\n    # ... do something ...\n\n    halt if nothing_more_to_do\n\n    # ... otherwise keep going ...\n  end\n\nend\n```\n\nUse the `halt` helper to stop executing the current task.  This doesn't halt the entire run (callbacks, subsequent tasks, etc).  It only halts the current task.  If you want to stop running everything, just raise an exception.\n\n##### `log_info`, `log_debug`, `log_error`\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  def run!\n    log_info  \"will always show up in the CLI output\"\n    log_debug \"will only show up in the CLI output in verbose mode\"\n    log_error \"will always show, but has special error handling\"\n  end\n\nend\n```\n\nUse the `log_*` helpers to log information as the task is running.  Each corresponds to a logger level.  The CLI logs to stdout on the INFO level by default.  When run in verbose mode, it logs to stdout on the DEBUG level.  If an optional log file has been configured, the CLI will log to the file on DEBUG level regardless of any verbose mode setting.\n\nYou can optionally style your log messages:\n\n```ruby\nlog_info \"my message\", :red, :on_white\nlog_info \"my \" + Dk::Ansi.styled_msg(\"special\", :bold, :blue) + \" message\"\n```\n\nSee `Dk::Ansi::CODES.keys` for a list of available style names.\n\n#### Task Descriptions\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  desc \"This is a task that does something\"\n\n  def run!\n    # ... do something ...\n  end\n\nend\n```\n\nThe descriptions of any routed tasks will be displayed when running `--help` in the CLI.\n\n#### Task Callbacks\n\nYou can configure other tasks as callbacks to your task:\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  before         MyBeforeTask\n  prepend_before MyOtherBeforeTask, 'some_param' =\u003e 'some_value'\n  after          MyAfterTask,       'some_param' =\u003e 'some_value'\n  prepend_after  MyOtherAfterTask\n\n  def run!\n    # ... do something ...\n  end\n\nend\n```\n\nEach callback can be optionally configured with a set of params.  These tasks will be run in the order they are added before/after the `run!` method of the current task.  The [`halt` helper](https://github.com/redding/dk#halt) does not stop these callbacks from running.\n\n#### Default SSH Hosts\n\nYou can configure a default list of hosts to use for ssh commands made in a Task.  These hosts will be used on all commands that don't specify a custom `:hosts` option:\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  ssh_hosts ['1.example.com', 'user@2.example.com']\n\n  def run!\n    ssh('ls -la') # =\u003e will ssh to 1.example.com and user@2.example.com\n    ssh('ls -la', :hosts =\u003e ['3.example.com']) # =\u003e will ssh to 3.example.com\n  end\n\nend\n```\n\nYou can also specify a named list of hosts that have been configured:\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  ssh_hosts 'my-app-servers'\n\n  def run!\n    ssh('ls -la') # =\u003e will ssh to the configured 'my-app-servers' hosts\n  end\n\nend\n```\n\nYou can also specify a proc that will be instance eval'd on the task instance:\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  ssh_hosts{ params['some-servers'] }\n\n  def run!\n    ssh('ls -la') # =\u003e will ssh to the hosts specified by the 'some-servers' param\n  end\n\nend\n```\n\n#### Ensure Tasks Only Run Once\n\n```ruby\nrequire 'dk/task'\n\nclass MyTask\n  include Dk::Task\n\n  run_only_once true\n\n  def run!\n    # ... do something ...\n  end\n\nend\n```\n\nUse this setting to ensure that a task only runs once no matter how many other tasks or sub-tasks either run it manually or configure it as a callback.  This is useful for \"gatekeeper\" tasks such as tasks that validate params, etc.  By default, tasks can run multiple times.\n\n#### Testing Tasks\n\nDk comes with a test runner and some test helpers to assist in unit testing tasks.  The test runner doesn't run any callback tasks and captures spies for the task, cmd and ssh calls it runs.  It also turns off any logging.\n\n```ruby\n# in your test file or whatever\n\ninclude Dk::Task::TestHelpers\n\ntest \"my task should do something\" do\n  runner = test_runner(MyTask)\n  runner.run\n  runner.runs #=\u003e [TaskRun, Local::CmdSpy, Remote::CmdSpy, ... ]\n\n  # make assertions that the logic you expect to run actually ran\n  task = runner.task #=\u003e MyTask instance\n\n  # make assertions about your task instance if needed\nend\n```\n\nDk's test helpers include methods for stubbing cmd and ssh calls.  This allows controlling cmd/ssh behavior such as exit status, stdout and stderr.  If a task expects to use the stdout/stderr or does special handling when a call errors then the stubbing methods can be used for testing.\n\n```ruby\n# in your test file or whatever\n\ninclude Dk::Task::TestHelpers\n\ntest \"my task should do something\" do\n  runner = test_runner(MyTask)\n  runner.stub_cmd(\"ls -la\") do |spy| # spy is a Local::CmdSpy\n    spy.stdout = \"...\"\n  end\n  runner.stub_ssh(\"ls -la\", :input =\u003e 'whatever') do |spy| # spy is a Remote::CmdSpy\n    spy.exitstatus = 1 # simulare the ssh call failing\n  end\n\n  # ....\nend\n```\n\nYou can even stub with procs.  Those procs will be instance eval'd against the task to determine if the stub is a match.  This allows you to stub the same way the call is made in the task (which is handy when the cmds are driven by dynamic values on the task instance).\n\n```ruby\n# in your test file or whatever\n\ninclude Dk::Task::TestHelpers\n\ntest \"my task should do something\" do\n  runner = test_runner(MyTask)\n  runner.stub_cmd(proc{ self.class.some_cmd_str }, :opts =\u003e proc{ some_opts }) do |spy| # spy is a Local::CmdSpy\n    spy.stdout = \"...\"\n  end\n  runner.stub_ssh(\"ls -la\", :input =\u003e proc{ some_task_input_val }) do |spy| # spy is a Remote::CmdSpy\n    spy.exitstatus = 1 # simulare the ssh call failing\n  end\n\n  # ....\nend\n```\n\n## Dk 3rd-party gems\n\nThese gems extend Dk for specific behavior:\n\n* [dk-pkg](https://github.com/redding/dk-pkg): Dk logic for installing pkgs\n* [dk-abdeploy](https://github.com/redding/dk-abdeploy): Dk tasks that implement the A/B deploy scheme\n* [dk-dumpdb](https://github.com/redding/dk-dumpdb): Build Dk tasks to dump and restore your databases\n\n(if you build your own gem that extends Dk, let us know and we'll link it here)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'dk'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install dk\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Added some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredding%2Fdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fdk/lists"}