| 1 | |
| 2 | |
| 3 | |
| 4 | package limatmpl |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "net/url" |
| 11 | "path" |
| 12 | "path/filepath" |
| 13 | "runtime" |
| 14 | "strings" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/localpathutil" |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | |
| 21 | func (tmpl *Template) UseAbsLocators(ctx context.Context) error { |
| 22 | err := tmpl.useAbsLocators(ctx) |
| 23 | return tmpl.ClearOnError(err) |
| 24 | } |
| 25 | |
| 26 | func (tmpl *Template) useAbsLocators(ctx context.Context) error { |
| 27 | if err := tmpl.Unmarshal(); err != nil { |
| 28 | return err |
| 29 | } |
| 30 | basePath, err := basePath(tmpl.Locator) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | for i, baseLocator := range tmpl.Config.Base { |
| 35 | absLocator, err := absPath(baseLocator.URL, basePath) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | if i == 0 { |
| 40 | |
| 41 | fmt.Fprintf(&tmpl.expr, "| ($a.base | select(type == \"!!str\")) |= %q\n", absLocator) |
| 42 | fmt.Fprintf(&tmpl.expr, "| ($a.base | select(type == \"!!map\") | .url) |= %q\n", absLocator) |
| 43 | fmt.Fprintf(&tmpl.expr, "| ($a.base | select(type == \"!!seq\" and (.[0] | type) == \"!!str\") | .[0]) |= %q\n", absLocator) |
| 44 | fmt.Fprintf(&tmpl.expr, "| ($a.base | select(type == \"!!seq\" and (.[0] | type) == \"!!map\") | .[0].url) |= %q\n", absLocator) |
| 45 | } else { |
| 46 | fmt.Fprintf(&tmpl.expr, "| ($a.base[%d] | select(type == \"!!str\")) |= %q\n", i, absLocator) |
| 47 | fmt.Fprintf(&tmpl.expr, "| ($a.base[%d] | select(type == \"!!map\") | .url) |= %q\n", i, absLocator) |
| 48 | } |
| 49 | } |
| 50 | for i, p := range tmpl.Config.Probes { |
| 51 | if p.File != nil { |
| 52 | absLocator, err := absPath(p.File.URL, basePath) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | fmt.Fprintf(&tmpl.expr, "| ($a.probes[%d].file | select(type == \"!!str\")) = %q\n", i, absLocator) |
| 57 | fmt.Fprintf(&tmpl.expr, "| ($a.probes[%d].file | select(type == \"!!map\") | .url) = %q\n", i, absLocator) |
| 58 | } |
| 59 | } |
| 60 | for i, p := range tmpl.Config.Provision { |
| 61 | if p.File != nil { |
| 62 | absLocator, err := absPath(p.File.URL, basePath) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | fmt.Fprintf(&tmpl.expr, "| ($a.provision[%d].file | select(type == \"!!str\")) = %q\n", i, absLocator) |
| 67 | fmt.Fprintf(&tmpl.expr, "| ($a.provision[%d].file | select(type == \"!!map\") | .url) = %q\n", i, absLocator) |
| 68 | } |
| 69 | } |
| 70 | return tmpl.evalExpr(ctx) |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | func withVolume(path string) (string, error) { |
| 77 | if runtime.GOOS == "windows" && filepath.VolumeName(path) == "" { |
| 78 | root, err := filepath.Abs("/") |
| 79 | if err != nil { |
| 80 | return "", err |
| 81 | } |
| 82 | path = filepath.VolumeName(root) + path |
| 83 | } |
| 84 | return filepath.Clean(path), nil |
| 85 | } |
| 86 | |
| 87 | |
| 88 | func basePath(locator string) (string, error) { |
| 89 | u, err := url.Parse(locator) |
| 90 | |
| 91 | if err == nil && len(u.Scheme) > 1 { |
| 92 | |
| 93 | if u.Opaque != "" { |
| 94 | return u.Scheme + ":" + strings.TrimSuffix(path.Dir(u.Opaque), "."), nil |
| 95 | } |
| 96 | return u.Scheme + "://" + strings.TrimSuffix(path.Dir(path.Join(u.Host, u.Path)), "."), nil |
| 97 | } |
| 98 | base, err := filepath.Abs(filepath.Dir(locator)) |
| 99 | if err != nil { |
| 100 | return "", err |
| 101 | } |
| 102 | return withVolume(base) |
| 103 | } |
| 104 | |
| 105 | |
| 106 | func absPath(locator, basePath string) (string, error) { |
| 107 | if locator == "" { |
| 108 | return "", errors.New("locator is empty") |
| 109 | } |
| 110 | u, err := url.Parse(locator) |
| 111 | if err == nil && len(u.Scheme) > 1 { |
| 112 | return locator, nil |
| 113 | } |
| 114 | |
| 115 | if localpathutil.IsTildePath(locator) { |
| 116 | locator, err = localpathutil.Expand(locator) |
| 117 | if err != nil { |
| 118 | return "", err |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | volumeLen := len(filepath.VolumeName(locator)) |
| 123 | if locator[volumeLen] != '/' && locator[volumeLen] != filepath.Separator { |
| 124 | switch { |
| 125 | case basePath == "": |
| 126 | return "", errors.New("basePath is empty") |
| 127 | case basePath == "-": |
| 128 | return "", errors.New("can't use relative paths when reading template from STDIN") |
| 129 | case strings.Contains(locator, "../") || strings.Contains(locator, `..\`): |
| 130 | |
| 131 | |
| 132 | return "", fmt.Errorf("relative locator path %#q must not contain '..' path segments", locator) |
| 133 | case volumeLen != 0: |
| 134 | return "", fmt.Errorf("relative locator path %#q must not include a volume name", locator) |
| 135 | } |
| 136 | u, err = url.Parse(basePath) |
| 137 | if err != nil { |
| 138 | return "", err |
| 139 | } |
| 140 | if len(u.Scheme) > 1 { |
| 141 | |
| 142 | if u.Opaque != "" || (u.Scheme == "template" && u.Host == "") { |
| 143 | return u.Scheme + ":" + path.Join(u.Opaque, locator), nil |
| 144 | } |
| 145 | return u.JoinPath(locator).String(), nil |
| 146 | } |
| 147 | locator = filepath.Join(basePath, locator) |
| 148 | } |
| 149 | return withVolume(locator) |
| 150 | } |