Commit b9ad52ff080b32da2fc4c520b48c69c7396a2526

Authored by Göran Krampe
1 parent db95b901

Added default config file and subexes format for download/upload commands.

Showing 1 changed file with 29 additions and 9 deletions
blimp.nim
1 -import md5, os, osproc, parseopt2, strutils, parsecfg, streams, lapp 1 +import md5, os, osproc, parseopt2, strutils, parsecfg, streams, lapp, subexes
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,9 +19,19 @@ import md5, os, osproc, parseopt2, strutils, parsecfg, streams, lapp @@ -19,9 +19,19 @@ import md5, os, osproc, parseopt2, strutils, parsecfg, streams, lapp
19 # <gitroot>/.blimp.conf 19 # <gitroot>/.blimp.conf
20 # ~/blimpstore/.blimp.conf 20 # ~/blimpstore/.blimp.conf
21 21
22 -var blimpStore, remoteBlimpStore: string = nil  
23 -var verbose: bool 22 +var
  23 + blimpStore, uploadCommandFormat, downloadCommandFormat: string
  24 + remoteBlimpStore: string = nil
  25 + verbose: bool
24 26
  27 +let
  28 + defaultConfig = """
  29 +[rsync]
  30 +remote = "blimp@some-rsync-server.com::blimpstore"
  31 +# $1 is filename, $2 is remote and $3 is the local blimpstore
  32 +upload = "rsync --password-file ~/blimp.pass -avzP $3/$1 $2/"
  33 +download = "rsync -avzP $2/$1 $3/"
  34 +"""
25 35
26 # Load blimp.conf file, overkill for now but... 36 # Load blimp.conf file, overkill for now but...
27 proc parseConfFile(filename: string) = 37 proc parseConfFile(filename: string) =
@@ -37,8 +47,13 @@ proc parseConfFile(filename: string) = @@ -37,8 +47,13 @@ proc parseConfFile(filename: string) =
37 of cfgSectionStart: 47 of cfgSectionStart:
38 continue # Ignore 48 continue # Ignore
39 of cfgKeyValuePair: 49 of cfgKeyValuePair:
40 - if e.key == "remote": 50 + case e.key
  51 + of "remote":
41 remoteBlimpStore = e.value 52 remoteBlimpStore = e.value
  53 + of "upload":
  54 + uploadCommandFormat = e.value
  55 + of "download":
  56 + downloadCommandFormat = e.value
42 else: 57 else:
43 quit("Unknown configuration: " & e.key) 58 quit("Unknown configuration: " & e.key)
44 of cfgOption: 59 of cfgOption:
@@ -57,15 +72,17 @@ proc uploadFile(blimpFilename: string) = @@ -57,15 +72,17 @@ proc uploadFile(blimpFilename: string) =
57 if remoteBlimpStore.isNil: 72 if remoteBlimpStore.isNil:
58 echo("Remote blimpstore not set in configuration file, not uploading content:\n\t" & blimpFilename) 73 echo("Remote blimpstore not set in configuration file, not uploading content:\n\t" & blimpFilename)
59 return 74 return
60 - let errorCode = run("rsync -a " & blimpStore / blimpFilename & " " & remoteBlimpStore)  
61 - if errorCode != 0: quit("Something went wrong uploading content to " & remoteBlimpStore, 2) 75 + let errorCode = run(format(uploadCommandFormat, blimpFilename, remoteBlimpStore, blimpStore))
  76 + if errorCode != 0:
  77 + quit("Something went wrong uploading " & blimpFilename & " to " & remoteBlimpStore, 2)
62 78
63 # Download a file to the remote master blimpStore 79 # Download a file to the remote master blimpStore
64 proc downloadFile(blimpFilename: string) = 80 proc downloadFile(blimpFilename: string) =
65 if remoteBlimpStore.isNil: 81 if remoteBlimpStore.isNil:
66 quit("Remote blimpstore not set in configuration file, can not download content:\n\t" & blimpFilename) 82 quit("Remote blimpstore not set in configuration file, can not download content:\n\t" & blimpFilename)
67 - let errorCode = run("rsync -a " & remoteBlimpStore / blimpFilename & " " & blimpStore / "")  
68 - if errorCode != 0: quit("Something went wrong downloading " & blimpFilename & " from " & remoteBlimpStore, 3) 83 + let errorCode = run(format(downloadCommandFormat, blimpFilename, remoteBlimpStore, blimpStore))
  84 + if errorCode != 0:
  85 + quit("Something went wrong downloading " & blimpFilename & " from " & remoteBlimpStore, 3)
69 86
70 87
71 # Copy content to blimpStore, no upload yet. 88 # Copy content to blimpStore, no upload yet.
@@ -135,7 +152,10 @@ blimpStore = getHomeDir() / &quot;blimpstore&quot; @@ -135,7 +152,10 @@ blimpStore = getHomeDir() / &quot;blimpstore&quot;
135 152
136 # Make sure we have the dir, or create it. 153 # Make sure we have the dir, or create it.
137 try: 154 try:
138 - if not existsDir(blimpStore): createDir(blimpStore) 155 + if not existsDir(blimpStore):
  156 + createDir(blimpStore)
  157 + if not existsFile(blimpStore / ".blimp.conf"):
  158 + writeFile(blimpStore / ".blimp.conf", defaultConfig)
139 except: 159 except:
140 quit("Could not create " & blimpStore & " directory.", 1) 160 quit("Could not create " & blimpStore & " directory.", 1)
141 161