This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
users.go
133 lines (112 loc) · 4.06 KB
/
users.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package pi
import (
"fmt"
"net/http"
)
type usersCommand struct {
Create createUserCommand `description:"create User" command:"create" subcommands-optional:"true"`
Update updateUserCommand `description:"update User Token" command:"update" subcommands-optional:"true"`
Delete deleteUserCommand `description:"delete User" command:"delete" subcommands-optional:"true"`
}
type createUserCommand struct {
Token string `short:"t" long:"token" description:"A token string used to authenticate as a user to be created." required:"true"`
Username string `short:"u" long:"username" description:"User name to be created." required:"true"`
AgreeTermsOfService string `short:"a" long:"agree-terms-of-service" description:"Specify yes or no whether you agree to the terms of service." choice:"yes" choice:"no" required:"true"`
NotMinor string `short:"m" long:"not-minor" description:"Specify yes or no as to whether you are not a minor or if you are a minor and you have the parental consent of using pixela." choice:"yes" choice:"no" required:"true"`
ThanksCode string `short:"c" long:"thanks-code" description:"Like a registration code obtained when you register for Patreon support. For detail, see https://github.com/a-know/Pixela/wiki/How-to-support-Pixela-by-Patreon-%EF%BC%8F-Use-Limited-Features"`
}
type createUserParams struct {
Token string `json:"token"`
Username string `json:"username"`
AgreeTerms string `json:"agreeTermsOfService"`
NotMinor string `json:"notMinor"`
ThanksCode string `json:"thanksCode"`
}
type updateUserCommand struct {
Username string `short:"u" long:"username" description:"User name to be updated."`
NewToken string `short:"t" long:"new-token" description:"A new authentication token for update." required:"true"`
ThanksCode string `short:"c" long:"thanks-code" description:"Like a registration code obtained when you register for Patreon support. For detail, see https://github.com/a-know/Pixela/wiki/How-to-support-Pixela-by-Patreon-%EF%BC%8F-Use-Limited-Features"`
}
type updateUserParams struct {
NewToken string `json:"newToken,omitempty"`
ThanksCode string `json:"thanksCode,omitempty"`
}
type deleteUserCommand struct {
Username string `short:"u" long:"username" description:"User name to be deleted."`
}
func (cC *createUserCommand) Execute(args []string) error {
req, err := generateCreateUserRequest(cC)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateCreateUserRequest(cC *createUserCommand) (*http.Request, error) {
paramStruct := &createUserParams{
Token: cC.Token,
Username: cC.Username,
AgreeTerms: cC.AgreeTermsOfService,
NotMinor: cC.NotMinor,
ThanksCode: cC.ThanksCode,
}
req, err := generateRequest(
"POST",
"v1/users",
paramStruct,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate create api request : %s", err)
}
return req, nil
}
func (uC *updateUserCommand) Execute(args []string) error {
req, err := generateUpdateUserRequest(uC)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateUpdateUserRequest(uC *updateUserCommand) (*http.Request, error) {
username, err := getUsername(uC.Username)
if err != nil {
return nil, err
}
paramStruct := &updateUserParams{
NewToken: uC.NewToken,
ThanksCode: uC.ThanksCode,
}
req, err := generateRequestWithToken(
"PUT",
fmt.Sprintf("v1/users/%s", username),
paramStruct,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate update api request : %s", err)
}
return req, nil
}
func (dC *deleteUserCommand) Execute(args []string) error {
req, err := generateDeleteUserRequest(dC)
if err != nil {
return err
}
err = doRequest(req)
return err
}
func generateDeleteUserRequest(dC *deleteUserCommand) (*http.Request, error) {
username, err := getUsername(dC.Username)
if err != nil {
return nil, err
}
req, err := generateRequestWithToken(
"DELETE",
fmt.Sprintf("v1/users/%s", username),
nil,
)
if err != nil {
return nil, fmt.Errorf("Failed to generate delete api request : %s", err)
}
return req, nil
}