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

Set current repo as Env var #115

Closed
carusooo opened this issue Mar 3, 2023 · 2 comments · Fixed by #127
Closed

Set current repo as Env var #115

carusooo opened this issue Mar 3, 2023 · 2 comments · Fixed by #127
Labels
enhancement New feature or request

Comments

@carusooo
Copy link

carusooo commented Mar 3, 2023

Describe the solution you'd like
Set the current repo as an environment variable that can be accessed while processing. From the logs it looks like this is available at invocation time

[git-xargs] DEBU[2023-03-03T10:38:56-08:00] Created branch                                Branch Name=refs/heads/add-self-hosted Repo=my-cool-repo
[git-xargs] DEBU[2023-03-03T10:38:56-08:00]                                               Repo=my-cool-repo

Describe alternatives you've considered
I could parse this out of the cwd, but that seems brittle since there's a lot of nonces to prevent collisions

/private/var/folders/zc/ysm6hx_n2qng1h09htfdw_8m0000gn/T/git-xargs-my-cool-repo1360847715

I could also invoke a git command and poke at the output, that involves too much shell cruft and line noise.

Additional context

It's possible this is already implemented or could be solved simpler than I envision.

@carusooo carusooo added the enhancement New feature or request label Mar 3, 2023
@andyfeller
Copy link
Contributor

andyfeller commented Jun 28, 2023

I would like to expand upon @carusooo's idea (hello, fellow Andy 👋) by also exposing --dry-run flag as environment variable, too. This is essential in order to build safe processes beyond changing content to know whether to affect change or not.

Looking at the code in repo-operations.go, this should be fairly straight forward to include after setting cmd.Dir:

func executeCommandWithLogger(config *config.GitXargsConfig, repositoryDir string, repo *github.Repository, logger *logrus.Logger) error {
if len(config.Args) < 1 {
return errors.WithStackTrace(types.NoCommandSuppliedErr{})
}
cmdArgs := config.Args
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Dir = repositoryDir
logger.WithFields(logrus.Fields{
"Repo": repo.GetName(),
"Directory": repositoryDir,
"Command": config.Args,
}).Debug("Executing command against local clone of repo...")
stdoutStdErr, err := cmd.CombinedOutput()

Maybe this looks something like:

func executeCommandWithLogger(config *config.GitXargsConfig, repositoryDir string, repo *github.Repository, logger *logrus.Logger) error {
    if len(config.Args) < 1 {
        return errors.WithStackTrace(types.NoCommandSuppliedErr{})
    }

    cmdArgs := config.Args

    cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
    cmd.Dir = repositoryDir
    cmd.Env = os.Environ()
    cmd.Env = append(cmd.Environ(), fmt.Sprintf("XARGS_REPO_NAME=%s", repo.GetName()))
    cmd.Env = append(cmd.Environ(), fmt.Sprintf("XARGS_DRY_RUN=%s", config.DryRun))

    logger.WithFields(logrus.Fields{
        "Repo":      repo.GetName(),
        "Directory": repositoryDir,
        "Command":   config.Args,
    }).Debug("Executing command against local clone of repo...")


    stdoutStdErr, err := cmd.CombinedOutput()
git-xargs --help usage statement

$ git-xargs.exe --help
Usage: git-xargs.exe [--loglevel] [--github-org] [--draft] [--dry-run] [--skip-pull-requests] [--skip-archived-repos]
[--repo] [--repos] [--branch-name] [--base-branch-name] [--commit-message] [--pull-request-title]
[--pull-request-description] [--reviewers] [--team-reviewers] [--seconds-between-prs] [--max-pr-retries]
[--seconds-to-wait-when-rate-limited] [--no-skip-ci] [--keep-cloned-repositories] [--help] command [options] [args]

git-xargs is a command-line tool (CLI) for making updates across multiple Github repositories with a single command.

Commands:

   help, h  Shows a list of commands or help for one command

@zackproser : any particular concerns with this approach ☝️? should there be a prefix / namespace for the env vars to avoid clashing with other concerns?

andyfeller added a commit to andyfeller/git-xargs that referenced this issue Jun 28, 2023
Closes gruntwork-io#115

These changes add support for git-xargs registered environment variables
based on arguments and flags intended for use by the commands and
scripts called by git-xargs.  These environment variables start with
`XARGS_` to avoid collision with other use cases and include:

- `XARGS_REPO_NAME`: the name of the repository targeted
- `XARGS_DRY_RUN`: whether `--dry-run` flag was provided

Additionally, these changes contain a basic test to ensure this feature
works as expected going forward.
@andyfeller
Copy link
Contributor

@carusooo : check out #127 :fishsticks:

zackproser pushed a commit that referenced this issue Jun 30, 2023
…rguments and flags (#127)

* Register env vars based on arguments and flags

Closes #115

These changes add support for git-xargs registered environment variables
based on arguments and flags intended for use by the commands and
scripts called by git-xargs.  These environment variables start with
`XARGS_` to avoid collision with other use cases and include:

- `XARGS_REPO_NAME`: the name of the repository targeted
- `XARGS_DRY_RUN`: whether `--dry-run` flag was provided

Additionally, these changes contain a basic test to ensure this feature
works as expected going forward.

* README documentation, repository owner env var

While updating documentation, I realized that the original intent of
issue 115 might require the repository owner information as well as the
repository name.  This expands the initial changes and performs some
minor refactoring.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants