-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
59 lines (53 loc) · 1.29 KB
/
utils.go
File metadata and controls
59 lines (53 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// pluralize returns the singular or plural form of a word based on count
func pluralize(count int, singular, plural string) string {
if count == 1 {
return singular
}
return plural
}
func toStdErr(dryRun bool, format string, args ...any) {
if dryRun {
format = "[dry-run] " + format
}
fmt.Fprintf(os.Stderr, format, args...)
}
// dryRunString returns a shell-executable representation of the command,
// including only the env vars that plur sets (not the full inherited env).
func dryRunString(cmd *exec.Cmd) string {
var envs []string
if cmd.Env != nil {
envs = cmd.Environ()
}
var extras []string
for _, env := range envs {
if strings.HasPrefix(env, EnvTestEnvNumber+"=") ||
strings.HasPrefix(env, EnvParallelTestGroups+"=") ||
strings.HasPrefix(env, "RAILS_ENV=") {
extras = append(extras, env)
}
}
cmdStr := strings.Join(cmd.Args, " ")
if len(extras) > 0 {
return strings.Join(extras, " ") + " " + cmdStr
}
return cmdStr
}
func printDryRunCommand(dryRun bool, cmd *exec.Cmd) {
if !dryRun {
return
}
toStdErr(true, "%s\n", dryRunString(cmd))
}
func printDryRunWorker(dryRun bool, workerIndex int, cmd *exec.Cmd) {
if !dryRun {
return
}
toStdErr(true, "Worker %d: %s\n", workerIndex, dryRunString(cmd))
}