annotate multipass-remove.go @ 13:da6c493cf08a

Move Unix-specific user auth files into unix_users.go.
author Paul Fisher <paul@pfish.zone>
date Thu, 29 Oct 2015 21:29:27 -0400
parents e246c8a4d28e
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 }