lets-cli/lets

Interpolation of command in docopt options

Closed this issue · 0 comments

Often we have several almost-identical commands which share most of the body.

But due to constraints to https://github.com/docopt/docopt.go we have to declare Usage with particular command name. Let me show an example:

commands:
  run:
    options: |
      Usage: lets run [--config=<config>]
    env: 
      SOME_SECRET: "123"
    depends: [build-app]
    cmd: docker-compose up app postgres
  
  run-app:
    options: |
      Usage: lets run-app [--config=<config>]
    env: 
      SOME_SECRET: "123"
    cmd: docker-compose up app

  run-debug:
    options: |
      Usage: lets run-debug [--config=<config>]
    env: 
      SOME_SECRET: "123"
    cmd: docker-compose up app-debug

We can get rid of some repetition using yaml features:

commands:
  run: &run
    options: |
      Usage: 
        lets run [--config=<config>]
        lets run-app [--config=<config>]
        lets run-debug [--config=<config>]
    env: 
      SOME_SECRET: "123"
    depends: [build-app]
    cmd: docker-compose up app postgres
  
  run-app:
    <<: *run
    cmd: docker-compose up app

  run-debug:
    <<: *run
    cmd: docker-compose up app-debug

But its still required to enumerate all run run command options in options in run command. Its bad.

What I suggest is:

  1. Add new env - LETS_COMMAND_NAME which is a command name itself.
  2. We can use this env in options and interpolate options string.
commands:
  run: &run
    options: |
      Usage:  lets ${LETS_COMMAND_NAME} [--config=<config>]
    env: 
      SOME_SECRET: "123"
    depends: [build-app]
    cmd: docker-compose up app postgres
  
  run-app:
    <<: *run
    cmd: docker-compose up app

  run-debug:
    <<: *run
    cmd: docker-compose up app-debug

Thus we do not breaking docopt contract and providing absolutely correct docopt string and its cleaner as well.