First alpha release
This commit is contained in:
parent
34e6a95517
commit
0508a3364d
4 changed files with 170 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -2,4 +2,4 @@
|
|||
nimcache/
|
||||
nimblecache/
|
||||
htmldocs/
|
||||
|
||||
ncp
|
||||
|
|
51
Makefile
Normal file
51
Makefile
Normal file
|
@ -0,0 +1,51 @@
|
|||
##
|
||||
# Ncp
|
||||
#
|
||||
# @file
|
||||
# @version 0.1
|
||||
|
||||
SRC = *.nim
|
||||
SRCDIR = src
|
||||
BIN = ncp
|
||||
PREFIX = /usr/local
|
||||
|
||||
all : ncp
|
||||
.PHONY : all
|
||||
|
||||
ncp:
|
||||
nimble build '-d:ssl --cc:clang -d:release'
|
||||
|
||||
.PHONY: debug
|
||||
debug:
|
||||
nimble build '-d:ssl --cc:clang'
|
||||
|
||||
.PHONY: small
|
||||
small:
|
||||
nimble build '-d:ssl --cc:gcc -d:danger -d:strip --opt:size -d:release --passC:-flto --passL:-flto'
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f ./${BIN}
|
||||
|
||||
.PHONY: docs
|
||||
docs:
|
||||
nim doc ${SRCDIR}/${SRC}
|
||||
|
||||
.PHONY: pretty
|
||||
pretty:
|
||||
nimpretty ${SRCDIR}/${SRC}
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
nimble test
|
||||
|
||||
.PHONY: install
|
||||
install: ${BIN}
|
||||
mkdir -p $(DESTDIR)$(PREFIX)/bin/
|
||||
cp $< $(DESTDIR)$(PREFIX)/bin/${BIN}
|
||||
|
||||
.PHONY: uninstall
|
||||
uninstall:
|
||||
rm -f $(DESTDIR)$(PREFIX)/bin/${BIN}
|
||||
|
||||
# end
|
14
ncp.nimble
Normal file
14
ncp.nimble
Normal file
|
@ -0,0 +1,14 @@
|
|||
# Package
|
||||
|
||||
version = "0.1.0"
|
||||
author = "Kyle Brown"
|
||||
description = "File magement written in Nim"
|
||||
license = "MIT"
|
||||
srcDir = "src"
|
||||
bin = @["ncp"]
|
||||
|
||||
|
||||
# Dependencies
|
||||
|
||||
requires "nim >= 1.4.2"
|
||||
requires "docopt >= 0.6.7"
|
104
src/ncp.nim
Normal file
104
src/ncp.nim
Normal file
|
@ -0,0 +1,104 @@
|
|||
let doc = """
|
||||
ncp is a very basic cp written in nim with some modern defaults.
|
||||
|
||||
Usage:
|
||||
ncp <path>... [--recurse] [--posix] [--verbose]
|
||||
ncp (-h | --help)
|
||||
ncp [--version]
|
||||
|
||||
Options:
|
||||
-h, --help Show this screen.
|
||||
-r, --recurse Enables recursive finding of files (Enabled by default)
|
||||
-i, --interactive Prompts the user for action to overwrite
|
||||
-v, --verbose Enables verbose output
|
||||
--posix Disables default enables
|
||||
--version Show version.
|
||||
"""
|
||||
|
||||
|
||||
import docopt
|
||||
import lists
|
||||
import os
|
||||
import strformat
|
||||
import strutils
|
||||
|
||||
func exists(check: string): bool =
|
||||
return bool(fileExists(check) or dirExists(check))
|
||||
|
||||
func stripPath(input: string): string =
|
||||
let file = splitPath(input)
|
||||
return file.tail
|
||||
|
||||
proc isFile(check: string): bool =
|
||||
if check.exists:
|
||||
return bool(os.getFileInfo(check).kind == os.pcFile)
|
||||
|
||||
proc confirm(input: string): bool =
|
||||
while true:
|
||||
fmt"Overwrite '{input}'?".echo
|
||||
let response = readLine(stdin).toLowerAscii
|
||||
if response == "y" or response == "yes":
|
||||
return true
|
||||
elif response == "n" or response == "no":
|
||||
return false
|
||||
else:
|
||||
"Please enter \"Y\" or \"N\"".echo
|
||||
|
||||
proc copy(f, t: string, recurse, interactive, verbose: bool) =
|
||||
let to = fmt"{t}/{f.stripPath}"
|
||||
if verbose: fmt"Copying from {f}, to {to}".echo
|
||||
if f.isFile: # input is a file
|
||||
if to.exists and not to.isFile: # file to directory
|
||||
copyFileWithPermissions(f, to)
|
||||
elif not to.exists: # file to file, doesn't exist
|
||||
copyFileWithPermissions(f, to)
|
||||
else: # file to file does exist
|
||||
if interactive and confirm(to):
|
||||
if to.isFile:
|
||||
to.removeFile
|
||||
# Attempts again if file deleted. Disabled verbosity on recursion
|
||||
copy(f, t, recurse, interactive, false)
|
||||
else: to.removeDir
|
||||
|
||||
else: # f is a directory
|
||||
if recurse:
|
||||
copyDirWithPermissions(f, t)
|
||||
|
||||
|
||||
proc main() =
|
||||
var
|
||||
from_list = initDoublyLinkedList[string]()
|
||||
recurse: bool
|
||||
to: string
|
||||
interactive: bool
|
||||
verbose: bool
|
||||
posix: bool
|
||||
|
||||
let args = docopt(doc, version = "cp 0.1")
|
||||
let path = args["<path>"]
|
||||
if posix:
|
||||
if args["--recurse"]: recurse = parseBool($args["--recurse"])
|
||||
if args["--interactive"]: interactive = parseBool($args["--interactive"])
|
||||
if args["--verbose"]: interactive = parseBool($args["--verbose"])
|
||||
else:
|
||||
recurse = true
|
||||
interactive = true
|
||||
verbose = true
|
||||
|
||||
if path.len <= 1:
|
||||
"ncp: Missing file for operand".echo
|
||||
"See 'ncp --help' for more info".echo
|
||||
quit(QuitFailure)
|
||||
|
||||
# Builds from list. Last option is the output dir
|
||||
for i, c in path:
|
||||
if i < path.len-1:
|
||||
from_list.append(c.join)
|
||||
else: to = c.normalizedPath
|
||||
|
||||
for i in from_list:
|
||||
copy(i, to, recurse, interactive, verbose)
|
||||
|
||||
|
||||
when isMainModule:
|
||||
main()
|
Loading…
Add table
Reference in a new issue