golang.org/x/tools@v0.21.1-0.20240520172518-788d39e776b1/internal/proxydir/proxydir.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 proxydir 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 package proxydir 9 10 import ( 11 "archive/zip" 12 "fmt" 13 "io" 14 "os" 15 "path/filepath" 16 "strings" 17 18 "golang.org/x/tools/internal/testenv" 19 ) 20 21 // WriteModuleVersion creates a directory in the proxy dir for a module. 22 func WriteModuleVersion(rootDir, module, ver string, files map[string][]byte) (rerr error) { 23 dir := filepath.Join(rootDir, module, "@v") 24 if err := os.MkdirAll(dir, 0755); err != nil { 25 return err 26 } 27 28 // The go command checks for versions by looking at the "list" file. Since 29 // we are supporting multiple versions, create this file if it does not exist 30 // or append the version number to the preexisting file. 31 f, err := os.OpenFile(filepath.Join(dir, "list"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) 32 if err != nil { 33 return err 34 } 35 defer checkClose("list file", f, &rerr) 36 if _, err := f.WriteString(ver + "\n"); err != nil { 37 return err 38 } 39 40 // Serve the go.mod file on the <version>.mod url, if it exists. Otherwise, 41 // serve a stub. 42 modContents, ok := files["go.mod"] 43 if !ok { 44 modContents = []byte("module " + module) 45 } 46 if err := os.WriteFile(filepath.Join(dir, ver+".mod"), modContents, 0644); err != nil { 47 return err 48 } 49 50 // info file, just the bare bones. 51 infoContents := []byte(fmt.Sprintf(`{"Version": "%v", "Time":"2017-12-14T13:08:43Z"}`, ver)) 52 if err := os.WriteFile(filepath.Join(dir, ver+".info"), infoContents, 0644); err != nil { 53 return err 54 } 55 56 // zip of all the source files. 57 f, err = os.OpenFile(filepath.Join(dir, ver+".zip"), os.O_CREATE|os.O_WRONLY, 0644) 58 if err != nil { 59 return err 60 } 61 defer checkClose("zip file", f, &rerr) 62 z := zip.NewWriter(f) 63 defer checkClose("zip writer", z, &rerr) 64 for name, contents := range files { 65 zf, err := z.Create(module + "@" + ver + "/" + name) 66 if err != nil { 67 return err 68 } 69 if _, err := zf.Write(contents); err != nil { 70 return err 71 } 72 } 73 74 return nil 75 } 76 77 func checkClose(name string, closer io.Closer, err *error) { 78 if cerr := closer.Close(); cerr != nil && *err == nil { 79 *err = fmt.Errorf("closing %s: %v", name, cerr) 80 } 81 } 82 83 // ToURL returns the file uri for a proxy directory. 84 func ToURL(dir string) string { 85 if testenv.Go1Point() >= 13 { 86 // file URLs on Windows must start with file:///. See golang.org/issue/6027. 87 path := filepath.ToSlash(dir) 88 if !strings.HasPrefix(path, "/") { 89 path = "/" + path 90 } 91 return "file://" + path 92 } else { 93 // Prior to go1.13, the Go command on Windows only accepted GOPROXY file URLs 94 // of the form file://C:/path/to/proxy. This was incorrect: when parsed, "C:" 95 // is interpreted as the host. See golang.org/issue/6027. This has been 96 // fixed in go1.13, but we emit the old format for old releases. 97 return "file://" + filepath.ToSlash(dir) 98 } 99 }