Debian Codegraph @

debian/go/src/golang.org/x/telemetry/internal/proxy/proxy.go

raw symbols SCIP
1 // Copyright 2023 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 proxy provides functions for writing module data to a directory
6 // in proxy format, so that it can be used as a module proxy by setting
7 // GOPROXY="file://<dir>".
8 // This is copied from golang.org/x/tools/gopls/internal/{proxydir,proxy}.
9 package proxy
10
11 import (
12 "archive/zip"
13 "fmt"
14 "io"
15 "os"
16 "path/filepath"
17 "strings"
18
19 "golang.org/x/mod/module"
20 )
21
22 // WriteProxy creates a new proxy file tree using the provided content,
23 // and returns its URL.
24 func WriteProxy(tmpdir string, files map[string][]byte) (string, error) {
25 type moduleVersion struct {
26 modulePath, version string
27 }
28 // Transform into the format expected by the proxydir package.
29 filesByModule := make(map[moduleVersion]map[string][]byte)
30 for name, data := range files {
31 modulePath, version, suffix := splitModuleVersionPath(name)
32 mv := moduleVersion{modulePath, version}
33 if _, ok := filesByModule[mv]; !ok {
34 filesByModule[mv] = make(map[string][]byte)
35 }
36 filesByModule[mv][suffix] = data
37 }
38 for mv, files := range filesByModule {
39 if err := writeModuleVersion(tmpdir, mv.modulePath, mv.version, files); err != nil {
40 return "", fmt.Errorf("error writing %s@%s: %v", mv.modulePath, mv.version, err)
41 }
42 }
43 return toURL(tmpdir), nil
44 }
45
46 // splitModuleVersionPath extracts module information from files stored in the
47 // directory structure modulePath@version/suffix.
48 // For example:
49 //
50 // splitModuleVersionPath("[email protected]/package") = ("mod.com", "v1.2.3", "package")
51 func splitModuleVersionPath(path string) (modulePath, version, suffix string) {
52 parts := strings.Split(path, "/")
53 var modulePathParts []string
54 for i, p := range parts {
55 if strings.Contains(p, "@") {
56 mv := strings.SplitN(p, "@", 2)
57 modulePathParts = append(modulePathParts, mv[0])
58 return strings.Join(modulePathParts, "/"), mv[1], strings.Join(parts[i+1:], "/")
59 }
60 modulePathParts = append(modulePathParts, p)
61 }
62 // Default behavior: this is just a module path.
63 return path, "", ""
64 }
65
66 // writeModuleVersion creates a directory in the proxy dir for a module.
67 func writeModuleVersion(rootDir, mod, ver string, files map[string][]byte) (rerr error) {
68 dir := filepath.Join(rootDir, mod, "@v")
69 if err := os.MkdirAll(dir, 0755); err != nil {
70 return err
71 }
72
73 // The go command checks for versions by looking at the "list" file. Since
74 // we are supporting multiple versions, create this file if it does not exist
75 // or append the version number to the preexisting file.
76
77 f, err := os.OpenFile(filepath.Join(dir, "list"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
78 if err != nil {
79 return err
80 }
81 defer checkClose("list file", f, &rerr)
82 if _, err := f.WriteString(ver + "\n"); err != nil {
83 return err
84 }
85
86 // Serve the go.mod file on the <version>.mod url, if it exists. Otherwise,
87 // serve a stub.
88 modContents, ok := files["go.mod"]
89 if !ok {
90 modContents = []byte("module " + mod)
91 }
92 if err := os.WriteFile(filepath.Join(dir, ver+".mod"), modContents, 0644); err != nil {
93 return err
94 }
95
96 // info file, just the bare bones.
97 infoContents := []byte(fmt.Sprintf(`{"Version": "%v", "Time":"2017-12-14T13:08:43Z"}`, ver))
98 if err := os.WriteFile(filepath.Join(dir, ver+".info"), infoContents, 0644); err != nil {
99 return err
100 }
101
102 // zip of all the source files.
103 f, err = os.OpenFile(filepath.Join(dir, ver+".zip"), os.O_CREATE|os.O_WRONLY, 0644)
104 if err != nil {
105 return err
106 }
107 defer checkClose("zip file", f, &rerr)
108 z := zip.NewWriter(f)
109 defer checkClose("zip writer", z, &rerr)
110 for name, contents := range files {
111 zf, err := z.Create(mod + "@" + ver + "/" + name)
112 if err != nil {
113 return err
114 }
115 if _, err := zf.Write(contents); err != nil {
116 return err
117 }
118 }
119
120 // Populate the /module/path/@latest that is used by @latest query.
121 if module.IsPseudoVersion(ver) {
122 latestFile := filepath.Join(rootDir, mod, "@latest")
123 if err := os.WriteFile(latestFile, infoContents, 0644); err != nil {
124 return err
125 }
126 }
127 return nil
128 }
129
130 func checkClose(name string, closer io.Closer, err *error) {
131 if cerr := closer.Close(); cerr != nil && *err == nil {
132 *err = fmt.Errorf("closing %s: %v", name, cerr)
133 }
134 }
135
136 // toURL returns the file uri for a proxy directory.
137 func toURL(dir string) string {
138 // file URLs on Windows must start with file:///. See golang.org/issue/6027.
139 path := filepath.ToSlash(dir)
140 if !strings.HasPrefix(path, "/") {
141 path = "/" + path
142 }
143 return "file://" + path
144 }