Last active
August 29, 2015 14:12
-
-
Save s-shin/3661d2ba02bd3bb2f0f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| { | |
| "name": "github.com/s-shin/sample", | |
| "dependencies": [ | |
| "github.com/gorilla/mux" | |
| ] | |
| } |
This file contains hidden or 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 | |
| import os | |
| import sys | |
| import copy | |
| import json | |
| import textwrap | |
| import subprocess | |
| PROJECT_ROOT = os.getcwd() | |
| GOPATH = os.path.join(PROJECT_ROOT, "local") | |
| GOPATH_SRC = os.path.join(GOPATH, "src") | |
| GOW_FILE_PATH = os.path.join(PROJECT_ROOT, "gow.json") | |
| def do_go(*opts): | |
| cmd = ["go"] + list(opts) | |
| env = copy.deepcopy(os.environ) | |
| env["GOPATH"] = GOPATH | |
| try: | |
| subprocess.call(cmd, env=env) | |
| except KeyboardInterrupt: | |
| print("") | |
| sys.exit(0) | |
| def do_gow_init(project_data): | |
| if project_data != None: | |
| name = project_data["name"] | |
| if name == None: | |
| try: | |
| name = raw_input("project name: ") | |
| except KeyboardInterrupt: | |
| print("") | |
| sys.exit(1) | |
| project_path_in_gopath = os.path.join(GOPATH_SRC, name) | |
| try: | |
| # mkdir -p local/src/path/to | |
| os.makedirs(os.path.dirname(project_path_in_gopath)) | |
| except: | |
| pass | |
| try: | |
| # ln -s ../../../.. local/src/path/to/project | |
| os.symlink( | |
| os.path.join(*([".."]*(len(name.split("/"))+1))), | |
| project_path_in_gopath | |
| ) | |
| except: | |
| pass | |
| if project_data == None: | |
| with open(GOW_FILE_PATH, "w") as f: | |
| json.dump({"name": name, "dependencies": []}, f, indent=2) | |
| def do_gow_install(project_data): | |
| for dep in project_data["dependencies"]: | |
| do_go("get", dep) | |
| def load_project_data(): | |
| try: | |
| with open(GOW_FILE_PATH) as f: | |
| return json.load(f) | |
| except: | |
| return None | |
| def main(): | |
| project_data = load_project_data() | |
| cmd = sys.argv[1] if len(sys.argv) > 1 else None | |
| if cmd == None: | |
| print(textwrap.dedent("""\ | |
| gow - a simple wrapper of the go command. | |
| Commands: | |
| gow init | |
| gow installdeps | |
| gow (any go options) | |
| """)) | |
| sys.exit(1) | |
| elif cmd == "init": | |
| do_gow_init(project_data) | |
| elif cmd == "installdeps": | |
| do_gow_install(project_data) | |
| else: | |
| do_go(*sys.argv[1:]) | |
| sys.exit(0) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment