Source file src/cmd/go/internal/modfetch/codehost/shell.go

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

View as plain text