github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/cmd/fetch_repo/fetch_repo.go (about)

     1  /* Copyright 2016 The Bazel Authors. All rights reserved.
     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  
    16  // Command fetch_repo downloads a Go module or repository at a specific
    17  // version or commit.
    18  //
    19  // In module mode, fetch_repo downloads a module using "go mod download",
    20  // verifies the contents against a sum, then copies the contents of the module
    21  // to a target directory. fetch_repo respects GOPATH, GOCACHE, and GOPROXY.
    22  //
    23  // In repository mode, fetch_repo clones a repository using a VCS tool.
    24  // fetch_repo performs import path redirection in this mode.
    25  package main
    26  
    27  import (
    28  	"flag"
    29  	"log"
    30  
    31  	"golang.org/x/tools/go/vcs"
    32  )
    33  
    34  var (
    35  	// Common flags
    36  	importpath = flag.String("importpath", "", "Go importpath to the repository fetch")
    37  	path       = flag.String("path", "", "absolute or relative path to a local go module")
    38  	dest       = flag.String("dest", "", "destination directory")
    39  
    40  	// Repository flags
    41  	remote = flag.String("remote", "", "The URI of the remote repository. Must be used with the --vcs flag.")
    42  	cmd    = flag.String("vcs", "", "Version control system to use to fetch the repository. Should be one of: git,hg,svn,bzr. Must be used with the --remote flag.")
    43  	rev    = flag.String("rev", "", "target revision")
    44  
    45  	// Module flags
    46  	version = flag.String("version", "", "module version. Must be semantic version or pseudo-version.")
    47  	sum     = flag.String("sum", "", "hash of module contents")
    48  )
    49  
    50  // Override in tests to disable network calls.
    51  var repoRootForImportPath = vcs.RepoRootForImportPath
    52  
    53  func main() {
    54  	log.SetFlags(0)
    55  	log.SetPrefix("fetch_repo: ")
    56  
    57  	flag.Parse()
    58  
    59  	if *importpath == "" && *path == "" {
    60  		log.Fatal("-importpath or -path must be set")
    61  	}
    62  
    63  	if *dest == "" {
    64  		log.Fatal("-dest must be set")
    65  	}
    66  	if flag.NArg() != 0 {
    67  		log.Fatal("fetch_repo does not accept positional arguments")
    68  	}
    69  
    70  	if *path != "" {
    71  		if *importpath != "" {
    72  			log.Fatal("-importpath must not be set")
    73  		}
    74  		if *remote != "" {
    75  			log.Fatal("-remote must not be set in module path mode")
    76  		}
    77  		if *cmd != "" {
    78  			log.Fatal("-vcs must not be set in module path mode")
    79  		}
    80  		if *rev != "" {
    81  			log.Fatal("-rev must not be set in module path mode")
    82  		}
    83  		if *version != "" {
    84  			log.Fatal("-version must not be set in module path mode")
    85  		}
    86  		if *sum != "" {
    87  			log.Fatal("-sum must not be set in module path mode")
    88  		}
    89  		if err := moduleFromPath(*path, *dest); err != nil {
    90  			log.Fatal(err)
    91  		}
    92  	} else if *version != "" {
    93  		if *remote != "" {
    94  			log.Fatal("-remote must not be set in module mode")
    95  		}
    96  		if *cmd != "" {
    97  			log.Fatal("-vcs must not be set in module mode")
    98  		}
    99  		if *rev != "" {
   100  			log.Fatal("-rev must not be set in module mode")
   101  		}
   102  		if *version == "" {
   103  			log.Fatal("-version must be set in module mode")
   104  		}
   105  		if *sum == "" {
   106  			log.Fatal("-sum must be set in module mode")
   107  		}
   108  		if err := fetchModule(*dest, *importpath, *version, *sum); err != nil {
   109  			log.Fatal(err)
   110  		}
   111  	} else {
   112  		if *version != "" {
   113  			log.Fatal("-version must not be set in repository mode")
   114  		}
   115  		if *sum != "" {
   116  			log.Fatal("-sum must not be set in repository mode")
   117  		}
   118  		if *rev == "" {
   119  			log.Fatal("-rev must be set in repository mode")
   120  		}
   121  		if err := fetchRepo(*dest, *remote, *cmd, *importpath, *rev); err != nil {
   122  			log.Fatal(err)
   123  		}
   124  	}
   125  }