1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 "golang.org/x/tools/internal/proxydir"
13 // WriteProxy creates a new proxy file tree using the txtar-encoded content,
14 // and returns its URL.
15 func WriteProxy(tmpdir, txt string) (string, error) {
16 files := unpackTxt(txt)
17 type moduleVersion struct {
18 modulePath, version string
20 // Transform into the format expected by the proxydir package.
21 filesByModule := make(map[moduleVersion]map[string][]byte)
22 for name, data := range files {
23 modulePath, version, suffix := splitModuleVersionPath(name)
24 mv := moduleVersion{modulePath, version}
25 if _, ok := filesByModule[mv]; !ok {
26 filesByModule[mv] = make(map[string][]byte)
28 filesByModule[mv][suffix] = data
30 for mv, files := range filesByModule {
31 if err := proxydir.WriteModuleVersion(tmpdir, mv.modulePath, mv.version, files); err != nil {
32 return "", fmt.Errorf("error writing %s@%s: %v", mv.modulePath, mv.version, err)
35 return proxydir.ToURL(tmpdir), nil