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 support for git-xargs registered environment variables based on arguments and flags #127

Merged
merged 3 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ git-xargs --github-org my-github-org \
"$(pwd)/scripts/my-ruby-script.rb"
```

## Using `git-xargs` environment variables in commands or scripts

When executing commands or scripts, `git-xargs` will register the following environment variables for use by commands or scripts based on the arguments and flags provided:

| Env var | Value
| ------------------ | ---------------------------
| `XARGS_DRY_RUN` | Whether the `--dry-run` flag was provided to `git-xargs`; options are `true`, `false`
| `XARGS_REPO_NAME` | Name of the target repository being processed
| `XARGS_REPO_OWNER` | Owner of the target repository being processed

## Debugging runtime errors

By default, `git-xargs` will conceal runtime errors as they occur because its log level setting is `INFO` if not overridden by the `--loglevel` flag.
Expand Down
7 changes: 7 additions & 0 deletions data/test/_testscripts/test-env-vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
# This script writes some text to stdout and stderr and then exits.
# This is used to test that git-xargs registers environment variables based on flags and arguments.

echo "XARGS_DRY_RUN=$XARGS_DRY_RUN"
echo "XARGS_REPO_NAME=$XARGS_REPO_NAME"
echo "XARGS_REPO_OWNER=$XARGS_REPO_OWNER"
4 changes: 4 additions & 0 deletions repository/repo-operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func executeCommandWithLogger(config *config.GitXargsConfig, repositoryDir strin

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

logger.WithFields(logrus.Fields{
"Repo": repo.GetName(),
Expand Down
42 changes: 42 additions & 0 deletions repository/repo-operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"bytes"
"fmt"
"testing"

"github.com/gruntwork-io/git-xargs/config"
Expand Down Expand Up @@ -50,3 +51,44 @@ func TestExecuteCommandWithLogger(t *testing.T) {
assert.Contains(t, buffer.String(), "Hello, from STDOUT")
assert.Contains(t, buffer.String(), "Hello, from STDERR")
}

// Test that we can execute a script and that the environment variables are set correctly.
func TestExecuteCommandWithLoggerWithEnvVars(t *testing.T) {
t.Parallel()

cfg := config.NewGitXargsConfig()
cfg.Args = []string{"../data/test/_testscripts/test-env-vars.sh"}
repo := getMockGithubRepo()

var buffer bytes.Buffer

// Test whether the lack of --dry-run sets environment variable correctly
cfg.DryRun = false

logger := &logrus.Logger{
Out: &buffer,
Level: logrus.TraceLevel,
Formatter: new(logrus.TextFormatter),
}

err := executeCommandWithLogger(cfg, ".", repo, logger)
assert.NoError(t, err)
assert.Contains(t, buffer.String(), "XARGS_DRY_RUN=false")
assert.Contains(t, buffer.String(), fmt.Sprintf("XARGS_REPO_NAME=%s", *repo.Name))
assert.Contains(t, buffer.String(), fmt.Sprintf("XARGS_REPO_OWNER=%s", *repo.Owner.Login))

// Test whether --dry-run sets environment variable correctly
cfg.DryRun = true

logger = &logrus.Logger{
Out: &buffer,
Level: logrus.TraceLevel,
Formatter: new(logrus.TextFormatter),
}

err = executeCommandWithLogger(cfg, ".", repo, logger)
assert.NoError(t, err)
assert.Contains(t, buffer.String(), "XARGS_DRY_RUN=true")
assert.Contains(t, buffer.String(), fmt.Sprintf("XARGS_REPO_NAME=%s", *repo.Name))
assert.Contains(t, buffer.String(), fmt.Sprintf("XARGS_REPO_OWNER=%s", *repo.Owner.Login))
}