gruntwork-io / git-xargs

git-xargs is a command-line tool (CLI) for making updates across multiple Github repositories with a single command.
https://blog.gruntwork.io/introducing-git-xargs-an-open-source-tool-to-update-multiple-github-repos-753f9f3675ec
Apache License 2.0
921 stars 63 forks source link

Set current repo as Env var #115

Closed carusooo closed 1 year ago

carusooo commented 1 year ago

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.

andyfeller commented 1 year ago

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:

https://github.com/gruntwork-io/git-xargs/blob/c630589aa2ccd9245c7934934be818b403e7c0d5/repository/repo-operations.go#L104-L120

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

```shell $ 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 commented 1 year ago

@carusooo : check out https://github.com/gruntwork-io/git-xargs/pull/127 :fishsticks: