Skip to content

Commit

Permalink
feature: batch deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
mrinjamul committed Sep 28, 2020
1 parent 3947217 commit 0991ddc
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sudo mv gpassmanager /usr/bin

## Usage

Simple Password Manager Application version 0.4.0
Simple Password Manager Application version 0.4.1

Usage:
gpassmanager [command]
Expand Down
101 changes: 57 additions & 44 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,31 @@ var removeCmd = &cobra.Command{
func removeRun(cmd *cobra.Command, args []string) {
if len(args) == 0 {
color.Red("Error: too short argument")
color.Yellow("Usage: gpassmanager remove [id]")
color.Yellow("Usage: gpassmanager remove [id] [id] ...")
os.Exit(0)
}
if len(args) > 1 {
color.Red("Error: too much arguments")
color.Yellow("Usage: gpassmanager remove [id]")
os.Exit(0)
}
i, err := strconv.Atoi(args[0])

if err != nil {
color.Red(args[0] + " is not a valid id\ninvalid syntax")
os.Exit(0)
// if len(args) > 1 {
// color.Red("Error: too much arguments")
// color.Yellow("Usage: gpassmanager remove [id]")
// os.Exit(0)
// }

var rmList = []int{}

for arg := 0; arg < len(args); arg++ {
i, err := strconv.Atoi(args[arg])

if err != nil {
color.Red(args[0] + " is not a valid id\ninvalid syntax")
os.Exit(0)
}
rmList = append(rmList, i)
}

rmList = gpm.SortSlice(rmList)
rmList = gpm.RemoveDuplicate(rmList)

if _, err := os.Stat(gpm.DatabaseFile); os.IsNotExist(err) {
gpm.CreateDatabase()
}
Expand Down Expand Up @@ -85,46 +96,48 @@ func removeRun(cmd *cobra.Command, args []string) {
fmt.Println("No passwords found !")
os.Exit(0)
}
if i > 0 && i <= len(accounts) {
colorFmt := color.New(color.FgRed, color.Bold)
var response string
var accountName string = ""
username := accounts[i-1].UserName
if accounts[i-1].AccountName != "" {
accountName = " (" + accounts[i-1].AccountName + ")"
}
colorFmt.Print("Do you want to remove " + username + accountName + " (y/n) : ")
fmt.Scanln(&response)
switch strings.ToLower(response) {
case "y", "yes":
accounts = gpm.RemoveAccount(accounts, i-1)
color.Yellow("[" + strconv.Itoa(i) + "] " + username + " has been removed")
if len(accounts) != 0 {
gpm.SavePasswords(bytePassword, accounts)
} else {
res := gpm.ConfirmPrompt("All password removed!\nDo you want to remove the master key ?")
if res {
err := gpm.CreateDatabase()
if err != nil {
fmt.Println(err)
}
} else {

for _, i := range rmList {
if i > 0 && i <= len(accounts) {
colorFmt := color.New(color.FgRed, color.Bold)
var response string
var accountName string = ""
username := accounts[i-1].UserName
if accounts[i-1].AccountName != "" {
accountName = " (" + accounts[i-1].AccountName + ")"
}
colorFmt.Print("Do you want to remove " + username + accountName + " (y/n) : ")
fmt.Scanln(&response)
switch strings.ToLower(response) {
case "y", "yes":
accounts = gpm.RemoveAccount(accounts, i-1)
color.Yellow("[" + strconv.Itoa(i) + "] " + username + " has been removed")
if len(accounts) != 0 {
gpm.SavePasswords(bytePassword, accounts)
} else {
res := gpm.ConfirmPrompt("All password removed!\nDo you want to remove the master key ?")
if res {
err := gpm.CreateDatabase()
if err != nil {
fmt.Println(err)
}
} else {
gpm.SavePasswords(bytePassword, accounts)
}

}

case "n", "no":
colorFmt = color.New(color.FgGreen)
colorFmt.Println("Operation Canceled")
default:
colorFmt = color.New(color.FgGreen)
colorFmt.Println("Operation Canceled")
}

case "n", "no":
colorFmt = color.New(color.FgGreen)
colorFmt.Println("Operation Canceled")
default:
colorFmt = color.New(color.FgGreen)
colorFmt.Println("Operation Canceled")
} else {
color.Red("[" + strconv.Itoa(i) + "] does not match any Account !")
}
} else {
color.Red("[" + strconv.Itoa(i) + "] does not match any Account !")
}

}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion docs/_includes/03-body.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Get Started

Simple Password Manager Application version 0.4.0
Simple Password Manager Application version 0.4.1

Usage:
gpassmanager [command]
Expand Down
26 changes: 25 additions & 1 deletion gpm/gpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"log"
mathrand "math/rand"
"os"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -58,7 +59,7 @@ func GetHomeDir() string {

// GetVersion returns version name, and code
func GetVersion() string {
var version = "0.4.0"
var version = "0.4.1"
return version
}

Expand Down Expand Up @@ -266,3 +267,26 @@ func GeneratePassword(length int) string {

return password
}

// SortSlice sort arrays
func SortSlice(slice []int) []int {
sort.Slice(slice, func(i, j int) bool { return slice[i] > slice[j] })
return slice
}

// RemoveDuplicate removes duplicate from slice
func RemoveDuplicate(slice []int) []int {
keys := make(map[int]bool)
list := []int{}

// If the key(values of the slice) is not equal
// to the already present value in new slice (list)
// then we append it. else we jump on another element.
for _, entry := range slice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
31 changes: 31 additions & 0 deletions gpm/gpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,34 @@ func TestGeneratePassword(t *testing.T) {
}
}
}

// TestSortSlice sort arrays
func TestSortSlice(t *testing.T) {
slice := []int{4, 2, 8, 3, 8, 5, 2, 1}
slice = SortSlice(slice)
want := []int{8, 8, 5, 4, 3, 2, 2, 1}
if len(slice) != len(want) {
t.Errorf("Both result is not same")
}
for i := range slice {
if slice[i] != want[i] {
t.Errorf("Want %d but got %d", want[i], slice[i])
}
}
}

// TestRemoveDuplicate removes duplicate from slice
func TestRemoveDuplicate(t *testing.T) {
slice := []int{4, 2, 8, 3, 8, 5, 2, 1}
slice = SortSlice(slice)
slice = RemoveDuplicate(slice)
want := []int{8, 5, 4, 3, 2, 1}
if len(slice) != len(want) {
t.Errorf("Both result is not same")
}
for i := range want {
if slice[i] != want[i] {
t.Errorf("Want %d but got %d", want[i], slice[i])
}
}
}

0 comments on commit 0991ddc

Please sign in to comment.