#!/bin/python3
from fabric.api import *
from fabric.contrib import files
# Local git managment automation tools
# copyright matt birkland, Savelono.com 2017
# License: GPLv2
#
#
# Set the username
env.user = "git"
# Set the password [NOT RECOMMENDED]
env.password = ""
env.hosts = "10.1.10.10"
class FabricException(Exception):
pass
env.abort_exception = FabricException
#
# newgit pseudo code
# ------------
#
# - Prompt for name of newcode
# - Prompt for install directory
# - Test that directory starts with /
# - Test for tailing / & remove it
# - Display Git Repo build info
# - Test to see if project already exsist
# - Create git folder and intialize
# - Clone to local folder of choice
#
#------------------------------------------
def newgit():
while True:
git_name = input("Please enter name. Example, 'myproject' :")
if not git_name:
print("You must enter a name. Please try again or quit(ctrl-c).")
print()
continue
else:
break
print()
print()
print("Current ~/git/ tree:")
print("----------------------------------------------------")
local("ls -al ~/git")
print("----------------------------------------------------")
print()
while True:
git_dir = input("Please enter folder(default ~/git). Example, '/home/matt/software' :")
if not git_dir:
git_dir = "~/git"
break
# check for slash as first character in valid unix directory
elif git_dir[:1] != "/" and git_dir[:1] != "~":
print("ERROR: {}/{}".format(git_dir,git_name))
print("Please enter valid directory path. Example, '/home/matt/git/somedir' or ~/git/somedir.")
continue
# check for trailing slash and remove it
elif git_dir[-1:] == "/":
git_dir = git_dir[:-1]
break
else:
break
print("====================================================")
print(" Git Server info")
print("---------------------------")
print("Git Server: {}".format(env.hosts))
print("Git User: {}".format(env.user))
print("Git Project: {}:/git/{}.git".format(env.hosts,git_name))
print()
print(" Git Local info")
print("---------------------------")
print("Git Poject name: {}".format(git_name))
print("Git Directory: {}/{}".format(git_dir,git_name))
print("=====================================================")
print()
print()
while True:
this_ok = input("Is this correct? ('yes' or 'no') :")
if this_ok == "yes":
try:
with settings(warn_only=False):
check = run("mkdir ~/code/{}.git".format(git_name))
print()
print()
break
except FabricException:
print("Git repo already in use! Please run again and select a different name for the git project")
print()
exit(1)
elif this_ok == "no":
print("Exiting...")
exit()
else:
print()
print("=====================================================")
print(" Please Enter 'yes' or 'no'. Lowercase only please. ")
print("=====================================================")
print()
continue
# Intialize Git directory
try:
with settings(warn_only=False):
intialize_git = run("git init --bare ~/code/{}.git".format(git_name))
print()
print("Remote Git repo configured!")
print()
print()
except FabricException:
print("Unknown error")
print()
exit(1)
# Create Project directory
try:
with settings(warn_only=False):
clone_git = local("mkdir {}".format(git_dir))
print()
print()
except FabricException:
print("Directory exist already!")
print()
# Clone Git repo
try:
with settings(warn_only=False) and lcd("{}".format(git_dir)):
clone_git = local("git clone git@10.1.10.10:/git/{}.git".format(git_name))
print()
print("Git repo cloned to {}/{}!".format(git_dir,git_name))
print()
exit(1)
except FabricException:
print("Unknown error")
print()
exit(1)