Skip to content

Commit 61b95cc

Browse files
author
Anand
committed
ref issue #21 - Removal also accepts a range of ids
1 parent 4fc13f6 commit 61b95cc

File tree

1 file changed

+58
-16
lines changed

1 file changed

+58
-16
lines changed

actions.go

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func addNewEntry() error {
248248
err, passwd = generateStrongPassword()
249249
fmt.Printf("done")
250250
}
251-
// fmt.Printf("Password => %s\n", passwd)
251+
// fmt.Printf("Password => %s\n", passwd)
252252

253253
notes = readInput(reader, "\nNotes")
254254

@@ -405,7 +405,7 @@ func editCurrentEntry(idString string) error {
405405
fmt.Printf("\nGenerating new password ...")
406406
err, passwd = generateStrongPassword()
407407
}
408-
// fmt.Printf("Password => %s\n", passwd)
408+
// fmt.Printf("Password => %s\n", passwd)
409409

410410
fmt.Printf("\nCurrent Notes: %s\n", entry.Notes)
411411
notes = readInput(reader, "New Notes")
@@ -434,7 +434,7 @@ func listCurrentEntry(idString string) error {
434434

435435
id, _ = strconv.Atoi(idString)
436436

437-
// fmt.Printf("Listing current entry - %d\n", id)
437+
// fmt.Printf("Listing current entry - %d\n", id)
438438
err, entry = getEntryById(id)
439439
if err != nil || entry == nil {
440440
fmt.Printf("No entry found for id %d\n", id)
@@ -444,7 +444,7 @@ func listCurrentEntry(idString string) error {
444444
err = printEntry(entry, true)
445445

446446
if err == nil && settingsRider.CopyPassword {
447-
// fmt.Printf("Copying password " + entry.Password + " to clipboard\n")
447+
// fmt.Printf("Copying password " + entry.Password + " to clipboard\n")
448448
copyPasswordToClipboard(entry.Password)
449449
}
450450

@@ -547,17 +547,51 @@ func findCurrentEntry(term string) error {
547547
return err
548548
}
549549

550+
// Remove a range of entries <id1>-<id2> say 10-14
551+
func removeMultipleEntries(idRangeEntry string) error {
552+
553+
var err error
554+
var idRange []string
555+
var id1, id2 int
556+
557+
idRange = strings.Split(idRangeEntry, "-")
558+
559+
if len(idRange) != 2 {
560+
fmt.Println("Invalid id range - " + idRangeEntry)
561+
return errors.New("Invalid id range - " + idRangeEntry)
562+
}
563+
564+
id1, _ = strconv.Atoi(idRange[0])
565+
id2, _ = strconv.Atoi(idRange[1])
566+
567+
if id1 >= id2 {
568+
fmt.Println("Invalid id range - " + idRangeEntry)
569+
return errors.New("Invalid id range - " + idRangeEntry)
570+
}
571+
572+
for idNum := id1; idNum <= id2; idNum++ {
573+
err = removeCurrentEntry(fmt.Sprintf("%d", idNum))
574+
}
575+
576+
return err
577+
}
578+
550579
// Remove current entry by id
551580
func removeCurrentEntry(idString string) error {
552581

553582
var err error
554583
var entry *Entry
555584
var id int
585+
var response string
556586

557587
if err = checkActiveDatabase(); err != nil {
558588
return err
559589
}
560590

591+
if strings.Contains(idString, "-") {
592+
return removeMultipleEntries(idString)
593+
}
594+
561595
id, _ = strconv.Atoi(idString)
562596

563597
err, entry = getEntryById(id)
@@ -566,10 +600,18 @@ func removeCurrentEntry(idString string) error {
566600
return err
567601
}
568602

569-
// Drop from the database
570-
err = removeDatabaseEntry(entry)
571-
if err == nil {
572-
fmt.Printf("Entry with id %d was removed from the database\n", id)
603+
printEntryMinimal(entry, true)
604+
605+
response = readInput(bufio.NewReader(os.Stdin), "Please confirm removal [Y/n]: ")
606+
607+
if strings.ToLower(response) != "n" {
608+
// Drop from the database
609+
err = removeDatabaseEntry(entry)
610+
if err == nil {
611+
fmt.Printf("Entry with id %d was removed from the database\n", id)
612+
}
613+
} else {
614+
fmt.Println("Removal of entry canceled by user.")
573615
}
574616

575617
return err
@@ -655,7 +697,7 @@ func encryptDatabase(dbPath string, givenPasswd *string) error {
655697
}
656698
}
657699

658-
// err = encryptFileAES(dbPath, passwd)
700+
// err = encryptFileAES(dbPath, passwd)
659701
_, settings := getOrCreateLocalConfig(APP)
660702

661703
switch settings.Cipher {
@@ -796,7 +838,7 @@ func exportToMarkdown(fileName string) error {
796838
}
797839
}
798840

799-
// fmt.Printf("%+v\n", maxLengths)
841+
// fmt.Printf("%+v\n", maxLengths)
800842
fh, err = os.Create(fileName)
801843
if err != nil {
802844
fmt.Printf("Cannt open \"%s\" for writing - \"%s\"\n", fileName, err.Error())
@@ -810,7 +852,7 @@ func exportToMarkdown(fileName string) error {
810852
// Write markdown header
811853
for idx, length := range maxLengths {
812854
delta := length - len(headers[idx])
813-
// fmt.Printf("%d\n", delta)
855+
// fmt.Printf("%d\n", delta)
814856
if delta > 0 {
815857
for i := 0; i < delta+2; i++ {
816858
headers[idx] += " "
@@ -872,7 +914,7 @@ func exportToPDF(fileName string) error {
872914
}
873915

874916
tmpFile = randomFileName(os.TempDir(), ".tmp")
875-
// fmt.Printf("Temp file => %s\n", tmpFile)
917+
// fmt.Printf("Temp file => %s\n", tmpFile)
876918
err = exportToMarkdownLimited(tmpFile)
877919

878920
if err == nil {
@@ -889,7 +931,7 @@ func exportToPDF(fileName string) error {
889931

890932
if pdfTkFound && len(passwd) > 0 {
891933
tmpFile = randomFileName(".", ".pdf")
892-
// fmt.Printf("pdf file => %s\n", tmpFile)
934+
// fmt.Printf("pdf file => %s\n", tmpFile)
893935
args = []string{fileName, "output", tmpFile, "user_pw", passwd}
894936
cmd = exec.Command("pdftk", args...)
895937
_, err = cmd.Output()
@@ -935,7 +977,7 @@ func exportToMarkdownLimited(fileName string) error {
935977
}
936978
}
937979

938-
// fmt.Printf("%+v\n", maxLengths)
980+
// fmt.Printf("%+v\n", maxLengths)
939981
fh, err = os.Create(fileName)
940982
if err != nil {
941983
fmt.Printf("Cannt open \"%s\" for writing - \"%s\"\n", fileName, err.Error())
@@ -949,7 +991,7 @@ func exportToMarkdownLimited(fileName string) error {
949991
// Write markdown header
950992
for idx, length := range maxLengths {
951993
delta := length - len(headers[idx])
952-
// fmt.Printf("%d\n", delta)
994+
// fmt.Printf("%d\n", delta)
953995
if delta > 0 {
954996
for i := 0; i < delta+2; i++ {
955997
headers[idx] += " "
@@ -1000,7 +1042,7 @@ func exportToHTML(fileName string) error {
10001042
return err
10011043
}
10021044

1003-
// fmt.Printf("%+v\n", maxLengths)
1045+
// fmt.Printf("%+v\n", maxLengths)
10041046
fh, err = os.Create(fileName)
10051047
if err != nil {
10061048
fmt.Printf("Cannt open \"%s\" for writing - \"%s\"\n", fileName, err.Error())

0 commit comments

Comments
 (0)