annotate cmds/multipass-remove/remove.go @ 18:00d30c67b56d

Put all the library stuff into multipass/file.
author Paul Fisher <paul@pfish.zone>
date Sun, 01 Nov 2015 12:16:51 -0500
parents 342f63116bfd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 // multipass-remove allows a user to remove an entry from their multipass database.
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
3 package main
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
4
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
5 import (
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
6 "bufio"
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7 "fmt"
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8 "os"
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9 "strconv"
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 "pfish.zone/go/multipass/file"
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12 )
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 func main() {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 passfile, err := file.ForMe()
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 if err != nil {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 fmt.Println(err.Error())
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 os.Exit(1)
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 }
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 fmt.Println("Choose a password to remove:")
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 reader := bufio.NewReader(os.Stdin)
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22 entries, err := passfile.AllEntries()
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 for i, entry := range entries {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 fmt.Printf("%2d: %s\n", i+1, entry.Description())
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 }
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 fmt.Print("Enter the number to remove: ")
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27 text, err := reader.ReadString('\n')
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28 if err != nil {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29 fmt.Println(err.Error())
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
30 os.Exit(1)
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
31 }
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
32 picked, err := strconv.Atoi(text[:len(text)-1])
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33 if err != nil {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 fmt.Println("Not a valid number")
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
35 os.Exit(1)
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
36 }
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
37 picked -= 1
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
38 if picked < 0 || len(entries) <= picked {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
39 fmt.Println("Not a valid selection")
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
40 os.Exit(1)
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
41 }
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
42
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 if err := passfile.Remove(entries[picked].ID()); err != nil {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
44 fmt.Printf("Couldn't remove password: %s\n", err.Error())
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
45 os.Exit(1)
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 }
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 fmt.Println("Removed password entry.")
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 }