This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
credential_remove.go
78 lines (64 loc) · 1.71 KB
/
credential_remove.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/cnabio/duffle/pkg/duffle/home"
)
type credentialRemoveCmd struct {
names []string
home home.Home
out io.Writer
}
func newCredentialRemoveCmd(w io.Writer) *cobra.Command {
rm := &credentialRemoveCmd{out: w}
cmd := &cobra.Command{
Use: "remove [NAME]",
Short: "remove one or more credential set",
Aliases: []string{"rm"},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("This command requires at least 1 argument: name of credential set")
}
rm.names = args
rm.home = home.Home(homePath())
return rm.run()
},
}
return cmd
}
func (rm *credentialRemoveCmd) run() error {
var removeErrors []string
var notFound []string
credentialSets := findCredentialSets(rm.home.Credentials())
// Put this in a map to minimize lookup time.
pathMap := map[string]string{}
for _, cred := range credentialSets {
pathMap[cred.name] = cred.path
}
for _, name := range rm.names {
if path, ok := pathMap[name]; ok {
if err := removeCredentialSet(path); err != nil {
removeErrors = append(removeErrors, fmt.Sprintf("Failed to remove credential set %s: %v", name, err))
} else {
fmt.Fprintf(rm.out, "Removed credential set: %s\n", name)
}
} else {
notFound = append(notFound, name)
}
}
if len(notFound) > 0 {
notFoundError := fmt.Sprintf("Unable to find credential set(s): %v", strings.Join(notFound, ", "))
removeErrors = append(removeErrors, notFoundError)
}
if len(removeErrors) > 0 {
return fmt.Errorf(strings.Join(removeErrors, "\n"))
}
return nil
}
func removeCredentialSet(path string) error {
return os.Remove(path)
}