(v ast.Var, dir string, e []string)
| 147 | } |
| 148 | |
| 149 | func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string) (string, error) { |
| 150 | c.muDynamicCache.Lock() |
| 151 | defer c.muDynamicCache.Unlock() |
| 152 | |
| 153 | // If the variable is not dynamic or it is empty, return an empty string |
| 154 | if v.Sh == nil || *v.Sh == "" { |
| 155 | return "", nil |
| 156 | } |
| 157 | |
| 158 | if c.dynamicCache == nil { |
| 159 | c.dynamicCache = make(map[string]string, 30) |
| 160 | } |
| 161 | if result, ok := c.dynamicCache[*v.Sh]; ok { |
| 162 | return result, nil |
| 163 | } |
| 164 | |
| 165 | // NOTE(@andreynering): If a var have a specific dir, use this instead |
| 166 | if v.Dir != "" { |
| 167 | dir = v.Dir |
| 168 | } |
| 169 | |
| 170 | var stdout bytes.Buffer |
| 171 | opts := &execext.RunCommandOptions{ |
| 172 | Command: *v.Sh, |
| 173 | Dir: dir, |
| 174 | Stdout: &stdout, |
| 175 | Stderr: c.Logger.Stderr, |
| 176 | Env: e, |
| 177 | } |
| 178 | if err := execext.RunCommand(context.Background(), opts); err != nil { |
| 179 | return "", fmt.Errorf(`task: Command "%s" failed: %s`, opts.Command, err) |
| 180 | } |
| 181 | |
| 182 | // Trim a single trailing newline from the result to make most command |
| 183 | // output easier to use in shell commands. |
| 184 | result := strings.TrimSuffix(stdout.String(), "\r\n") |
| 185 | result = strings.TrimSuffix(result, "\n") |
| 186 | |
| 187 | c.dynamicCache[*v.Sh] = result |
| 188 | // Never print the resolved value of a secret variable, even in verbose mode |
| 189 | logResult := result |
| 190 | if v.Secret { |
| 191 | logResult = "*****" |
| 192 | } |
| 193 | c.Logger.VerboseErrf(logger.Magenta, "task: dynamic variable: %q result: %q\n", *v.Sh, logResult) |
| 194 | |
| 195 | return result, nil |
| 196 | } |
| 197 | |
| 198 | // ResetCache clear the dynamic variables cache |
| 199 | func (c *Compiler) ResetCache() { |
no test coverage detected