diff --git a/src/main.nim b/src/main.nim index 9c9e987..a12a718 100644 --- a/src/main.nim +++ b/src/main.nim @@ -1,18 +1,22 @@ -# src/dashinit.nim +# src/main.nim import os, strutils import subcommands/help import subcommands/init import handlers/configHandler proc main() = + # Create the config file if it doesn't exist already createConfigFile() + + # Check for arguments and initialise directly if none are found let args = commandLineParams() if args.len == 0: init() return - let subcommand = args[0].toLower() + let subcommand = args[0].toLower() ## turn arguments into lowercase + # Check for arguments and execute proper command, if argument is invalid stop the program case subcommand of "help": displayHelp() diff --git a/src/subcommands/init.nim b/src/subcommands/init.nim index 50de29f..88e14fd 100644 --- a/src/subcommands/init.nim +++ b/src/subcommands/init.nim @@ -1,2 +1,79 @@ +import os, strutils, strformat +# import parsetoml + +# Define how to handle failed copies +proc copyFile(src, dest: string): bool = + try: + os.copyFile(src, dest) + echo "Successfully copied ", src, " to ", dest + return true + except: + echo "Failed to copy ", src, " to ", dest + return false + +# Define how to handle failed directory creations +proc createDir(dir: string): bool = + try: + os.createDir(dir) + echo "Successfully created ", dir + return true + except: + echo "Failed to create ", dir + 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}" + +#-------------------------------------------------------------------------------# + +# Check for files in directory proc init*() = - echo "Initialising Project..." \ No newline at end of file + var count = 0 + for entry in walkDir("."): + count += 1 + # If there are files, ask for confirmation, otherwise continue. + if not count == 0: + echo "This directory is not empty." + echo "Continue initialising? [y/N]" + var input = stdin.readLine() + case input.toLower + of "yes", "y", "z", "j": + echo "Initialising non-empty directory..." + else: + echo "Cancelling..." + quit 0 + else: + echo "Initialising..." + + # Handle -g parameter for git repos. + if "-g" in (commandLineParams()): + let createdRepo = execShellCmd("git init") + if createdRepo == 0: + echo "Successfully initialised local git repository!" + echo "Would you like to configure it? [Y/n]" + var input = stdin.readLine() + case input.toLower + of "yes", "y", "z", "j", "": + echo "Enter the git remote:" + let gitRemote = stdin.readLine() + echo "Enter your desired branch name:" + let gitBranch = stdin.readLine() + echo "Configuring Git repo..." + configureGitRepo(gitRemote, gitBranch) + else: + echo "Skipping repository configuration..." + echo "Successfully created local git repository!" + else: + echo: "Failed creating local git repository." + + echo "Done!" \ No newline at end of file