Add aliases to Kamal

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!
This commit is contained in:
Donal McBreen
2024-06-18 16:11:01 +01:00
committed by Donal McBreen
parent f48987aa03
commit b8af719bb7
19 changed files with 204 additions and 46 deletions

View File

@@ -17,6 +17,7 @@ end
DOCS = {
"accessory" => "Accessories",
"alias" => "Aliases",
"boot" => "Booting",
"builder" => "Builders",
"configuration" => "Configuration overview",
@@ -67,26 +68,27 @@ class DocWriter
output.puts
place = :new_section
elsif line =~ /^ *#/
generate_line(line, place: place)
generate_line(line, heading: place == :new_section)
place = :in_section
else
output.puts "```yaml"
output.print line
output.puts line
place = :in_yaml
end
when :in_yaml
when :in_yaml, :in_empty_line_yaml
if line =~ /^ *#/
output.puts "```"
generate_line(line, place: :new_section)
generate_line(line, heading: place == :in_empty_line_yaml)
place = :in_section
elsif line.empty?
place = :in_empty_line_yaml
else
output.puts
output.print line
output.puts line
end
end
end
output.puts "\n```" if place == :in_yaml
output.puts "```" if place == :in_yaml
end
def generate_header
@@ -98,7 +100,7 @@ class DocWriter
output.puts
end
def generate_line(line, place: :in_section)
def generate_line(line, heading: false)
line = line.gsub(/^ *#\s?/, "")
if line =~ /(.*)kamal docs ([a-z]*)(.*)/
@@ -109,7 +111,7 @@ class DocWriter
line = "#{$1}[#{titlify($2.split("/").last)}](#{$2})#{$3}"
end
if place == :new_section
if heading
output.puts "## [#{line}](##{linkify(line)})"
else
output.puts line