aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modfetch/codehost/shell.go
blob: b9525adf5e6a6b879a454a5b6ac21227716d9655 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2018 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.

// +build ignore

// Interactive debugging shell for codehost.Repo implementations.

package main

import (
	"archive/zip"
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"strings"
	"time"

	"cmd/go/internal/cfg"
	"cmd/go/internal/modfetch/codehost"
)

func usage() {
	fmt.Fprintf(os.Stderr, "usage: go run shell.go vcs remote\n")
	os.Exit(2)
}

func main() {
	cfg.GOMODCACHE = "/tmp/vcswork"
	log.SetFlags(0)
	log.SetPrefix("shell: ")
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() != 2 {
		usage()
	}

	repo, err := codehost.NewRepo(flag.Arg(0), flag.Arg(1))
	if err != nil {
		log.Fatal(err)
	}

	b := bufio.NewReader(os.Stdin)
	for {
		fmt.Fprintf(os.Stderr, ">>> ")
		line, err := b.ReadString('\n')
		if err != nil {
			log.Fatal(err)
		}
		f := strings.Fields(line)
		if len(f) == 0 {
			continue
		}
		switch f[0] {
		default:
			fmt.Fprintf(os.Stderr, "?unknown command\n")
			continue
		case "tags":
			prefix := ""
			if len(f) == 2 {
				prefix = f[1]
			}
			if len(f) > 2 {
				fmt.Fprintf(os.Stderr, "?usage: tags [prefix]\n")
				continue
			}
			tags, err := repo.Tags(prefix)
			if err != nil {
				fmt.Fprintf(os.Stderr, "?%s\n", err)
				continue
			}
			for _, tag := range tags {
				fmt.Printf("%s\n", tag)
			}

		case "stat":
			if len(f) != 2 {
				fmt.Fprintf(os.Stderr, "?usage: stat rev\n")
				continue
			}
			info, err := repo.Stat(f[1])
			if err != nil {
				fmt.Fprintf(os.Stderr, "?%s\n", err)
				continue
			}
			fmt.Printf("name=%s short=%s version=%s time=%s\n", info.Name, info.Short, info.Version, info.Time.UTC().Format(time.RFC3339))

		case "read":
			if len(f) != 3 {
				fmt.Fprintf(os.Stderr, "?usage: read rev file\n")
				continue
			}
			data, err := repo.ReadFile(f[1], f[2], 10<<20)
			if err != nil {
				fmt.Fprintf(os.Stderr, "?%s\n", err)
				continue
			}
			os.Stdout.Write(data)

		case "zip":
			if len(f) != 4 {
				fmt.Fprintf(os.Stderr, "?usage: zip rev subdir output\n")
				continue
			}
			subdir := f[2]
			if subdir == "-" {
				subdir = ""
			}
			rc, err := repo.ReadZip(f[1], subdir, 10<<20)
			if err != nil {
				fmt.Fprintf(os.Stderr, "?%s\n", err)
				continue
			}
			data, err := io.ReadAll(rc)
			rc.Close()
			if err != nil {
				fmt.Fprintf(os.Stderr, "?%s\n", err)
				continue
			}

			if f[3] != "-" {
				if err := ioutil.WriteFile(f[3], data, 0666); err != nil {
					fmt.Fprintf(os.Stderr, "?%s\n", err)
					continue
				}
			}
			z, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
			if err != nil {
				fmt.Fprintf(os.Stderr, "?%s\n", err)
				continue
			}
			for _, f := range z.File {
				fmt.Printf("%s %d\n", f.Name, f.UncompressedSize64)
			}
		}
	}
}