kcmerrill / alfred

(v0.2) Even Batman needs a little help. Task runner. Automator. Build system.
MIT License
63 stars 14 forks source link

Register multiple variables and postprocess output via Masterminds/sprig #51

Closed iilyak closed 6 years ago

iilyak commented 6 years ago

I am trying to refactor my draft of a pipeline to use DRY principle. It turns out to be quite hard. How hard it would be to support the following?

new:
  summary: Add new item
  command: echo {{ index .Args 0 }}
  register: file_name
  tasks:
    project
    project.edit

project:
  summary: Select project
  command: ls projects | bin/peco --prompt "project>"
  register: 
     project: projects/{{ index .Vars "project" | trim }}
     file_name: projects/{{ index .Vars "project"}}/{{index .Vars "file_name" | trim}}

Notice two things:

The new task could be improved as well (maybe):

new:
  summary: Add new item
  command: none
  register: 
      file_name:  {{ index .Args 0 | trim }}
  tasks:
    project
    project.edit
kcmerrill commented 6 years ago

Can you give me some more information on what the end goal should be.

It looks like you're trying to do something based on a list of projects? Not 100% sure, but this might be solved by the for loop. Perhaps not, either way let me know what youre trying to get out and I"d be happy to take a peek.

iilyak commented 6 years ago

I wanted to implement a notes taking feature for GTD workflow. I wanted to write alfred new <note-file-name> which would trigger the workflow as follows.

  1. ask which project the note should belong to (using https://github.com/peco/peco). The list of available project is list of directories in projects directory.
  2. Construct full path to new file projects/<project>/<note-file-name>.md
  3. If file does exists alfred should complain and abort
  4. If file doesn't exist project.tepmplate task would ask for template to use. The list of templates is list of files in projects/<project>/templates directory.
  5. It copies the template into path constructed in step 2
  6. It opens the file using editor

This workflow is working but I have to construct the full path everywhere. I cannot construct it in project task. I was thinking to implement internal strings.trim task and solve the problem as:

project:
   command:  alfred string.trim $(ls projects | bin/peco --prompt "project>")
   register: project

string.trim:
   command: echo {{ index .Args 0 | trim }} 

However generic string helper wouldn't work because:

  1. I need to concatenate <project> and <file_path>.

So I would end up with a bunch of private helper tasks.

kcmerrill commented 6 years ago

05:01 PM ✔ kcmerrill  tmp ] tree projects/
projects/
└── myprojectfolder
    └── my.cool.note

1 directory, 1 file
05:02 PM ✔ kcmerrill  tmp ] cat alfred.yml
note:
    summary: Add a new note
    usage: alfred note <note.name>
    exit: 42
    commands: |
        PF=$(echo "myprojectfolder") # I'm cheating here, but I'm assuming peco simply returns a string? if not let me know.
        echo $PF
        mkdir -p "/tmp/projects/${PF}"
        vim "/tmp/projects/${PF}/{{ index .Args 0 }}"
05:02 PM ✔ kcmerrill  tmp ]```
kcmerrill commented 6 years ago

Another thing ...

I'd simply pass .Args 0 throughout all of my tasks, no need to register it IMO.

So just assume all of your tasks, the first argument takes the filename, so task({{ index .Args 0 }}, other stuff, etc, etc)

khia commented 6 years ago

Thank you for the explanation I'll figure something out.

iilyak commented 6 years ago

Thank you for adding support for multiple variables for register.

kcmerrill commented 6 years ago

👍