-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (92 loc) · 2.81 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"log"
"os"
"github.com/beevee/portier/yandex"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "portier"
app.HelpName = "portier"
app.Usage = "Provides convenience functions for corporate Yandex.Taxi accounts"
app.Version = "1.0.0"
app.Commands = []cli.Command{
{
Name: "users",
Aliases: []string{"u"},
Usage: "Enables or disables orders from application for user role",
Flags: []cli.Flag{
cli.StringFlag{
Name: "role",
Usage: "operate on users in this role only",
Value: "Кирпичников", // this is to prevent accidental runs of batch operations on all users
},
},
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
return cli.NewExitError("please specify: enable or disable orders", 1)
}
api := &yandex.API{}
log.Print("initializing Yandex API\n")
err := api.Init(c.Parent().String("sessionid"), c.Parent().String("clientid"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to init Yandex API: %s\n", err), 1)
}
users, err := api.GetUsersByRole(c.String("role"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to fetch users with role %s: %s\n", c.String("role"), err), 1)
}
log.Printf("fetched %d users with role %s\n", len(users), c.String("role"))
for _, user := range users {
switch c.Args().First() {
case "enable":
log.Printf("enabling user %s (%s)\n", user.ID, user.Name)
if !user.IsActive {
if err := api.EnableUser(user); err != nil {
log.Printf("failed to enable user %s (%s): %s\n", user.ID, user.Name, err)
} else {
log.Printf("successfully enabled user %s (%s)\n", user.ID, user.Name)
}
} else {
log.Printf("user %s (%s) is already active\n", user.ID, user.Name)
}
case "disable":
log.Printf("disabling user %s (%s)\n", user.ID, user.Name)
if user.IsActive {
if err := api.DisableUser(user); err != nil {
log.Printf("failed to disable user %s (%s): %s\n", user.ID, user.Name, err)
} else {
log.Printf("successfully disabled user %s (%s)\n", user.ID, user.Name)
}
} else {
log.Printf("user %s (%s) is already inactive\n", user.ID, user.Name)
}
default:
return cli.NewExitError(fmt.Sprintf("we can only enable and disable, and you said: %s\n", c.Args().First()), 1)
}
}
return nil
},
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "sessionid, s",
Usage: "value of Yandex Session_id cookie",
EnvVar: "SESSION_ID",
Required: true,
},
cli.StringFlag{
Name: "clientid, c",
Usage: "Yandex client id",
EnvVar: "CLIENT_ID",
Required: true,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}