Commit db95b9010f34d83cac007015bbab0389847d378e

Authored by Göran Krampe
1 parent 5fc367ad

Various fixes, moved to lapp for opt parsing.

Showing 1 changed file with 35 additions and 17 deletions
blimp.nim
1 -import md5, os, osproc, parseopt2, strutils, parsecfg, streams 1 +import md5, os, osproc, parseopt2, strutils, parsecfg, streams, lapp
2 2
3 # blimp is a little utility program for handling large files 3 # blimp is a little utility program for handling large files
4 # in git repositories. Its inspired by git-fat and s3annex 4 # in git repositories. Its inspired by git-fat and s3annex
@@ -19,12 +19,12 @@ import md5, os, osproc, parseopt2, strutils, parsecfg, streams @@ -19,12 +19,12 @@ import md5, os, osproc, parseopt2, strutils, parsecfg, streams
19 # <gitroot>/.blimp.conf 19 # <gitroot>/.blimp.conf
20 # ~/blimpstore/.blimp.conf 20 # ~/blimpstore/.blimp.conf
21 21
22 -var blimpStore, remoteBlimpStore: string 22 +var blimpStore, remoteBlimpStore: string = nil
  23 +var verbose: bool
23 24
24 25
25 # Load blimp.conf file, overkill for now but... 26 # Load blimp.conf file, overkill for now but...
26 proc parseConfFile(filename: string) = 27 proc parseConfFile(filename: string) =
27 - echo filename  
28 var f = newFileStream(filename, fmRead) 28 var f = newFileStream(filename, fmRead)
29 if f != nil: 29 if f != nil:
30 var p: CfgParser 30 var p: CfgParser
@@ -47,20 +47,24 @@ proc parseConfFile(filename: string) = @@ -47,20 +47,24 @@ proc parseConfFile(filename: string) =
47 quit("Parsing " & filename & ": " & e.msg) 47 quit("Parsing " & filename & ": " & e.msg)
48 close(p) 48 close(p)
49 49
  50 +# Trivial helper to enable verbose
  51 +proc run(cmd: string): auto =
  52 + if verbose: echo(cmd)
  53 + execCmd(cmd)
50 54
51 # Upload a file to the remote master blimpStore 55 # Upload a file to the remote master blimpStore
52 proc uploadFile(blimpFilename: string) = 56 proc uploadFile(blimpFilename: string) =
53 if remoteBlimpStore.isNil: 57 if remoteBlimpStore.isNil:
54 echo("Remote blimpstore not set in configuration file, not uploading content:\n\t" & blimpFilename) 58 echo("Remote blimpstore not set in configuration file, not uploading content:\n\t" & blimpFilename)
55 return 59 return
56 - let errorCode = execCmd("rsync -a " & blimpStore / blimpFilename & " " & remoteBlimpStore) 60 + let errorCode = run("rsync -a " & blimpStore / blimpFilename & " " & remoteBlimpStore)
57 if errorCode != 0: quit("Something went wrong uploading content to " & remoteBlimpStore, 2) 61 if errorCode != 0: quit("Something went wrong uploading content to " & remoteBlimpStore, 2)
58 62
59 # Download a file to the remote master blimpStore 63 # Download a file to the remote master blimpStore
60 proc downloadFile(blimpFilename: string) = 64 proc downloadFile(blimpFilename: string) =
61 if remoteBlimpStore.isNil: 65 if remoteBlimpStore.isNil:
62 quit("Remote blimpstore not set in configuration file, can not download content:\n\t" & blimpFilename) 66 quit("Remote blimpstore not set in configuration file, can not download content:\n\t" & blimpFilename)
63 - let errorCode = execCmd("rsync -a " & remoteBlimpStore / blimpFilename & " " & blimpStore / "") 67 + let errorCode = run("rsync -a " & remoteBlimpStore / blimpFilename & " " & blimpStore / "")
64 if errorCode != 0: quit("Something went wrong downloading " & blimpFilename & " from " & remoteBlimpStore, 3) 68 if errorCode != 0: quit("Something went wrong downloading " & blimpFilename & " from " & remoteBlimpStore, 3)
65 69
66 70
@@ -79,26 +83,32 @@ proc copyFromblimpStore(blimpFilename, filename: string) = @@ -79,26 +83,32 @@ proc copyFromblimpStore(blimpFilename, filename: string) =
79 83
80 # Copy original file to blimpStore and replace with hash stub in git. 84 # Copy original file to blimpStore and replace with hash stub in git.
81 proc deflate(filename: string) = 85 proc deflate(filename: string) =
82 - let content = readFile(filename) 86 + var content: string
  87 + try:
  88 + content = readFile(filename)
  89 + except:
  90 + quit("Failed opening file: " & filename, 1)
