@@ -3,19 +3,20 @@ package secrets
3
3
import (
4
4
"errors"
5
5
"fmt"
6
- "io"
7
6
"io/ioutil"
8
7
9
8
"github.com/spf13/cobra"
10
9
11
- api "k8s.io/kubernetes/pkg/apis/ core"
12
- kcoreclient "k8s.io/kubernetes/pkg/ client/clientset_generated/internalclientset/ typed/core/internalversion "
10
+ corev1 "k8s.io/api/ core/v1 "
11
+ corev1client "k8s.io/client-go/kubernetes/ typed/core/v1 "
13
12
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
14
13
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
15
14
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
15
+ "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
16
16
kterm "k8s.io/kubernetes/pkg/kubectl/util/term"
17
17
18
18
"github.com/openshift/origin/pkg/cmd/util/term"
19
+ "github.com/openshift/origin/pkg/oc/util/ocscheme"
19
20
)
20
21
21
22
// CreateBasicAuthSecretRecommendedCommandName represents name of subcommand for `oc secrets` command
44
45
45
46
// CreateBasicAuthSecretOptions holds the credential needed to authenticate against SCM servers.
46
47
type CreateBasicAuthSecretOptions struct {
48
+ PrintFlags * genericclioptions.PrintFlags
49
+
50
+ Printer printers.ResourcePrinter
51
+
47
52
SecretName string
48
53
Username string
49
54
Password string
@@ -52,18 +57,21 @@ type CreateBasicAuthSecretOptions struct {
52
57
53
58
PromptForPassword bool
54
59
55
- Reader io.Reader
56
- Out io.Writer
60
+ SecretsInterface corev1client.SecretInterface
57
61
58
- SecretsInterface kcoreclient.SecretInterface
62
+ genericclioptions.IOStreams
63
+ }
64
+
65
+ func NewCreateBasicAuthSecretOptions (streams genericclioptions.IOStreams ) * CreateBasicAuthSecretOptions {
66
+ return & CreateBasicAuthSecretOptions {
67
+ PrintFlags : genericclioptions .NewPrintFlags ("created" ).WithTypeSetter (ocscheme .PrintingInternalScheme ),
68
+ IOStreams : streams ,
69
+ }
59
70
}
60
71
61
72
// NewCmdCreateBasicAuthSecret implements the OpenShift cli secrets new-basicauth subcommand
62
73
func NewCmdCreateBasicAuthSecret (name , fullName string , f kcmdutil.Factory , streams genericclioptions.IOStreams , newSecretFullName , ocEditFullName string ) * cobra.Command {
63
- o := & CreateBasicAuthSecretOptions {
64
- Out : streams .Out ,
65
- Reader : streams .In ,
66
- }
74
+ o := NewCreateBasicAuthSecretOptions (streams )
67
75
68
76
cmd := & cobra.Command {
69
77
Use : fmt .Sprintf ("%s SECRET --username=USERNAME --password=PASSWORD [--ca-cert=FILENAME] [--gitconfig=FILENAME]" , name ),
@@ -73,25 +81,9 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, stre
73
81
Deprecated : "use oc create secret" ,
74
82
Hidden : true ,
75
83
Run : func (c * cobra.Command , args []string ) {
76
- if err := o .Complete (f , args ); err != nil {
77
- kcmdutil .CheckErr (kcmdutil .UsageErrorf (c , err .Error ()))
78
- }
79
-
80
- if err := o .Validate (); err != nil {
81
- kcmdutil .CheckErr (kcmdutil .UsageErrorf (c , err .Error ()))
82
- }
83
-
84
- if len (kcmdutil .GetFlagString (c , "output" )) != 0 {
85
- secret , err := o .NewBasicAuthSecret ()
86
- kcmdutil .CheckErr (err )
87
-
88
- kcmdutil .CheckErr (kcmdutil .PrintObject (c , secret , streams .Out ))
89
- return
90
- }
91
-
92
- if err := o .CreateBasicAuthSecret (); err != nil {
93
- kcmdutil .CheckErr (err )
94
- }
84
+ kcmdutil .CheckErr (o .Complete (f , args ))
85
+ kcmdutil .CheckErr (o .Validate ())
86
+ kcmdutil .CheckErr (o .Run ())
95
87
},
96
88
}
97
89
@@ -103,13 +95,12 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, stre
103
95
cmd .MarkFlagFilename ("gitconfig" )
104
96
cmd .Flags ().BoolVarP (& o .PromptForPassword , "prompt" , "" , false , "If true, prompt for password or token" )
105
97
106
- kcmdutil .AddPrinterFlags (cmd )
107
-
98
+ o .PrintFlags .AddFlags (cmd )
108
99
return cmd
109
100
}
110
101
111
102
// CreateBasicAuthSecret saves created Secret structure and prints the secret name to the output on success.
112
- func (o * CreateBasicAuthSecretOptions ) CreateBasicAuthSecret () error {
103
+ func (o * CreateBasicAuthSecretOptions ) Run () error {
113
104
secret , err := o .NewBasicAuthSecret ()
114
105
if err != nil {
115
106
return err
@@ -119,16 +110,15 @@ func (o *CreateBasicAuthSecretOptions) CreateBasicAuthSecret() error {
119
110
return err
120
111
}
121
112
122
- fmt .Fprintf (o .GetOut (), "secret/%s\n " , secret .Name )
123
- return nil
113
+ return o .Printer .PrintObj (secret , o .Out )
124
114
}
125
115
126
116
// NewBasicAuthSecret builds up the Secret structure containing secret name, type and data structure
127
117
// containing desired credentials.
128
- func (o * CreateBasicAuthSecretOptions ) NewBasicAuthSecret () (* api .Secret , error ) {
129
- secret := & api .Secret {}
118
+ func (o * CreateBasicAuthSecretOptions ) NewBasicAuthSecret () (* corev1 .Secret , error ) {
119
+ secret := & corev1 .Secret {}
130
120
secret .Name = o .SecretName
131
- secret .Type = api .SecretTypeBasicAuth
121
+ secret .Type = corev1 .SecretTypeBasicAuth
132
122
secret .Data = map [string ][]byte {}
133
123
134
124
if len (o .Username ) != 0 {
@@ -164,32 +154,38 @@ func (o *CreateBasicAuthSecretOptions) Complete(f kcmdutil.Factory, args []strin
164
154
if len (args ) != 1 {
165
155
return errors .New ("must have exactly one argument: secret name" )
166
156
}
157
+
167
158
o .SecretName = args [0 ]
168
159
169
160
if o .PromptForPassword {
170
- if len (o .Password ) != 0 {
171
- return errors .New ("must provide either --prompt or --password flag" )
172
- }
173
- if ! kterm .IsTerminal (o .Reader ) {
161
+ if ! kterm .IsTerminal (o .In ) {
174
162
return errors .New ("provided reader is not a terminal" )
175
163
}
176
164
177
- o .Password = term .PromptForPasswordString (o .Reader , o .Out , "Password: " )
165
+ o .Password = term .PromptForPasswordString (o .In , o .Out , "Password: " )
178
166
if len (o .Password ) == 0 {
179
167
return errors .New ("password must be provided" )
180
168
}
181
169
}
182
170
183
- if f != nil {
184
- client , err := f .ClientSet ()
185
- if err != nil {
186
- return err
187
- }
188
- namespace , _ , err := f .ToRawKubeConfigLoader ().Namespace ()
189
- if err != nil {
190
- return err
191
- }
192
- o .SecretsInterface = client .Core ().Secrets (namespace )
171
+ config , err := f .ToRESTConfig ()
172
+ if err != nil {
173
+ return err
174
+ }
175
+
176
+ clientset , err := corev1client .NewForConfig (config )
177
+ if err != nil {
178
+ return err
179
+ }
180
+ namespace , _ , err := f .ToRawKubeConfigLoader ().Namespace ()
181
+ if err != nil {
182
+ return err
183
+ }
184
+ o .SecretsInterface = clientset .Secrets (namespace )
185
+
186
+ o .Printer , err = o .PrintFlags .ToPrinter ()
187
+ if err != nil {
188
+ return err
193
189
}
194
190
195
191
return nil
@@ -205,15 +201,9 @@ func (o CreateBasicAuthSecretOptions) Validate() error {
205
201
return errors .New ("must provide basic authentication credentials" )
206
202
}
207
203
208
- return nil
209
- }
210
-
211
- // GetOut check if the CreateBasicAuthSecretOptions Out Writer is set. Returns it if the Writer
212
- // is present, if not returns Writer on which all Write calls succeed without doing anything.
213
- func (o CreateBasicAuthSecretOptions ) GetOut () io.Writer {
214
- if o .Out == nil {
215
- return ioutil .Discard
204
+ if o .PromptForPassword && len (o .Password ) > 0 {
205
+ return errors .New ("must provide either --prompt or --password flag" )
216
206
}
217
207
218
- return o . Out
208
+ return nil
219
209
}
0 commit comments