MCPcopy
hub / github.com/go-task/task / HandleDynamicVar

Method HandleDynamicVar

compiler.go:149–196  ·  view source on GitHub ↗
(v ast.Var, dir string, e []string)

Source from the content-addressed store, hash-verified

147}
148
149func (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
199func (c *Compiler) ResetCache() {

Callers 2

getVariablesMethod · 0.95
compiledTaskMethod · 0.80

Calls 3

RunCommandFunction · 0.92
VerboseErrfMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected