Skip to content

Commit 00bb585

Browse files
committed
Add a plugin mechanism to provide custom locator URL schemes
The template reader will attempt to call `limactl-url-$SCHEME` to rewrite a custom URL as either a local filename, or a URL with a supported URL like https. Signed-off-by: Jan Dubois <[email protected]>
1 parent e49e987 commit 00bb585

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pkg/limatmpl/locator.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"net/url"
1212
"os"
13+
"os/exec"
1314
"path"
1415
"path/filepath"
1516
"regexp"
@@ -34,6 +35,11 @@ func Read(ctx context.Context, name, locator string) (*Template, error) {
3435
Locator: locator,
3536
}
3637

38+
locator, err = transformCustomURL(ctx, locator)
39+
if err != nil {
40+
return nil, err
41+
}
42+
3743
if imageTemplate(tmpl, locator) {
3844
return tmpl, nil
3945
}
@@ -285,3 +291,33 @@ func InstNameFromYAMLPath(yamlPath string) (string, error) {
285291
}
286292
return s, nil
287293
}
294+
295+
func transformCustomURL(ctx context.Context, locator string) (string, error) {
296+
u, err := url.Parse(locator)
297+
if err != nil || len(u.Scheme) <= 1 {
298+
return locator, nil
299+
}
300+
301+
// TODO we should call main.updatePathEnv() here (after moving it to some pkg)
302+
externalCmd := "limactl-url-" + u.Scheme
303+
execPath, err := exec.LookPath(externalCmd)
304+
if err != nil {
305+
return locator, nil
306+
}
307+
308+
cmd := exec.CommandContext(ctx, execPath, u.String())
309+
cmd.Env = os.Environ()
310+
311+
stdout, err := cmd.Output()
312+
if err != nil {
313+
if exitErr, ok := err.(*exec.ExitError); ok {
314+
stderrMsg := string(exitErr.Stderr)
315+
if stderrMsg != "" {
316+
return "", fmt.Errorf("command %q failed: %s", cmd.String(), strings.TrimSpace(stderrMsg))
317+
}
318+
}
319+
return "", fmt.Errorf("command %q failed: %w", cmd.String(), err)
320+
}
321+
322+
return strings.TrimSpace(string(stdout)), nil
323+
}

0 commit comments

Comments
 (0)