Last active
October 31, 2018 19:01
-
-
Save blalor/c2be9ba57c4f5b8fc5d2dfbd6f8ec0b6 to your computer and use it in GitHub Desktop.
clones a Go project into a new directory and sets up the workspace with direnv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
## clones a Go project into a new directory and sets up the workspace with direnv. | |
## | |
## example: | |
## $ go_clone [email protected]:hashicorp/nomad.git nomad | |
## | |
## * creates the "nomad" directory | |
## * creates a .envrc with "layout go" in it | |
## * clones the repo to $GOPATH/src/<import path> | |
## | |
## the above results in the repo being cloned to $PWD/nomad/src/github.com/hashicorp/nomad | |
import argparse | |
import re | |
import os | |
import subprocess | |
def main(): | |
parser = argparse.ArgumentParser( | |
description="clone a Go repo", | |
) | |
parser.add_argument("repo", help="the remote repo to clone") | |
parser.add_argument("path", help="the directory to clone into") | |
args = parser.parse_args() | |
## @todo match additional remotes per "git fetch" | |
match = re.match(r"(\w+@)?(.*):(.*?)(\.git)?$", args.repo) | |
assert match, "repo url doesn't match pattern" | |
# [email protected]:hashicorp/nomad.git -> github.com/hashicorp/nomad | |
import_path = match.groups()[1] + "/" + match.groups()[2] | |
os.makedirs(args.path) | |
envrc = os.path.abspath(os.path.join(args.path, ".envrc")) | |
with open(envrc, "w") as ofp: | |
print >>ofp, "layout go" | |
subprocess.check_call(["direnv", "allow", envrc]) | |
subprocess.check_call( | |
"""/bin/bash -c 'eval $( direnv export bash ); git clone %s $GOPATH/src/%s && ln -s src/%s repo'""" % (args.repo, import_path, import_path), | |
cwd=args.path, | |
shell=True, | |
) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment