6
6
"encoding/csv"
7
7
"errors"
8
8
"fmt"
9
+ "gorm.io/gorm"
9
10
"os"
10
11
"os/exec"
11
12
"os/signal"
@@ -175,14 +176,19 @@ func setActiveDatabasePath(dbPath string) error {
175
176
}
176
177
177
178
if newEncrypted {
178
- // Decrypt new database if it is encrypted
179
- fmt .Printf ("Database %s is encrypted, decrypting it\n " , fullPath )
180
- err , _ = decryptDatabase (fullPath )
181
- if err != nil {
182
- fmt .Printf ("Decryption Error - \" %s\" , not switching databases\n " , err .Error ())
183
- return err
179
+ if ! settings .AutoEncrypt {
180
+ // Decrypt new database if it is encrypted
181
+ fmt .Printf ("Database %s is encrypted, decrypting it\n " , fullPath )
182
+ err , _ = decryptDatabase (fullPath )
183
+ if err != nil {
184
+ fmt .Printf ("Decryption Error - \" %s\" , not switching databases\n " , err .Error ())
185
+ return err
186
+ } else {
187
+ newEncrypted = false
188
+ }
184
189
} else {
185
- newEncrypted = false
190
+ // New database is encrypted and autoencrypt is set - so keep it like that
191
+ // fmt.Printf("Database %s is already encrypted, nothing to do\n", fullPath)
186
192
}
187
193
}
188
194
}
@@ -193,7 +199,7 @@ func setActiveDatabasePath(dbPath string) error {
193
199
return nil
194
200
}
195
201
196
- if newEncrypted {
202
+ if newEncrypted && ! settings . AutoEncrypt {
197
203
// Use should manually decrypt before switching
198
204
fmt .Println ("Auto-encrypt disabled, decrypt new database manually before switching." )
199
205
return nil
@@ -223,6 +229,7 @@ func addNewEntry() error {
223
229
var url string
224
230
var notes string
225
231
var passwd string
232
+ var tags string
226
233
var err error
227
234
var customEntries []CustomEntry
228
235
@@ -250,7 +257,8 @@ func addNewEntry() error {
250
257
}
251
258
// fmt.Printf("Password => %s\n", passwd)
252
259
253
- notes = readInput (reader , "\n Notes" )
260
+ tags = readInput (reader , "\n Tags (separated by space): " )
261
+ notes = readInput (reader , "Notes" )
254
262
255
263
// Title and username/password are mandatory
256
264
if len (title ) == 0 {
@@ -269,7 +277,7 @@ func addNewEntry() error {
269
277
customEntries = addCustomFields (reader )
270
278
271
279
// Trim spaces
272
- err = addNewDatabaseEntry (title , userName , url , passwd , notes , customEntries )
280
+ err = addNewDatabaseEntry (title , userName , url , passwd , tags , notes , customEntries )
273
281
274
282
if err != nil {
275
283
fmt .Printf ("Error adding entry - \" %s\" \n " , err .Error ())
@@ -299,7 +307,7 @@ func addOrUpdateCustomFields(reader *bufio.Reader, entry *Entry) ([]CustomEntry,
299
307
fmt .Println ("Field Name: " + customEntry .FieldName )
300
308
fieldName = readInput (reader , "\t New Field Name (Enter to keep, \" x\" to delete)" )
301
309
if strings .ToLower (strings .TrimSpace (fieldName )) == "x" {
302
- fmt .Println ("Deleting field " + fieldName )
310
+ fmt .Println ("Deleting field: " + customEntry . FieldName )
303
311
} else {
304
312
if strings .TrimSpace (fieldName ) == "" {
305
313
fieldName = customEntry .FieldName
@@ -365,6 +373,7 @@ func editCurrentEntry(idString string) error {
365
373
var title string
366
374
var url string
367
375
var notes string
376
+ var tags string
368
377
var passwd string
369
378
var err error
370
379
var entry * Entry
@@ -407,13 +416,16 @@ func editCurrentEntry(idString string) error {
407
416
}
408
417
// fmt.Printf("Password => %s\n", passwd)
409
418
419
+ fmt .Printf ("\n Current Tags: %s\n " , entry .Tags )
420
+ tags = readInput (reader , "New Tags" )
421
+
410
422
fmt .Printf ("\n Current Notes: %s\n " , entry .Notes )
411
423
notes = readInput (reader , "New Notes" )
412
424
413
425
customEntries , flag := addOrUpdateCustomFields (reader , entry )
414
426
415
427
// Update
416
- err = updateDatabaseEntry (entry , title , userName , url , passwd , notes , customEntries , flag )
428
+ err = updateDatabaseEntry (entry , title , userName , url , passwd , tags , notes , customEntries , flag )
417
429
if err != nil {
418
430
fmt .Printf ("Error updating entry - \" %s\" \n " , err .Error ())
419
431
}
@@ -629,6 +641,9 @@ func copyCurrentEntry(idString string) error {
629
641
630
642
var err error
631
643
var entry * Entry
644
+ var entryNew * Entry
645
+ var exEntries []ExtendedEntry
646
+
632
647
var id int
633
648
634
649
if err = checkActiveDatabase (); err != nil {
@@ -643,12 +658,24 @@ func copyCurrentEntry(idString string) error {
643
658
return err
644
659
}
645
660
646
- err , _ = cloneEntry (entry )
661
+ err , entryNew = cloneEntry (entry )
647
662
if err != nil {
648
663
fmt .Printf ("Error cloning entry: \" %s\" \n " , err .Error ())
649
664
return err
650
665
}
651
666
667
+ exEntries = getExtendedEntries (entry )
668
+
669
+ if len (exEntries ) > 0 {
670
+ fmt .Printf ("%d extended entries found\n " , len (exEntries ))
671
+
672
+ err = cloneExtendedEntries (entryNew , exEntries )
673
+ if err != nil {
674
+ fmt .Printf ("Error cloning extended entries: \" %s\" \n " , err .Error ())
675
+ return err
676
+ }
677
+ }
678
+
652
679
return err
653
680
}
654
681
@@ -684,11 +711,11 @@ func encryptDatabase(dbPath string, givenPasswd *string) error {
684
711
}
685
712
686
713
if len (passwd ) == 0 {
687
- fmt .Printf ("Password: " )
714
+ fmt .Printf ("Encryption Password: " )
688
715
err , passwd = readPassword ()
689
716
690
717
if err == nil {
691
- fmt .Printf ("\n Password again: " )
718
+ fmt .Printf ("\n Encryption Password again: " )
692
719
err , passwd2 = readPassword ()
693
720
if err == nil {
694
721
if passwd != passwd2 {
@@ -736,7 +763,7 @@ func decryptDatabase(dbPath string) (error, string) {
736
763
return err , ""
737
764
}
738
765
739
- fmt .Printf ("Password: " )
766
+ fmt .Printf ("Decryption Password: " )
740
767
err , passwd = readPassword ()
741
768
742
769
if err != nil {
@@ -757,12 +784,65 @@ func decryptDatabase(dbPath string) (error, string) {
757
784
}
758
785
759
786
if err == nil {
760
- fmt .Println ("\n Decryption complete." )
787
+ fmt .Println ("...decryption complete." )
761
788
}
762
789
763
790
return err , passwd
764
791
}
765
792
793
+ // Migrate an existing database to the new schema
794
+ func migrateDatabase (dbPath string ) error {
795
+
796
+ var err error
797
+ var flag bool
798
+ var passwd string
799
+ var db * gorm.DB
800
+
801
+ if _ , err = os .Stat (dbPath ); os .IsNotExist (err ) {
802
+ fmt .Printf ("Error - path %s does not exist\n " , dbPath )
803
+ return err
804
+ }
805
+
806
+ if err , flag = isFileEncrypted (dbPath ); flag {
807
+ err , passwd = decryptDatabase (dbPath )
808
+ }
809
+
810
+ if err != nil {
811
+ return err
812
+ }
813
+
814
+ err , db = openDatabase (dbPath )
815
+
816
+ if err != nil {
817
+ fmt .Printf ("Error opening database path - %s: %s\n " , dbPath , err .Error ())
818
+ return err
819
+ }
820
+
821
+ fmt .Println ("Migrating tables ..." )
822
+ err = db .AutoMigrate (& Entry {})
823
+
824
+ if err != nil {
825
+ fmt .Printf ("Error migrating table \" entries\" - %s: %s\n " , dbPath , err .Error ())
826
+ return err
827
+ }
828
+
829
+ err = db .AutoMigrate (& ExtendedEntry {})
830
+
831
+ if err != nil {
832
+ fmt .Printf ("Error migrating table \" exentries\" - %s: %s\n " , dbPath , err .Error ())
833
+ return err
834
+ }
835
+
836
+ if flag {
837
+ // File was encrypted - encrypt it again
838
+ encryptDatabase (dbPath , & passwd )
839
+ }
840
+
841
+ fmt .Println ("Migration successful." )
842
+
843
+ return nil
844
+ }
845
+
766
846
// Export data to a varity of file types
767
847
func exportToFile (fileName string ) error {
768
848
0 commit comments