83 if content[0..4] == "hash:": 91 if content[0..4] == "hash:":
84 quit("File is already deflated, ignored.", 5) 92 quit("File is already deflated, ignored.", 5)
85 let hash = getMD5(content) 93 let hash = getMD5(content)
86 let blimpFilename = filename & "-" & hash 94 let blimpFilename = filename & "-" & hash
87 copyToBlimpStore(filename, blimpFilename) 95 copyToBlimpStore(filename, blimpFilename)
88 writeFile(filename, "hash:" & blimpFilename) 96 writeFile(filename, "hash:" & blimpFilename)
89 - 97 + echo("\t" & filename & " deflated.")
  98 +
90 # Parse out hash from hash stub and copy back original content from blimpStore. 99 # Parse out hash from hash stub and copy back original content from blimpStore.
91 proc inflate(filename: string) = 100 proc inflate(filename: string) =
92 var hashfile: File 101 var hashfile: File
93 if not open(hashfile, filename): 102 if not open(hashfile, filename):
94 - quit("Could not open file: " & filename, 4) 103 + quit("Failed opening file: " & filename, 4)
95 let hashline = split(string(readLine(hashfile)), {':'}) 104 let hashline = split(string(readLine(hashfile)), {':'})
96 if hashline[0] == "hash": 105 if hashline[0] == "hash":
97 let blimpfilename = hashline[1] 106 let blimpfilename = hashline[1]
98 #removeFile(filename) 107 #removeFile(filename)
99 copyFromblimpStore(blimpfilename, filename) 108 copyFromblimpStore(blimpfilename, filename)
100 else: 109 else:
101 - quit("File is not a blimp file.", 5) 110 + quit("\t" & filename & " is not deflated.", 5)
  111 + echo("\t" & filename & " inflated.")
102 112
103 # Find git root dir or fall back on current dir 113 # Find git root dir or fall back on current dir
104 proc gitRoot(): string = 114 proc gitRoot(): string =
@@ -111,6 +121,13 @@ proc gitRoot(): string = @@ -111,6 +121,13 @@ proc gitRoot(): string =
111 except: 121 except:
112 result = getCurrentDir() 122 result = getCurrentDir()
113 123
  124 +let help = """
  125 + blimp [options] <command> <filenames...>
  126 + -v,--verbose Verbosity
  127 + <command> (string) (i)nflate or (d)eflate
  128 + <filenames> (string...) One or more filepaths to inflate/deflate
  129 + """
  130 +
114 ################################ main ##################################### 131 ################################ main #####################################
115 132
116 # Hardwired to "blimpstore" directory in home dir. 133 # Hardwired to "blimpstore" directory in home dir.
@@ -127,18 +144,19 @@ except: @@ -127,18 +144,19 @@ except:
127 parseConfFile(gitRoot() / ".blimp.conf") 144 parseConfFile(gitRoot() / ".blimp.conf")
128 parseConfFile(blimpStore / ".blimp.conf") 145 parseConfFile(blimpStore / ".blimp.conf")
129 146
130 -# Only a command and a path as argument  
131 -# TODO: Change to lapp  
132 -let argv = commandLineParams()  
133 -let command = argv[0]  
134 -let filename = argv[1]  
135 - 147 +# Using lapp to get args
  148 +let args = parse(help)
  149 +let command = args["command"].asString
  150 +let filenames = args["filenames"].asSeq
  151 +verbose = args["verbose"].asBool
136 152
137 # Do the deed 153 # Do the deed
138 if command == "d" or command == "deflate": 154 if command == "d" or command == "deflate":
139 - deflate(filename) 155 + for fn in filenames:
  156 + deflate(fn.asString)
140 elif command == "i" or command == "inflate": 157 elif command == "i" or command == "inflate":
141 - inflate(filename) 158 + for fn in filenames:
  159 + inflate(fn.asString)
142 else: 160 else:
143 quit("Unknown command, only (d)eflate or (i)inflate are valid.", 6) 161 quit("Unknown command, only (d)eflate or (i)inflate are valid.", 6)
144 162