github.com/jd-ly/tools@v0.5.7/internal/lsp/fake/proxy.go (about)

     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.
     4  
     5  package fake
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/jd-ly/tools/internal/proxydir"
    11  )
    12  
    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
    19  	}
    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)
    27  		}
    28  		filesByModule[mv][suffix] = data
    29  	}
    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)
    33  		}
    34  	}
    35  	return proxydir.ToURL(tmpdir), nil
    36  }