golang.org/x/tools/gopls@v0.15.3/integration/govim/artifacts.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 main
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"io"
    11  	"net/http"
    12  	"os"
    13  	"path"
    14  )
    15  
    16  var bucket = flag.String("bucket", "golang-gopls_integration_tests", "GCS bucket holding test artifacts.")
    17  
    18  const usage = `
    19  artifacts [--bucket=<bucket ID>] <cloud build evaluation ID>
    20  
    21  Fetch artifacts from an integration test run. Evaluation ID should be extracted
    22  from the cloud build notification.
    23  
    24  In order for this to work, the GCS bucket that artifacts were written to must
    25  be publicly readable. By default, this fetches from the
    26  golang-gopls_integration_tests bucket.
    27  `
    28  
    29  func main() {
    30  	flag.Usage = func() {
    31  		fmt.Fprint(flag.CommandLine.Output(), usage)
    32  	}
    33  	flag.Parse()
    34  	if flag.NArg() != 1 {
    35  		flag.Usage()
    36  		os.Exit(2)
    37  	}
    38  	evalID := flag.Arg(0)
    39  	logURL := fmt.Sprintf("https://storage.googleapis.com/%s/log-%s.txt", *bucket, evalID)
    40  	if err := download(logURL); err != nil {
    41  		fmt.Fprintf(os.Stderr, "downloading logs: %v", err)
    42  	}
    43  	tarURL := fmt.Sprintf("https://storage.googleapis.com/%s/govim/%s/artifacts.tar.gz", *bucket, evalID)
    44  	if err := download(tarURL); err != nil {
    45  		fmt.Fprintf(os.Stderr, "downloading artifact tarball: %v", err)
    46  	}
    47  }
    48  
    49  func download(artifactURL string) error {
    50  	name := path.Base(artifactURL)
    51  	resp, err := http.Get(artifactURL)
    52  	if err != nil {
    53  		return fmt.Errorf("fetching from GCS: %v", err)
    54  	}
    55  	defer resp.Body.Close()
    56  	if resp.StatusCode != http.StatusOK {
    57  		return fmt.Errorf("got status code %d from GCS", resp.StatusCode)
    58  	}
    59  	data, err := io.ReadAll(resp.Body)
    60  	if err != nil {
    61  		return fmt.Errorf("reading result: %v", err)
    62  	}
    63  	if err := os.WriteFile(name, data, 0644); err != nil {
    64  		return fmt.Errorf("writing artifact: %v", err)
    65  	}
    66  	return nil
    67  }