-
Notifications
You must be signed in to change notification settings - Fork 24
/
remove.go
51 lines (44 loc) · 1.14 KB
/
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
package collaborators
import (
"errors"
"github.com/Scalingo/cli/config"
"github.com/Scalingo/cli/io"
"github.com/Scalingo/go-scalingo"
"gopkg.in/errgo.v1"
)
var (
notFound = errors.New("collaborator not found")
)
func Remove(app, email string) error {
c, err := config.ScalingoClient()
if err != nil {
return errgo.Notef(err, "fail to get Scalingo client")
}
collaborator, err := getFromEmail(c, app, email)
if err != nil {
if err == notFound {
io.Error(email + " is not a collaborator of " + app + ".")
return nil
} else {
return errgo.Mask(err, errgo.Any)
}
}
err = c.CollaboratorRemove(app, collaborator.ID)
if err != nil {
return errgo.Mask(err, errgo.Any)
}
io.Status(email, "has been removed from the collaborators of", app)
return nil
}
func getFromEmail(c *scalingo.Client, app, email string) (scalingo.Collaborator, error) {
collaborators, err := c.CollaboratorsList(app)
if err != nil {
return scalingo.Collaborator{}, errgo.Mask(err, errgo.Any)
}
for _, collaborator := range collaborators {
if collaborator.Email == email {
return collaborator, nil
}
}
return scalingo.Collaborator{}, notFound
}