/
user_create.go
128 lines (103 loc) · 3.52 KB
/
user_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
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
// Copyright 2022 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/superproj/onex.
//
package user
import (
"context"
"fmt"
"github.com/spf13/cobra"
cmdutil "github.com/superproj/onex/internal/onexctl/cmd/util"
"github.com/superproj/onex/internal/onexctl/util/templates"
v1 "github.com/superproj/onex/pkg/api/usercenter/v1"
"github.com/superproj/onex/pkg/cli/genericclioptions"
)
const (
createUsageStr = "create USERNAME PASSWORD EMAIL"
)
// CreateOptions is an options struct to support create subcommands.
type CreateOptions struct {
Email string
Nickname string
Phone string
CreateUserRequest *v1.CreateUserRequest
client v1.UserCenterHTTPClient
genericclioptions.IOStreams
}
var (
createLong = templates.LongDesc(`Create a user on onex cloud platform.
If nickname not specified, username will be used.`)
createExample = templates.Examples(`
# Create user with given input
onexctl user create foo Foo@2023
# Create user wt
onexctl user create foo Foo@2023 --email=foo@foxmail.com --phone=18128845xxx --nickname=colin`)
createUsageErrStr = fmt.Sprintf(
"expected '%s'.\nUSERNAME PASSWORD are required arguments for the create command",
createUsageStr,
)
)
// NewCreateOptions returns an initialized CreateOptions instance.
func NewCreateOptions(ioStreams genericclioptions.IOStreams) *CreateOptions {
return &CreateOptions{
Email: "colin@onex.com",
Phone: "1812884xxxx",
IOStreams: ioStreams,
}
}
// NewCmdCreate returns new initialized instance of create sub command.
func NewCmdCreate(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
o := NewCreateOptions(ioStreams)
cmd := &cobra.Command{
Use: createUsageStr,
DisableFlagsInUseLine: true,
Aliases: []string{},
Short: "Create a user resource",
TraverseChildren: true,
Long: createLong,
Example: createExample,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(f, cmd, args))
cmdutil.CheckErr(o.Validate(cmd, args))
cmdutil.CheckErr(o.Run(args))
},
SuggestFor: []string{},
}
// mark flag as deprecated
cmd.Flags().StringVar(&o.Email, "email", o.Email, "The email of the user.")
cmd.Flags().StringVar(&o.Nickname, "nickname", o.Nickname, "The nickname of the user.")
cmd.Flags().StringVar(&o.Phone, "phone", o.Phone, "The phone number of the user.")
return cmd
}
// Complete completes all the required options.
func (o *CreateOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmdutil.UsageErrorf(cmd, createUsageErrStr)
}
if o.Nickname == "" {
o.Nickname = args[0]
}
o.CreateUserRequest = &v1.CreateUserRequest{
Username: args[0],
Nickname: o.Nickname,
Password: args[1],
Email: o.Email,
Phone: o.Phone,
}
o.client = f.UserCenterClient()
return nil
}
// Validate makes sure there is no discrepency in command options.
func (o *CreateOptions) Validate(cmd *cobra.Command, args []string) error {
return o.CreateUserRequest.Validate()
}
// Run executes a create subcommand using the specified options.
func (o *CreateOptions) Run(args []string) error {
_, err := o.client.CreateUser(context.Background(), o.CreateUserRequest)
if err != nil {
return err
}
fmt.Fprintf(o.Out, "user/%s created\n", o.CreateUserRequest.Username)
return nil
}