annotate remove.go @ 19:58fe867c9ecf

Reorganize commands to more standard go layout. - Unify multipass user commands under one 'multipass' binary - Move multipass checkpassword command to mp-checkpassword.
author Paul Fisher <paul@pfish.zone>
date Sun, 01 Nov 2015 12:42:02 -0500
parents cmds/multipass-remove/remove.go@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
19
58fe867c9ecf Reorganize commands to more standard go layout.
Paul Fisher <paul@pfish.zone>
parents: 17
diff changeset
14 func remove(passfile *file.ShadowFile, input *bufio.Reader) int {
11
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 fmt.Println("Choose a password to remove:")
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 reader := bufio.NewReader(os.Stdin)
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 entries, err := passfile.AllEntries()
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 for i, entry := range entries {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 fmt.Printf("%2d: %s\n", i+1, entry.Description())
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 }
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 fmt.Print("Enter the number to remove: ")
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22 text, err := reader.ReadString('\n')
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 if err != nil {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 fmt.Println(err.Error())
19
58fe867c9ecf Reorganize commands to more standard go layout.
Paul Fisher <paul@pfish.zone>
parents: 17
diff changeset
25 return 1
11
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 }
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27 picked, err := strconv.Atoi(text[:len(text)-1])
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("Not a valid number")
19
58fe867c9ecf Reorganize commands to more standard go layout.
Paul Fisher <paul@pfish.zone>
parents: 17
diff changeset
30 return 1
11
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 -= 1
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33 if picked < 0 || len(entries) <= picked {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 fmt.Println("Not a valid selection")
19
58fe867c9ecf Reorganize commands to more standard go layout.
Paul Fisher <paul@pfish.zone>
parents: 17
diff changeset
35 return 1
11
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
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
38 if err := passfile.Remove(entries[picked].ID()); err != nil {
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
39 fmt.Printf("Couldn't remove password: %s\n", err.Error())
19
58fe867c9ecf Reorganize commands to more standard go layout.
Paul Fisher <paul@pfish.zone>
parents: 17
diff changeset
40 return 1
11
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 fmt.Println("Removed password entry.")
19
58fe867c9ecf Reorganize commands to more standard go layout.
Paul Fisher <paul@pfish.zone>
parents: 17
diff changeset
43 return 0
11
e246c8a4d28e Actually add the remove command.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
44 }