github.com/wolfd/bazel-gazelle@v0.14.0/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 is similar to "go get -d" but it works even if the given
    17  // repository path is not a buildable Go package and it checks out a specific
    18  // revision rather than the latest revision.
    19  //
    20  // The difference between fetch_repo and "git clone" or {new_,}git_repository is
    21  // that fetch_repo recognizes import redirection of Go and it supports other
    22  // version control systems than git.
    23  //
    24  // These differences help us to manage external Go repositories in the manner of
    25  // Bazel.
    26  package main
    27  
    28  import (
    29  	"flag"
    30  	"fmt"
    31  	"log"
    32  
    33  	"golang.org/x/tools/go/vcs"
    34  )
    35  
    36  var (
    37  	remote     = flag.String("remote", "", "The URI of the remote repository. Must be used with the --vcs flag.")
    38  	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.")
    39  	rev        = flag.String("rev", "", "target revision")
    40  	dest       = flag.String("dest", "", "destination directory")
    41  	importpath = flag.String("importpath", "", "Go importpath to the repository fetch")
    42  
    43  	// Used for overriding in tests to disable network calls.
    44  	repoRootForImportPath = vcs.RepoRootForImportPath
    45  )
    46  
    47  func getRepoRoot(remote, cmd, importpath string) (*vcs.RepoRoot, error) {
    48  	if (cmd == "") != (remote == "") {
    49  		return nil, fmt.Errorf("--remote should be used with the --vcs flag. If this is an import path, use --importpath instead.")
    50  	}
    51  
    52  	if cmd != "" && remote != "" {
    53  		v := vcs.ByCmd(cmd)
    54  		if v == nil {
    55  			return nil, fmt.Errorf("invalid VCS type: %s", cmd)
    56  		}
    57  		return &vcs.RepoRoot{
    58  			VCS:  v,
    59  			Repo: remote,
    60  			Root: importpath,
    61  		}, nil
    62  	}
    63  
    64  	// User did not give us complete information for VCS / Remote.
    65  	// Try to figure out the information from the import path.
    66  	r, err := repoRootForImportPath(importpath, true)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	if importpath != r.Root {
    71  		return nil, fmt.Errorf("not a root of a repository: %s", importpath)
    72  	}
    73  	return r, nil
    74  }
    75  
    76  func run() error {
    77  	r, err := getRepoRoot(*remote, *cmd, *importpath)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	return r.VCS.CreateAtRev(*dest, r.Repo, *rev)
    82  }
    83  
    84  func main() {
    85  	flag.Parse()
    86  
    87  	if err := run(); err != nil {
    88  		log.Fatal(err)
    89  	}
    90  }