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

Enable Environment Variable Expansion in Alias Commands for Dynamic Configuration #1167

Open
muthuishere opened this issue Oct 26, 2024 · 0 comments

Comments

@muthuishere
Copy link

muthuishere commented Oct 26, 2024

Issue Summary

The current alias configuration in the codebase only supports basic shortcuts. It doesn't allow for environment variable expansion or parameter passing. This limitation prevents users from utilizing dynamic values through environment variables, impacting tasks like database exports that rely on such configurations.

Problem

Currently, the alias configuration does not expand environment variables. For instance, when trying to use an environment variable for a database export:

aliases:
  db-export: accessory exec --quiet  --interactive --reuse db "pg_dumpall -U $DB_USER"

The $DB_USER variable is not being expanded because the code does not handle environment variable substitution. This makes it impossible to use dynamic values set in the environment for the alias commands.

Use Case

Consider a scenario where the user wants to use an alias to run a database export using a user defined by the environment variable DB_USER. The current alias setup doesn't expand $DB_USER, leading to the command not functioning as intended.

Current Code

The existing code in Kamal::Cli::Alias::Command looks like this:

class Kamal::Cli::Alias::Command < Thor::DynamicCommand
  def run(instance, args = [])
    if (_alias = KAMAL.config.aliases[name])
      Kamal::Cli::Main.start(Shellwords.split(_alias.command) + ARGV[1..-1])
    else
      super
    end
  end
end

This implementation does not expand environment variables, leading to the inability to pass dynamic values via the environment.

Proposed Fix

To handle environment variable substitution, the code can be modified to expand any environment variables found in the alias command before executing it:

class Kamal::Cli::Alias::Command < Thor::DynamicCommand
  def run(instance, args = [])
    if (_alias = KAMAL.config.aliases[name])
      # Expand environment variables in the command
      expanded_command = _alias.command.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\$\{([a-zA-Z_][a-zA-Z0-9_]*)\}/) do
        ENV[$1 || $2]
      end
      Kamal::Cli::Main.start(Shellwords.split(expanded_command) + ARGV[1..-1])
    else
      super
    end
  end
end

Explanation of the Fix

  1. Environment Variable Expansion:
    • The proposed change introduces a regular expression that searches for environment variables in two formats: $VAR_NAME and ${VAR_NAME}.
    • It uses ENV[$1 || $2] to retrieve the value from the environment, ensuring that any specified variables are substituted with their actual values.

Benefits of the Fix

  • This change will allow users to:
    • Use environment variables directly in alias commands.
    • Pass dynamic values to commands, making the system more flexible.
    • Utilize parameters set in the environment without hardcoding them into the alias configuration.

Current work around

  • use the entire command as it is to export
  • use it in makefile
db.export:
  kamal accessory exec --quiet --interactive --reuse db "pg_dumpall -U $(DB_USER)" > $(DB_NAME).sql
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

No branches or pull requests

1 participant