...

Source file src/github.com/bazelbuild/bazel-gazelle/cmd/fetch_repo/fetch_repo.go

Documentation: github.com/bazelbuild/bazel-gazelle/cmd/fetch_repo

     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  	dest       = flag.String("dest", "", "destination directory")
    38  
    39  	// Repository flags
    40  	remote = flag.String("remote", "", "The URI of the remote repository. Must be used with the --vcs flag.")
    41  	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.")
    42  	rev    = flag.String("rev", "", "target revision")
    43  
    44  	// Module flags
    45  	version = flag.String("version", "", "module version. Must be semantic version or pseudo-version.")
    46  	sum     = flag.String("sum", "", "hash of module contents")
    47  )
    48  
    49  // Override in tests to disable network calls.
    50  var repoRootForImportPath = vcs.RepoRootForImportPath
    51  
    52  func main() {
    53  	log.SetFlags(0)
    54  	log.SetPrefix("fetch_repo: ")
    55  
    56  	flag.Parse()
    57  	if *importpath == "" {
    58  		log.Fatal("-importpath must be set")
    59  	}
    60  	if *dest == "" {
    61  		log.Fatal("-dest must be set")
    62  	}
    63  	if flag.NArg() != 0 {
    64  		log.Fatal("fetch_repo does not accept positional arguments")
    65  	}
    66  
    67  	if *version != "" {
    68  		if *remote != "" {
    69  			log.Fatal("-remote must not be set in module mode")
    70  		}
    71  		if *cmd != "" {
    72  			log.Fatal("-vcs must not be set in module mode")
    73  		}
    74  		if *rev != "" {
    75  			log.Fatal("-rev must not be set in module mode")
    76  		}
    77  		if *version == "" {
    78  			log.Fatal("-version must be set in module mode")
    79  		}
    80  		if *sum == "" {
    81  			log.Fatal("-sum must be set in module mode")
    82  		}
    83  		if err := fetchModule(*dest, *importpath, *version, *sum); err != nil {
    84  			log.Fatal(err)
    85  		}
    86  	} else {
    87  		if *version != "" {
    88  			log.Fatal("-version must not be set in repository mode")
    89  		}
    90  		if *sum != "" {
    91  			log.Fatal("-sum must not be set in repository mode")
    92  		}
    93  		if *rev == "" {
    94  			log.Fatal("-rev must be set in repository mode")
    95  		}
    96  		if err := fetchRepo(*dest, *remote, *cmd, *importpath, *rev); err != nil {
    97  			log.Fatal(err)
    98  		}
    99  	}
   100  }
   101  

View as plain text