Snippets

Paweł R Install golang on OSX

Created by Paweł R

File go_install.py Added

  • Ignore whitespace
  • Hide word diff
+#!/usr/bin/python
+
+import sys
+import getopt
+import os
+from os.path import expanduser
+
+def main(argv):
+    userName = "user"
+    gopath = "golang"
+
+    try:
+        opts, args = getopt.getopt(argv, "hug:d", ["help", "user=", "gopath="])
+    except getopt.GetoptError:
+        usage()
+        sys.exit(2)
+
+    for opt, arg in opts:
+        if opt in ("-h", "--help"):
+            usage()
+            sys.exit()
+        elif opt == '-d':
+            global _debug
+            _debug = 1
+        elif opt in ("-u", "--user"):
+            userName = arg
+        elif opt in ("-g", "--gopath"):
+            gopath = arg
+
+    home = expanduser("~")
+    bashrc = home + "/.bashrc"
+    zshrc = home + "/.zshrc"
+
+    os.system("mkdir %s" % home + "/" + gopath)
+    if not userName == "user":
+        os.system("mkdir -p %s" % home + "/src/github.com/" + userName)
+
+    append_line_to_file("export GOPATH=$gopath", bashrc)
+    append_line_to_file("export GOROOT=/usr/local/opt/go/libexec", bashrc)
+    append_line_to_file("export PATH=$PATH:$GOPATH/bin", bashrc)
+    append_line_to_file("export PATH=$PATH:$GOROOT/bin", bashrc)
+
+    append_line_to_file("export GOPATH=$gopath", zshrc)
+    append_line_to_file("export GOROOT=/usr/local/opt/go/libexec", zshrc)
+    append_line_to_file("export PATH=$PATH:$GOPATH/bin", zshrc)
+    append_line_to_file("export PATH=$PATH:$GOROOT/bin", zshrc)
+
+    if (which("brew") == None):
+        installBrew = query_yes_no("Do you want to intstall brew?")
+        if installBrew:
+            success = os.system('ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"')
+            if success == False:
+                print "Failed to install brew! Maybe try manual instal?"
+                sys.exit(2)
+        else:
+            print "Aborting instalation"
+            sys.exit(2)
+
+    os.system('brew update')
+    os.system('brew install go')
+    os.system('brew install git')
+    os.system('brew install mercurial')
+    success = True
+    if success:
+        success = os.system('go get golang.org/x/tools/cmd/godoc | go get golang.org/x/tools/cmd/vet')
+        if success:
+            print "Successfully installed golang"
+        else:
+            print "Failed to install golang"
+            sys.exit(2)
+    else:
+        print "Failed to install golang"
+        sys.exit(2)
+
+def usage():
+    print "python golangInstall.py [-u gitUserName] [-g gopath] [-h]"
+
+def which(program):
+    import os
+    def is_exe(fpath):
+        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
+
+    fpath, fname = os.path.split(program)
+    if fpath:
+        if is_exe(program):
+            return program
+    else:
+        for path in os.environ["PATH"].split(os.pathsep):
+            path = path.strip('"')
+            exe_file = os.path.join(path, program)
+            if is_exe(exe_file):
+                return exe_file
+
+    return None
+
+def query_yes_no(question, default="yes"):
+    """Ask a yes/no question via raw_input() and return their answer.
+
+    "question" is a string that is presented to the user.
+    "default" is the presumed answer if the user just hits <Enter>.
+        It must be "yes" (the default), "no" or None (meaning
+        an answer is required of the user).
+
+    The "answer" return value is True for "yes" or False for "no".
+    """
+    valid = {"yes": True, "y": True, "ye": True,
+             "no": False, "n": False}
+    if default is None:
+        prompt = " [y/n] "
+    elif default == "yes":
+        prompt = " [Y/n] "
+    elif default == "no":
+        prompt = " [y/N] "
+    else:
+        raise ValueError("invalid default answer: '%s'" % default)
+
+    while True:
+        sys.stdout.write(question + prompt)
+        choice = raw_input().lower()
+        if default is not None and choice == '':
+            return valid[default]
+        elif choice in valid:
+            return valid[choice]
+        else:
+            sys.stdout.write("Please respond with 'yes' or 'no' "
+                             "(or 'y' or 'n').\n")
+
+def append_line_to_file(line, file):
+    value = str(line)
+    with open(file, 'a+') as f:
+        f.seek(0)
+        if not any(value == x.rstrip('\r\n') for x in f):
+            f.write(value + '\n')
+
+if __name__ == "__main__":
+    main(sys.argv[1:])
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.