ahoy-cli / ahoy

Create self-documenting cli programs from YAML files. Easily wrap bash, grunt, npm, docker, (anything) to standardize your processes and make the lives of the people working on your project better.
http://ahoycli.com/
MIT License
265 stars 36 forks source link

Influence imports #78

Closed schuhwerk closed 1 day ago

schuhwerk commented 3 years ago

Hi! It's really nice, when you have an idea about a tool and actually find, that somebody already created it 😄 Thank you!

I'm not sure if this is the right place to ask (so I don't expect an answer and you can just close this issue).

I currently work with a structure like this:

I use some_software in different environments. They all have the same (or mostly the same) tasks, where only basics like urls need replacement. So it makes most sense (to me ;) to use the same tasks.yml for all environments, and pass different environment-variables to them.

So i tried the following:

#.ahoy.yml
commands:
  some_software:
    usage: List the commands from the imported config files.
    imports:
    - ./environment1.yml #<-- this imports tasks.yml
    - ./environment2.yml #<-- this also imports tasks.yml
#.environment1.yml
commands:
  environment1:
    usage: List tasks
    imports:
    - ./tasks.yml
#.tasks.yml
commands:
  task1:
    usage: Use env defined in parent
    cmd: echo "$SOME_ENVIRONMENT_VARIABLE followed by parameters $1"

I learned, that entrypoint is only executed, when there is actual commands (not for imports) and there can not be a cmd and import at the same time. So I don't know how to "define something" in environment1.yml. This might be related to https://github.com/ahoy-cli/ahoy/issues/21. Do you have any idea, how I could achieve this? Did I miss something?

A solution could be executing ahoy from subfolders (that contain files with environment-variables) and referencing the task.yml in a parent folder. As root stays the same (ls -la always shows the folder where you started ahoy), including the environment-variables in entrypoint in tasks.yml whould include the environment-variables from the subfolders. This takes away some of the "simplicity" though.

frankcarey commented 3 years ago

Hi! It's really nice, when you have an idea about a tool and actually find, that somebody already created it 😄 Thank you!

For sure! Thank you for being a supporter! Just seeing this now, but let me think on it a bit and check examples of what I've done in my own projects for that type of thing and get back to you soon hopefully.

frankcarey commented 3 years ago

Quick thought now - A common thing I do at least is call ahoy from ahoy. The script is very lightweight and fast, so I will do things where instead of using imports, I write some bash in one command to call ahoy -f some-commands.yaml $@ to pass along the params from the "parent" ahoy call to "children", meaning you don't have to use the imports at all. Downside is you do have to have one command in place like I think you are saying, but I haven't found that to be an issue too often. Definitely let me know if that solution works for you or how it could be better!

hanoii commented 6 months ago

I am curious on why you'd need to pass on environment variables, being that environment variables are already exactly that. The use case you are referring to as having to replace urls and stuff per environment is simply making sure those environment variables are available before calling the ahoy command, or am I missing something?

schuhwerk commented 6 months ago

Thanks for your ideas! They work nicely together 🥳

#.ahoy.yml
ahoyapi: v2
commands:
  env-1:
    usage: Commands for E1.
    cmd: export $(grep -v '^#' 1.env | xargs) && ahoy -f tasks.ahoy.yml $@
  env-2:
    usage: Commands for E2.
    cmd: export $(grep -v '^#' 2.env | xargs) && ahoy -f tasks.ahoy.yml $@
#tasks.ahoy.yml
ahoyapi: v2
commands:
  test-command:
    usage: An example of a single-line command.
    cmd: echo "Do stuff with $NAME"
#1.env
NAME="Env-1"
#2.env
NAME="Env-2"

Running ahoy env-X test-command returns "Do stuff with Env-X". That's good enough for me (so you can close the issue, if you want :)

ocean commented 1 week ago

@schuhwerk I'm glad Frank's suggestion worked for you! I'm one of the new maintainers of ahoy, and I did read your issue but couldn't really figure out exactly what was going wrong for you, sorry.

Anyway, I really like your solution, I'm in the process of updating the documentation at the moment, would it be alright if I copied your implementation as an idea to present in some sort of "Examples" page? (I would credit you and this issue of course).

schuhwerk commented 1 day ago

Hi @ocean. Of course you can use the example. Thanks for asking & maintaining ahoy!