-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete.go
81 lines (66 loc) · 1.53 KB
/
delete.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
79
80
81
package account
import (
"context"
"errors"
"fmt"
"log"
"github.com/netsoc/cli/pkg/config"
"github.com/netsoc/cli/pkg/util"
iam "github.com/netsoc/iam/client"
"github.com/spf13/cobra"
)
type deleteOptions struct {
Config func() (*config.Config, error)
IAMClient func() (*iam.APIClient, error)
NoConfirm bool
User string
}
// NewCmdDelete creates a new account delete command
func NewCmdDelete(f *util.CmdFactory) *cobra.Command {
opts := deleteOptions{
Config: f.Config,
IAMClient: f.IAMClient,
}
cmd := &cobra.Command{
Use: "delete",
Short: "Delete account",
RunE: func(cmd *cobra.Command, args []string) error {
return deleteRun(opts)
},
}
cmd.Flags().BoolVar(&opts.NoConfirm, "yes", false, "don't ask for confirmation")
util.AddOptUser(cmd, &opts.User)
return cmd
}
func deleteRun(opts deleteOptions) error {
c, err := opts.Config()
if err != nil {
return err
}
if c.Token == "" {
return errors.New("not logged in")
}
prompt := "Really delete yourself?"
if opts.User != "self" {
prompt = fmt.Sprintf("Really delete user %v?", opts.User)
}
if !opts.NoConfirm {
shouldDelete, err := util.YesNo(prompt, false)
if err != nil {
return err
}
if !shouldDelete {
return nil
}
}
client, err := opts.IAMClient()
if err != nil {
return err
}
ctx := context.WithValue(context.Background(), iam.ContextAccessToken, c.Token)
if _, _, err := client.UsersApi.DeleteUser(ctx, opts.User); err != nil {
return util.APIError(err)
}
log.Println("Deleted successfully")
return nil
}