MaskSecretsWithExtra is like MaskSecrets but also resolves extra variables (e.g., loop vars).
(cmdTemplate string, vars *ast.Vars, extra map[string]any)
| 13 | |
| 14 | // MaskSecretsWithExtra is like MaskSecrets but also resolves extra variables (e.g., loop vars). |
| 15 | func MaskSecretsWithExtra(cmdTemplate string, vars *ast.Vars, extra map[string]any) string { |
| 16 | if vars == nil { |
| 17 | vars = ast.NewVars() |
| 18 | } |
| 19 | |
| 20 | // Fast path: if there are no secrets to mask, resolve the template directly |
| 21 | // without the extra DeepCopy + masking pass. |
| 22 | if !hasSecrets(vars) { |
| 23 | cache := &Cache{Vars: vars} |
| 24 | result := ReplaceWithExtra(cmdTemplate, cache, extra) |
| 25 | if cache.Err() != nil { |
| 26 | return cmdTemplate |
| 27 | } |
| 28 | return result |
| 29 | } |
| 30 | |
| 31 | // Create a copy with secret values masked, leaving the originals untouched. |
| 32 | maskedVars := vars.DeepCopy() |
| 33 | for name, v := range maskedVars.All() { |
| 34 | if v.Secret { |
| 35 | maskedVars.Set(name, ast.Var{ |
| 36 | Value: "*****", |
| 37 | Secret: true, |
| 38 | }) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | cache := &Cache{Vars: maskedVars} |
| 43 | result := ReplaceWithExtra(cmdTemplate, cache, extra) |
| 44 | |
| 45 | // If there was an error, return the original template |
| 46 | if cache.Err() != nil { |
| 47 | return cmdTemplate |
| 48 | } |
| 49 | |
| 50 | return result |
| 51 | } |
| 52 | |
| 53 | // hasSecrets reports whether any variable is marked as secret. |
| 54 | func hasSecrets(vars *ast.Vars) bool { |
no test coverage detected
searching dependent graphs…