Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aliases to Kamal #912

Merged
merged 2 commits into from
Aug 29, 2024
Merged

Add aliases to Kamal #912

merged 2 commits into from
Aug 29, 2024

Conversation

djmb
Copy link
Collaborator

@djmb djmb commented Aug 26, 2024

Aliases are defined in the configuration file under the aliases key.

The configuration is a map of alias name to command. When we run the command the we just do a literal replacement of the alias with the string.

So if we have:

aliases:
  console: app exec -r console -i --reuse "rails console"

Then running kamal console -r workers will run the command

$ kamal app exec -r console -i --reuse "rails console" -r workers

Because of the order Thor parses the arguments, this allows us to override the role from the alias command.

There might be cases where we need to munge the command a bit more but that would involve getting into Thor command parsing internals, which are complicated and possibly subject to change.

There's a chance that your aliases could conflict with future built-in commands, but there's not likely to be many of those and if it happens you'll get a validation error when you upgrade.

Thanks to @dhnaranjo for the idea!

validate_type! value, Hash
when "labels"
validate_hash_of! value, example_value.first[1].class
validate_type! validation_config, example.class
Copy link
Collaborator Author

@djmb djmb Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously all second level config items were expected to be hashes, but for aliases they are strings, so we need to expand the config validation to handle that.

Aliases are defined in the configuration file under the `aliases` key.

The configuration is a map of alias name to command. When we run the
command the we just do a literal replacement of the alias with the
string.

So if we have:

```yaml
aliases:
  console: app exec -r console -i --reuse "rails console"
```

Then running `kamal console -r workers` will run the command

```sh
$ kamal app exec -r console -i --reuse "rails console" -r workers
```

Because of the order Thor parses the arguments, this allows us to
override the role from the alias command.

There might be cases where we need to munge the command a bit more but
that would involve getting into Thor command parsing internals,
which are complicated and possibly subject to change.

There's a chance that your aliases could conflict with future built-in
commands, but there's not likely to be many of those and if it happens
you'll get a validation error when you upgrade.

Thanks to @dhnaranjo for the idea!
If you can have an alias like:

```
aliases:
  rails: app exec -p rails
```

Then `kamal rails db:migrate:status` will execute
`kamal app exec -p rails db:migrate:status`.

So this works, we'll allow multiple arguments `app exec` and
`server exec` to accept multiple arguments.

The arguments are combined by simply joining them with a space. This
means that these are equivalent:

```
kamal app exec -p rails db:migrate:status
kamal app exec -p "rails db:migrate:status"
```

If you want to pass an argument with spaces, you'll need to quote it:

```
kamal app exec -p "git commit -am \"My comment\""
kamal app exec -p git commit -am "\"My comment\""
```
@djmb
Copy link
Collaborator Author

djmb commented Aug 28, 2024

I've added 579e169, so that commands can be combined for app exec and server exec.

This means we can have an alias like rails: app exec -p rails and then run kamal rails db:migrate.

@dhh
Copy link
Member

dhh commented Aug 28, 2024

This is going to be so nice. We should define a few of the stock ones we use for Rails, like console, in the Rails' default config/deploy.yml file when this is released 👌

@djmb djmb merged commit b7382ce into main Aug 29, 2024
9 checks passed
@djmb djmb deleted the alias branch August 29, 2024 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants