Start work on init command

This commit is contained in:
Sangelo 2023-10-06 15:14:13 +02:00
parent bec6cc8c58
commit 06b129bcbf
2 changed files with 84 additions and 3 deletions

View file

@ -1,18 +1,22 @@
# src/dashinit.nim # src/main.nim
import os, strutils import os, strutils
import subcommands/help import subcommands/help
import subcommands/init import subcommands/init
import handlers/configHandler import handlers/configHandler
proc main() = proc main() =
# Create the config file if it doesn't exist already
createConfigFile() createConfigFile()
# Check for arguments and initialise directly if none are found
let args = commandLineParams() let args = commandLineParams()
if args.len == 0: if args.len == 0:
init() init()
return 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 case subcommand
of "help": of "help":
displayHelp() displayHelp()

View file

@ -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*() = proc init*() =
echo "Initialising Project..." 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!"