File: proxydir.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (99 lines) | stat: -rw-r--r-- 3,046 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package proxydir provides functions for writing module data to a directory
// in proxy format, so that it can be used as a module proxy by setting
// GOPROXY="file://<dir>".
package proxydir

import (
	"archive/zip"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"

	"golang.org/x/tools/internal/testenv"
)

// WriteModuleVersion creates a directory in the proxy dir for a module.
func WriteModuleVersion(rootDir, module, ver string, files map[string][]byte) (rerr error) {
	dir := filepath.Join(rootDir, module, "@v")
	if err := os.MkdirAll(dir, 0755); err != nil {
		return err
	}

	// The go command checks for versions by looking at the "list" file.  Since
	// we are supporting multiple versions, create this file if it does not exist
	// or append the version number to the preexisting file.
	f, err := os.OpenFile(filepath.Join(dir, "list"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		return err
	}
	defer checkClose("list file", f, &rerr)
	if _, err := f.WriteString(ver + "\n"); err != nil {
		return err
	}

	// Serve the go.mod file on the <version>.mod url, if it exists. Otherwise,
	// serve a stub.
	modContents, ok := files["go.mod"]
	if !ok {
		modContents = []byte("module " + module)
	}
	if err := os.WriteFile(filepath.Join(dir, ver+".mod"), modContents, 0644); err != nil {
		return err
	}

	// info file, just the bare bones.
	infoContents := []byte(fmt.Sprintf(`{"Version": "%v", "Time":"2017-12-14T13:08:43Z"}`, ver))
	if err := os.WriteFile(filepath.Join(dir, ver+".info"), infoContents, 0644); err != nil {
		return err
	}

	// zip of all the source files.
	f, err = os.OpenFile(filepath.Join(dir, ver+".zip"), os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		return err
	}
	defer checkClose("zip file", f, &rerr)
	z := zip.NewWriter(f)
	defer checkClose("zip writer", z, &rerr)
	for name, contents := range files {
		zf, err := z.Create(module + "@" + ver + "/" + name)
		if err != nil {
			return err
		}
		if _, err := zf.Write(contents); err != nil {
			return err
		}
	}

	return nil
}

func checkClose(name string, closer io.Closer, err *error) {
	if cerr := closer.Close(); cerr != nil && *err == nil {
		*err = fmt.Errorf("closing %s: %v", name, cerr)
	}
}

// ToURL returns the file uri for a proxy directory.
func ToURL(dir string) string {
	if testenv.Go1Point() >= 13 {
		// file URLs on Windows must start with file:///. See golang.org/issue/6027.
		path := filepath.ToSlash(dir)
		if !strings.HasPrefix(path, "/") {
			path = "/" + path
		}
		return "file://" + path
	} else {
		// Prior to go1.13, the Go command on Windows only accepted GOPROXY file URLs
		// of the form file://C:/path/to/proxy. This was incorrect: when parsed, "C:"
		// is interpreted as the host. See golang.org/issue/6027. This has been
		// fixed in go1.13, but we emit the old format for old releases.
		return "file://" + filepath.ToSlash(dir)
	}
}