/
user_get.go
60 lines (50 loc) · 1.25 KB
/
user_get.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
/*
Author: lkong
Description: test cmd tool
*/
package cmd
import (
"fmt"
"io"
"grapehttp/client/cmd/templates"
cmdutil "grapehttp/client/cmd/util"
"grapehttp/pkg/i18n"
"github.com/spf13/cobra"
)
var (
getExample = templates.Examples(i18n.T(`
# Get lkong user informations
fctl get lkong`))
)
func NewCmdUserGet(f cmdutil.Factory, out io.Writer, cmdErr io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "get USERNAME",
Short: i18n.T("Get user informations"),
Long: "Get user informations",
Example: getExample,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmdutil.CheckErr(cmdutil.UsageErrorf(cmd, "Unexpected args: %v", args))
}
cmdutil.CheckErr(RunUserGet(f, out, cmdErr, cmd, args))
return
},
Aliases: []string{},
}
return cmd
}
func RunUserGet(f cmdutil.Factory, out io.Writer, cmdErr io.Writer, cmd *cobra.Command, args []string) error {
req := struct {
Username string `json:"username"`
}{args[0]}
request := f.Gorequest()
resp, body, errs := request.Get("http://"+f.Server+"/-/user/get").
Set("Authorization", "Basic "+f.Auth()).
Send(req).
End()
if err := cmdutil.CombineRequestErr(resp, body, errs); err != nil {
return err
}
fmt.Printf("%s\n", body)
return nil
}