Commit db95b9010f34d83cac007015bbab0389847d378e
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 | 3 | # blimp is a little utility program for handling large files |
4 | 4 | # in git repositories. Its inspired by git-fat and s3annex |
... | ... | @@ -19,12 +19,12 @@ import md5, os, osproc, parseopt2, strutils, parsecfg, streams |
19 | 19 | # <gitroot>/.blimp.conf |
20 | 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 | 26 | # Load blimp.conf file, overkill for now but... |
26 | 27 | proc parseConfFile(filename: string) = |
27 | - echo filename | |
28 | 28 | var f = newFileStream(filename, fmRead) |
29 | 29 | if f != nil: |
30 | 30 | var p: CfgParser |
... | ... | @@ -47,20 +47,24 @@ proc parseConfFile(filename: string) = |
47 | 47 | quit("Parsing " & filename & ": " & e.msg) |
48 | 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 | 55 | # Upload a file to the remote master blimpStore |
52 | 56 | proc uploadFile(blimpFilename: string) = |
53 | 57 | if remoteBlimpStore.isNil: |
54 | 58 | echo("Remote blimpstore not set in configuration file, not uploading content:\n\t" & blimpFilename) |
55 | 59 | return |
56 | - let errorCode = execCmd("rsync -a " & blimpStore / blimpFilename & " " & remoteBlimpStore) | |
60 | + let errorCode = run("rsync -a " & blimpStore / blimpFilename & " " & remoteBlimpStore) | |
57 | 61 | if errorCode != 0: quit("Something went wrong uploading content to " & remoteBlimpStore, 2) |
58 | 62 | |
59 | 63 | # Download a file to the remote master blimpStore |
60 | 64 | proc downloadFile(blimpFilename: string) = |
61 | 65 | if remoteBlimpStore.isNil: |
62 | 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 | 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 | 83 | |
80 | 84 | # Copy original file to blimpStore and replace with hash stub in git. |
81 | 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 | 91 | if content[0..4] == "hash:": |
84 | 92 | quit("File is already deflated, ignored.", 5) |
85 | 93 | let hash = getMD5(content) |
86 | 94 | let blimpFilename = filename & "-" & hash |
87 | 95 | copyToBlimpStore(filename, blimpFilename) |
88 | 96 | writeFile(filename, "hash:" & blimpFilename) |
89 | - | |
97 | + echo("\t" & filename & " deflated.") | |
98 | + | |
90 | 99 | # Parse out hash from hash stub and copy back original content from blimpStore. |
91 | 100 | proc inflate(filename: string) = |
92 | 101 | var hashfile: File |
93 | 102 | if not open(hashfile, filename): |
94 | - quit("Could not open file: " & filename, 4) | |
103 | + quit("Failed opening file: " & filename, 4) | |
95 | 104 | let hashline = split(string(readLine(hashfile)), {':'}) |
96 | 105 | if hashline[0] == "hash": |
97 | 106 | let blimpfilename = hashline[1] |
98 | 107 | #removeFile(filename) |
99 | 108 | copyFromblimpStore(blimpfilename, filename) |
100 | 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 | 113 | # Find git root dir or fall back on current dir |
104 | 114 | proc gitRoot(): string = |
... | ... | @@ -111,6 +121,13 @@ proc gitRoot(): string = |
111 | 121 | except: |
112 | 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 | 131 | ################################ main ##################################### |
115 | 132 | |
116 | 133 | # Hardwired to "blimpstore" directory in home dir. |
... | ... | @@ -127,18 +144,19 @@ except: |
127 | 144 | parseConfFile(gitRoot() / ".blimp.conf") |
128 | 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 | 153 | # Do the deed |
138 | 154 | if command == "d" or command == "deflate": |
139 | - deflate(filename) | |
155 | + for fn in filenames: | |
156 | + deflate(fn.asString) | |
140 | 157 | elif command == "i" or command == "inflate": |
141 | - inflate(filename) | |
158 | + for fn in filenames: | |
159 | + inflate(fn.asString) | |
142 | 160 | else: |
143 | 161 | quit("Unknown command, only (d)eflate or (i)inflate are valid.", 6) |
144 | 162 | ... | ... |