Restructure Git handling

This commit is contained in:
Sangelo 2023-03-06 19:14:14 +01:00
parent 7ebf0298b1
commit 67fdb77a0d

View file

@ -2,10 +2,11 @@ import os
import strutils import strutils
import strformat import strformat
# Check for files in directory
var count = 0 var count = 0
for entry in walkDir("."): for entry in walkDir("."):
count += 1 count += 1
# If there are files, ask for confirmation, otherwise continue.
if count == 0: if count == 0:
echo "Initialising..." echo "Initialising..."
else: else:
@ -19,6 +20,7 @@ else:
echo "Cancelling..." echo "Cancelling..."
quit 0 quit 0
# Define how to handle failed copies
proc copyFile(src, dest: string): bool = proc copyFile(src, dest: string): bool =
try: try:
os.copyFile(src, dest) os.copyFile(src, dest)
@ -28,6 +30,7 @@ proc copyFile(src, dest: string): bool =
echo "Failed to copy ", src, " to ", dest echo "Failed to copy ", src, " to ", dest
return false return false
# Define how to handle failed directory creations
proc createDir(dir: string): bool = proc createDir(dir: string): bool =
try: try:
os.createDir(dir) os.createDir(dir)
@ -37,21 +40,40 @@ proc createDir(dir: string): bool =
echo "Failed to create ", dir echo "Failed to create ", dir
return false return false
# Define how to handle configuring a git repo
proc configureGitRepo(gitRemote, gitBranch: string) =
let setupGitRepo = execShellCmd(&"git remote add origin {gitRemote}")
let setGitBranch = execShellCmd(&"git branch -m {gitBranch}")
if setupGitRepo == 0:
echo &"Successfully added remote {gitRemote}"
else:
echo &"Failed to add remote {gitRemote}"
if setGitBranch == 0:
echo &"Successfully set branch to {gitBranch}"
else:
echo &"Failed to set branch {gitBranch}"
#-------------------------------------------------------------------------------#
# Set necessary variables (home directory, config path)
let homeDir = getEnv("HOME") let homeDir = getEnv("HOME")
let vscodeDir = ".vscode" let vscodeDir = ".vscode"
let tasksPath = joinPath(homeDir, "/.config/niminit/tasks.json") let tasksPath = joinPath(homeDir, "/.config/niminit/tasks.json")
let launchPath = joinPath(homeDir, "/.config/niminit/launch.json") let launchPath = joinPath(homeDir, "/.config/niminit/launch.json")
# Create .vscode directory and copy files into it
let vscDirCreated = createDir(vscodeDir) let vscDirCreated = createDir(vscodeDir)
let tasksCopied = copyFile(tasksPath, joinPath(vscodeDir, "tasks.json")) let tasksCopied = copyFile(tasksPath, joinPath(vscodeDir, "tasks.json"))
let launchCopied = copyFile(launchPath, joinPath(vscodeDir, "launch.json")) let launchCopied = copyFile(launchPath, joinPath(vscodeDir, "launch.json"))
# Check for success and quit if command errored
if vscDirCreated and tasksCopied and launchCopied: if vscDirCreated and tasksCopied and launchCopied:
echo "All files copied successfully" echo "All files copied successfully"
else: else:
echo "Failed to copy one or more files" echo "Failed to copy one or more files"
quit 1 quit 1
# Handle -g parameter for git repos.
if "-g" in (commandLineParams()): if "-g" in (commandLineParams()):
let createdRepo = execShellCmd("git init") let createdRepo = execShellCmd("git init")
if createdRepo == 0: if createdRepo == 0:
@ -64,18 +86,10 @@ if "-g" in (commandLineParams()):
let gitRemote = stdin.readLine() let gitRemote = stdin.readLine()
echo "Enter your desired branch name:" echo "Enter your desired branch name:"
let gitBranch = stdin.readLine() let gitBranch = stdin.readLine()
echo "Setting values..." echo "Configuring Git repo..."
let setupGitRepo = execShellCmd(&"git remote add origin {gitRemote}") configureGitRepo(gitRemote, gitBranch)
let setGitBranch = execShellCmd(&"git branch -m {gitBranch}")
if setupGitRepo == 0:
echo &"Successfully added remote {gitRemote}"
else:
echo &"Failed to add remote {gitRemote}"
if setGitBranch == 0:
echo &"Successfully set branch to {gitBranch}"
else:
echo &"Failed to set branch {gitBranch}"
else: else:
echo "Skipping repository configuration..."
echo "Successfully created local git repository!" echo "Successfully created local git repository!"
else: else:
echo: "Failed creating local git repository." echo: "Failed creating local git repository."