Source file src/cmd/go/internal/modfetch/codehost/shell.go
1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "archive/zip"
13 "bufio"
14 "bytes"
15 "flag"
16 "fmt"
17 "io/ioutil"
18 "log"
19 "os"
20 "strings"
21 "time"
22
23 "cmd/go/internal/modfetch/codehost"
24 )
25
26 func usage() {
27 fmt.Fprintf(os.Stderr, "usage: go run shell.go vcs remote\n")
28 os.Exit(2)
29 }
30
31 func main() {
32 codehost.WorkRoot = "/tmp/vcswork"
33 log.SetFlags(0)
34 log.SetPrefix("shell: ")
35 flag.Usage = usage
36 flag.Parse()
37 if flag.NArg() != 2 {
38 usage()
39 }
40
41 repo, err := codehost.NewRepo(flag.Arg(0), flag.Arg(1))
42 if err != nil {
43 log.Fatal(err)
44 }
45
46 b := bufio.NewReader(os.Stdin)
47 for {
48 fmt.Fprintf(os.Stderr, ">>> ")
49 line, err := b.ReadString('\n')
50 if err != nil {
51 log.Fatal(err)
52 }
53 f := strings.Fields(line)
54 if len(f) == 0 {
55 continue
56 }
57 switch f[0] {
58 default:
59 fmt.Fprintf(os.Stderr, "?unknown command\n")
60 continue
61 case "tags":
62 prefix := ""
63 if len(f) == 2 {
64 prefix = f[1]
65 }
66 if len(f) > 2 {
67 fmt.Fprintf(os.Stderr, "?usage: tags [prefix]\n")
68 continue
69 }
70 tags, err := repo.Tags(prefix)
71 if err != nil {
72 fmt.Fprintf(os.Stderr, "?%s\n", err)
73 continue
74 }
75 for _, tag := range tags {
76 fmt.Printf("%s\n", tag)
77 }
78
79 case "stat":
80 if len(f) != 2 {
81 fmt.Fprintf(os.Stderr, "?usage: stat rev\n")
82 continue
83 }
84 info, err := repo.Stat(f[1])
85 if err != nil {
86 fmt.Fprintf(os.Stderr, "?%s\n", err)
87 continue
88 }
89 fmt.Printf("name=%s short=%s version=%s time=%s\n", info.Name, info.Short, info.Version, info.Time.UTC().Format(time.RFC3339))
90
91 case "read":
92 if len(f) != 3 {
93 fmt.Fprintf(os.Stderr, "?usage: read rev file\n")
94 continue
95 }
96 data, err := repo.ReadFile(f[1], f[2], 10<<20)
97 if err != nil {
98 fmt.Fprintf(os.Stderr, "?%s\n", err)
99 continue
100 }
101 os.Stdout.Write(data)
102
103 case "zip":
104 if len(f) != 4 {
105 fmt.Fprintf(os.Stderr, "?usage: zip rev subdir output\n")
106 continue
107 }
108 subdir := f[2]
109 if subdir == "-" {
110 subdir = ""
111 }
112 rc, _, err := repo.ReadZip(f[1], subdir, 10<<20)
113 if err != nil {
114 fmt.Fprintf(os.Stderr, "?%s\n", err)
115 continue
116 }
117 data, err := ioutil.ReadAll(rc)
118 rc.Close()
119 if err != nil {
120 fmt.Fprintf(os.Stderr, "?%s\n", err)
121 continue
122 }
123
124 if f[3] != "-" {
125 if err := ioutil.WriteFile(f[3], data, 0666); err != nil {
126 fmt.Fprintf(os.Stderr, "?%s\n", err)
127 continue
128 }
129 }
130 z, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
131 if err != nil {
132 fmt.Fprintf(os.Stderr, "?%s\n", err)
133 continue
134 }
135 for _, f := range z.File {
136 fmt.Printf("%s %d\n", f.Name, f.UncompressedSize64)
137 }
138 }
139 }
140 }
141
View as plain text