Skip to content

Commit 6847fd2

Browse files
authored
Add support for git-xargs registered environment variables based on arguments 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.
1 parent c630589 commit 6847fd2

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ git-xargs --github-org my-github-org \
237237
"$(pwd)/scripts/my-ruby-script.rb"
238238
```
239239
240+
## Using `git-xargs` environment variables in commands or scripts
241+
242+
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:
243+
244+
| Env var | Value
245+
| ------------------ | ---------------------------
246+
| `XARGS_DRY_RUN` | Whether the `--dry-run` flag was provided to `git-xargs`; options are `true`, `false`
247+
| `XARGS_REPO_NAME` | Name of the target repository being processed
248+
| `XARGS_REPO_OWNER` | Owner of the target repository being processed
249+
240250
## Debugging runtime errors
241251
242252
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.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
# This script writes some text to stdout and stderr and then exits.
3+
# This is used to test that git-xargs registers environment variables based on flags and arguments.
4+
5+
echo "XARGS_DRY_RUN=$XARGS_DRY_RUN"
6+
echo "XARGS_REPO_NAME=$XARGS_REPO_NAME"
7+
echo "XARGS_REPO_OWNER=$XARGS_REPO_OWNER"

repository/repo-operations.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func executeCommandWithLogger(config *config.GitXargsConfig, repositoryDir strin
110110

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

114118
logger.WithFields(logrus.Fields{
115119
"Repo": repo.GetName(),

repository/repo-operations_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package repository
22

33
import (
44
"bytes"
5+
"fmt"
56
"testing"
67

78
"github.com/gruntwork-io/git-xargs/config"
@@ -50,3 +51,44 @@ func TestExecuteCommandWithLogger(t *testing.T) {
5051
assert.Contains(t, buffer.String(), "Hello, from STDOUT")
5152
assert.Contains(t, buffer.String(), "Hello, from STDERR")
5253
}
54+
55+
// Test that we can execute a script and that the environment variables are set correctly.
56+
func TestExecuteCommandWithLoggerWithEnvVars(t *testing.T) {
57+
t.Parallel()
58+
59+
cfg := config.NewGitXargsConfig()
60+
cfg.Args = []string{"../data/test/_testscripts/test-env-vars.sh"}
61+
repo := getMockGithubRepo()
62+
63+
var buffer bytes.Buffer
64+
65+
// Test whether the lack of --dry-run sets environment variable correctly
66+
cfg.DryRun = false
67+
68+
logger := &logrus.Logger{
69+
Out: &buffer,
70+
Level: logrus.TraceLevel,
71+
Formatter: new(logrus.TextFormatter),
72+
}
73+
74+
err := executeCommandWithLogger(cfg, ".", repo, logger)
75+
assert.NoError(t, err)
76+
assert.Contains(t, buffer.String(), "XARGS_DRY_RUN=false")
77+
assert.Contains(t, buffer.String(), fmt.Sprintf("XARGS_REPO_NAME=%s", *repo.Name))
78+
assert.Contains(t, buffer.String(), fmt.Sprintf("XARGS_REPO_OWNER=%s", *repo.Owner.Login))
79+
80+
// Test whether --dry-run sets environment variable correctly
81+
cfg.DryRun = true
82+
83+
logger = &logrus.Logger{
84+
Out: &buffer,
85+
Level: logrus.TraceLevel,
86+
Formatter: new(logrus.TextFormatter),
87+
}
88+
89+
err = executeCommandWithLogger(cfg, ".", repo, logger)
90+
assert.NoError(t, err)
91+
assert.Contains(t, buffer.String(), "XARGS_DRY_RUN=true")
92+
assert.Contains(t, buffer.String(), fmt.Sprintf("XARGS_REPO_NAME=%s", *repo.Name))
93+
assert.Contains(t, buffer.String(), fmt.Sprintf("XARGS_REPO_OWNER=%s", *repo.Owner.Login))
94+
}

0 commit comments

Comments
 (0)