-
Notifications
You must be signed in to change notification settings - Fork 2
/
create.go
167 lines (153 loc) · 4.08 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package serviceusers
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/appgate/sdp-api-client-go/api/v20/openapi"
"github.com/appgate/sdpctl/pkg/docs"
"github.com/appgate/sdpctl/pkg/factory"
"github.com/appgate/sdpctl/pkg/filesystem"
"github.com/appgate/sdpctl/pkg/prompt"
"github.com/appgate/sdpctl/pkg/util"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
)
func NewServiceUsersCreateCMD(f *factory.Factory) *cobra.Command {
opts := ServiceUsersOptions{
Config: f.Config,
API: f.ServiceUsers,
In: f.Stdin,
Out: f.IOOutWriter,
}
cmd := &cobra.Command{
Use: "create",
Short: docs.ServiceUsersCreate.Short,
Long: docs.ServiceUsersCreate.Long,
Example: docs.ServiceUsersCreate.ExampleString(),
RunE: func(cmd *cobra.Command, args []string) error {
return serviceUserCreateRun(cmd, args, opts)
},
}
flags := cmd.Flags()
flags.String("name", "", "name for service user")
flags.StringSlice("tags", []string{}, "tags for service user")
flags.StringP("from-file", "f", "", "create a user from a valid json file")
return cmd
}
func serviceUserCreateRun(cmd *cobra.Command, args []string, opts ServiceUsersOptions) error {
ctx := context.Background()
api, err := opts.API(opts.Config)
if err != nil {
return err
}
fromFile, err := cmd.Flags().GetString("from-file")
if err != nil {
return err
}
users := []openapi.ServiceUsersGetRequest{}
if len(fromFile) > 0 {
path := filesystem.AbsolutePath(fromFile)
ok, err := util.FileExists(path)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("File not found: %s", path)
}
file, err := os.ReadFile(path)
if err != nil {
return err
}
// Try unmarshal json array first
dtoArray := ServiceUserArrayDTO{}
if err := json.Unmarshal(file, &dtoArray); err != nil {
// if the file is a single user object, unmarshal that instead
singleDTO := ServiceUserDTO{}
if err := json.Unmarshal(file, &singleDTO); err == nil {
dtoArray = append(dtoArray, singleDTO)
}
}
for i := 0; i < len(dtoArray); i++ {
u := openapi.ServiceUsersGetRequest{
Name: dtoArray[i].Name,
Password: dtoArray[i].Password,
Disabled: openapi.PtrBool(dtoArray[i].Disabled),
Labels: &dtoArray[i].Labels,
Notes: openapi.PtrString(dtoArray[i].Notes),
Tags: dtoArray[i].Tags,
}
users = append(users, u)
}
} else {
username, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
var password string
var hasStdin bool
stat, err := os.Stdin.Stat()
if err == nil && (stat.Mode()&os.ModeCharDevice) == 0 {
hasStdin = true
}
if hasStdin {
buf, err := io.ReadAll(opts.In)
if err != nil {
return fmt.Errorf("Failed to read from stdin: %w", err)
}
password = strings.TrimSuffix(string(buf), "\n")
}
noInteractive, err := cmd.Flags().GetBool("no-interactive")
if err != nil {
return err
}
if !noInteractive {
if len(username) <= 0 {
qs := &survey.Input{
Message: "Name for service user:",
}
if err := prompt.SurveyAskOne(qs, &username); err != nil {
return err
}
}
if len(password) <= 0 {
password, err = prompt.PasswordConfirmation("Passphrase for service user:")
if err != nil {
return err
}
}
}
var errs *multierror.Error
if len(username) <= 0 {
errs = multierror.Append(errs, errors.New("name is required"))
}
if len(password) <= 0 {
errs = multierror.Append(errs, errors.New("passphrase is required"))
}
if errs != nil {
errs = multierror.Append(errs, errors.New("failed to create user: missing data"))
return errs.ErrorOrNil()
}
users = append(users, openapi.ServiceUsersGetRequest{
Name: username,
Password: password,
})
}
if len(users) <= 0 {
return fmt.Errorf("Failed to create user(s): no user data provided")
}
var errs *multierror.Error
for _, u := range users {
created, err := api.Create(ctx, u)
if err != nil {
errs = multierror.Append(errs, err)
continue
}
util.PrintJSON(opts.Out, created)
}
return errs.ErrorOrNil()
}