-
Notifications
You must be signed in to change notification settings - Fork 3
/
create.go
67 lines (60 loc) · 1.29 KB
/
create.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
package main
import (
"errors"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
type createUserReq struct {
Username *string `json:"username"`
Password *string `json:"password"`
}
func (r *createUserReq) validate() error {
if r.Username == nil {
return errors.New(`username field is required`)
} else if r.Password == nil {
return errors.New("password field is required")
}
return nil
}
func cleanCreateUserOutput(stdout string) string {
lines := strings.Split(stdout, "\n")
if len(lines) < 4 {
return stdout
}
return strings.Join(lines[3:], "\n")
}
func createUserHandler(c *gin.Context) {
req := createUserReq{}
if err := c.ShouldBind(&req); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, response{
Error: true,
Message: err.Error(),
})
return
}
if err := req.validate(); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, response{
Error: true,
Message: err.Error(),
})
return
}
cred := getCredential(c)
result, err := execGSQL(
*cred.Username,
*cred.Password,
nil,
"create user",
strings.Join([]string{
*req.Username,
*req.Password,
*req.Password,
}, "\n")+"\n")
result = cleanCreateUserOutput(result)
_, err = processOutput(result, err)
c.JSON(200, response{
Error: err != nil,
Message: result,
})
}