go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/cmd/gitiles/download_file.go (about)

     1  // Copyright 2019 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/maruel/subcommands"
    22  
    23  	"go.chromium.org/luci/auth"
    24  	"go.chromium.org/luci/common/api/gitiles"
    25  	"go.chromium.org/luci/common/errors"
    26  	gitilespb "go.chromium.org/luci/common/proto/gitiles"
    27  	"go.chromium.org/luci/common/retry"
    28  	"go.chromium.org/luci/common/retry/transient"
    29  	"go.chromium.org/luci/grpc/grpcutil"
    30  )
    31  
    32  func cmdDownloadFile(authOpts auth.Options) *subcommands.Command {
    33  	return &subcommands.Command{
    34  		UsageLine: "download-file <options> repository-url committish path",
    35  		ShortDesc: "prints the contents of the file at path in the repository",
    36  		LongDesc:  "Prints the contents of the file at path in the repository at requested commit.",
    37  		CommandRun: func() subcommands.CommandRun {
    38  			c := downloadFileRun{}
    39  			c.commonFlags.Init(authOpts)
    40  			return &c
    41  		},
    42  	}
    43  }
    44  
    45  type downloadFileRun struct {
    46  	commonFlags
    47  }
    48  
    49  func (c *downloadFileRun) Parse(a subcommands.Application, args []string) error {
    50  	switch err := c.commonFlags.Parse(); {
    51  	case err != nil:
    52  		return err
    53  	case len(args) != 3:
    54  		return errors.New("exactly 3 position arguments are expected")
    55  	default:
    56  		return nil
    57  	}
    58  }
    59  
    60  func (c *downloadFileRun) main(a subcommands.Application, args []string) error {
    61  	ctx := c.defaultFlags.MakeLoggingContext(os.Stderr)
    62  
    63  	repoURL := args[0]
    64  	committish := args[1]
    65  	path := args[2]
    66  
    67  	host, project, err := gitiles.ParseRepoURL(repoURL)
    68  	if err != nil {
    69  		return errors.Annotate(err, "invalid repo URL %q", args[0]).Err()
    70  	}
    71  
    72  	req := &gitilespb.DownloadFileRequest{
    73  		Project:    project,
    74  		Committish: committish,
    75  		Path:       path,
    76  	}
    77  
    78  	authCl, err := c.createAuthClient()
    79  	if err != nil {
    80  		return err
    81  	}
    82  	g, err := gitiles.NewRESTClient(authCl, host, true)
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	var res *gitilespb.DownloadFileResponse
    88  	if err := retry.Retry(ctx, transient.Only(retry.Default), func() error {
    89  		var err error
    90  		res, err = g.DownloadFile(ctx, req)
    91  		return grpcutil.WrapIfTransient(err)
    92  	}, nil); err != nil {
    93  		return err
    94  	}
    95  
    96  	if res.Contents == "" {
    97  		fmt.Fprintf(a.GetErr(), "warning: file %s is empty", path)
    98  	}
    99  	fmt.Fprintf(a.GetOut(), "%s", res.Contents)
   100  	return nil
   101  }
   102  
   103  func (c *downloadFileRun) Run(a subcommands.Application, args []string, _ subcommands.Env) int {
   104  	if err := c.Parse(a, args); err != nil {
   105  		fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
   106  		return 1
   107  	}
   108  	if err := c.main(a, args); err != nil {
   109  		fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
   110  		return 1
   111  	}
   112  	return 0
   113  }