pyinfra icon indicating copy to clipboard operation
pyinfra copied to clipboard

Run arbitrary operations through CLI

Open href opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe

The pyinfra CLI can be used to directly run operations as follows:

$ pyinfra INVENTORY server.user pyinfra home=/home/pyinfra

The code is clever enough to even allow fur custom user modules to be loaded. That is, if I have a foo.bar operation in my environment, it can be called as follows:

$ pyinfra INVENTORY foo.bar foo bar=baz

The only thing: The code is rather rigid and only tries this route, if the passed string has exactly one period in it: https://github.com/Fizzadar/pyinfra/blob/6a932fd4dbf2817a7a7f71a4160881e7b31cc42a/pyinfra_cli/main.py#L354

That is, the following will not work, even though I define the baz operation in foo.bar:

$ pyinfra INVENTORY foo.bar.baz foo bar=baz

Describe the solution you'd like

Unlike I'm misunderstanding something - I am new to pyinfra - this limitation is somewhat arbitrary. I think it would make sense to lift it in one of the following ways:

  1. Just use elif len(operations[0].split(".")) >=2 - this works in my local tests.
  2. Try to import whatever string is left and if it turns out to be an operation, use it.

I would go with the first approach, as I don't see much benefit in the second one, though that might cover some esoteric uses of the import systems (think custom import hooks).

href avatar Jun 09 '22 06:06 href

👍 I like this idea, will confirm it doesn't clash with any existing functionality but a good addition for sure.

Fizzadar avatar Jun 19 '22 07:06 Fizzadar