Commits

Benjamin Althues committed c40c2a2

Save labels to labelfile when using the -c option

Try to add labels that are passed as arguments to labelfile, when
passing the '-c, --create' flag. The labels will be saved along with any
'-l', '-a', and/or '-n' argument.

$ dispass -c -l 12 -a dispass2 -n 14 label_1 label_2

will display the passphrases and add the entries to the labelfile:
+---------+--------+----------+--------+
| Label | Length | Algo | Number |
+---------+--------+----------+--------+
| label_1 | 12 | dispass2 | 14 |
| label_2 | 12 | dispass2 | 14 |
+---------+--------+----------+--------+

Comments (0)

Files changed (2)

 import algos
 
 from dispass import versionStr
+from filehandler import Filehandler
 
 try:
     import curses
 class CLI:
     '''Command Line Interface handling'''
 
-    promptDouble = False
-    '''Boolean. Prompt for password twice'''
+    createLabel = False
+    '''Boolean. Prompt for password twice and save label to labelfile'''
 
     scriptableIO = None
     '''Boolean. Optimize input/output for wrapping dispass'''
         self.algorithm = settings.algorithm
         self.passphraseLength = settings.passphrase_length
         self.seqno = settings.sequence_number
+        self.settings = settings
         self.useCurses = hasCurses
 
     def setAlgo(self, algo):
 
         self.passphraseLength = length
 
-    def setPrompt(self, promptDouble=False):
-        '''Set options for the passwordPrompt)
-
-        :Parameters:
-            - `promptDouble`: Boolean. Prompt 2x and compare passwords
-        '''
-        self.promptDouble = promptDouble
-
     def passwordPrompt(self):
         '''Prompt for password.
 
                 print 'Please try again.'
                 continue
 
-            if self.promptDouble:
+            if self.createLabel:
                 inp2 = getpass.getpass("Password (again): ")
                 if inp == inp2:
                     break
 
         return inp
 
-    def interactive(self, labels):
+    def interactive(self, labels, filehandler):
         '''Start interactive prompt, generating and showing the passprase(s)
 
         :Parameters:
         '''
 
         password = self.passwordPrompt()
-
         algo_dispass1 = algos.Dispass1()
         algo_dispass2 = algos.Dispass2()
+        fh = filehandler
+        added = False
+        saved = False
 
         if isinstance(labels, list):
             labelmap = []
             for i in labels:
                 labelmap.append((i, (self.passphraseLength, self.algorithm)))
+                if fh.add(labelname=i, length=self.passphraseLength,
+                          algo=self.algorithm, seqno=self.seqno):
+                    added = True
+            if added and fh.save():
+                saved = True
 
             if self.algorithm == 'dispass1':
                 self.passphrases = algo_dispass1.digestPasswordDict(
 
             divlen = len(max(labels, key=len)) + 2
             self.passphrases = dict(self.passphrases)
+
         elif isinstance(labels, dict):
             for algo, labels in labels.iteritems():
                 if algo == 'dispass1':
                 else:
                     print "{:{fill}} {}".format(label, passphrase, fill=divlen)
         self.passphrases = {}
+
+        if saved:
+            print('Succesfully added label(s) to {loc}'
+                  .format(loc=fh.file_location))
+        if self.createLabel and not saved:
+            print('error: could not save to "{loc}"\n'
+                  .format(loc=fh.file_location))

dispass/dispass.py

                     return 1
                 console.setSeqNo(seqno)
             elif o in ("-c", "--create"):
-                console.setPrompt(promptDouble=True)
+                console.createLabel = True
             elif o in ("-l", "--length"):
                 try:
                     length = int(a)
             else:
                 assert False, "unhandled option"
 
+        if f_flag:
+            lf = Filehandler(settings, file_location=f_flag)
+        else:
+            lf = Filehandler(settings)
+
         if labels:
-            console.interactive(labels)
+            console.interactive(labels, lf)
         else:
             if a_flag:
                 print('error: option -a can only be used when specifying '
                       'label(s) as argument(s)')
                 return 1
 
-            if f_flag:
-                lf = Filehandler(settings, file_location=f_flag)
-            else:
-                lf = Filehandler(settings)
-
             if lf.file_found:
                 console.interactive(lf.algodict)
                 return