forked from BinSquare/envmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspawn.go
More file actions
53 lines (46 loc) · 1.2 KB
/
spawn.go
File metadata and controls
53 lines (46 loc) · 1.2 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
package main
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
)
func SpawnWithEnv(ctx context.Context, command string, args []string, secretEnv map[string]string) error {
cmd := exec.CommandContext(ctx, command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
merged := os.Environ()
for k, v := range secretEnv {
merged = append(merged, fmt.Sprintf("%s=%s", k, v))
}
cmd.Env = merged
return cmd.Run()
}
func MaskValue(value string) string {
if value == "" {
return "(empty)"
}
if len(value) <= 4 {
return "****"
}
return value[:2] + "****" + value[len(value)-2:]
}
// shellQuote applies a minimal POSIX-safe single-quote escaping for display.
func shellQuote(s string) string {
if s == "" {
return "''"
}
// If the string contains only safe characters, return as-is.
for _, r := range s {
if !(r == '_' || r == '-' || r == '.' || r == '/' || r == ':' || r == '@' || r == '+' || (r >= '0' && r <= '9') || (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z')) {
// Need quoting
goto needsQuote
}
}
return s
needsQuote:
// Escape single quotes by closing, escaping, and reopening: ' -> '\''.
return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
}