@@ -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
+ coreapiv1 "k8s.io/api/ core/v1 "
11
+ coreclientv1 "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 coreclientv1.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 ),
@@ -81,15 +89,7 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, stre
81
89
kcmdutil .CheckErr (kcmdutil .UsageErrorf (c , err .Error ()))
82
90
}
83
91
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 {
92
+ if err := o .Run (); err != nil {
93
93
kcmdutil .CheckErr (err )
94
94
}
95
95
},
@@ -103,13 +103,12 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, stre
103
103
cmd .MarkFlagFilename ("gitconfig" )
104
104
cmd .Flags ().BoolVarP (& o .PromptForPassword , "prompt" , "" , false , "If true, prompt for password or token" )
105
105
106
- kcmdutil .AddPrinterFlags (cmd )
107
-
106
+ o .PrintFlags .AddFlags (cmd )
108
107
return cmd
109
108
}
110
109
111
110
// CreateBasicAuthSecret saves created Secret structure and prints the secret name to the output on success.
112
- func (o * CreateBasicAuthSecretOptions ) CreateBasicAuthSecret () error {
111
+ func (o * CreateBasicAuthSecretOptions ) Run () error {
113
112
secret , err := o .NewBasicAuthSecret ()
114
113
if err != nil {
115
114
return err
@@ -119,16 +118,15 @@ func (o *CreateBasicAuthSecretOptions) CreateBasicAuthSecret() error {
119
118
return err
120
119
}
121
120
122
- fmt .Fprintf (o .GetOut (), "secret/%s\n " , secret .Name )
123
- return nil
121
+ return o .Printer .PrintObj (secret , o .Out )
124
122
}
125
123
126
124
// NewBasicAuthSecret builds up the Secret structure containing secret name, type and data structure
127
125
// containing desired credentials.
128
- func (o * CreateBasicAuthSecretOptions ) NewBasicAuthSecret () (* api .Secret , error ) {
129
- secret := & api .Secret {}
126
+ func (o * CreateBasicAuthSecretOptions ) NewBasicAuthSecret () (* coreapiv1 .Secret , error ) {
127
+ secret := & coreapiv1 .Secret {}
130
128
secret .Name = o .SecretName
131
- secret .Type = api .SecretTypeBasicAuth
129
+ secret .Type = coreapiv1 .SecretTypeBasicAuth
132
130
secret .Data = map [string ][]byte {}
133
131
134
132
if len (o .Username ) != 0 {
@@ -170,26 +168,34 @@ func (o *CreateBasicAuthSecretOptions) Complete(f kcmdutil.Factory, args []strin
170
168
if len (o .Password ) != 0 {
171
169
return errors .New ("must provide either --prompt or --password flag" )
172
170
}
173
- if ! kterm .IsTerminal (o .Reader ) {
171
+ if ! kterm .IsTerminal (o .In ) {
174
172
return errors .New ("provided reader is not a terminal" )
175
173
}
176
174
177
- o .Password = term .PromptForPasswordString (o .Reader , o .Out , "Password: " )
175
+ o .Password = term .PromptForPasswordString (o .In , o .Out , "Password: " )
178
176
if len (o .Password ) == 0 {
179
177
return errors .New ("password must be provided" )
180
178
}
181
179
}
182
180
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 )
181
+ config , err := f .ToRESTConfig ()
182
+ if err != nil {
183
+ return err
184
+ }
185
+
186
+ clientset , err := coreclientv1 .NewForConfig (config )
187
+ if err != nil {
188
+ return err
189
+ }
190
+ namespace , _ , err := f .ToRawKubeConfigLoader ().Namespace ()
191
+ if err != nil {
192
+ return err
193
+ }
194
+ o .SecretsInterface = clientset .Secrets (namespace )
195
+
196
+ o .Printer , err = o .PrintFlags .ToPrinter ()
197
+ if err != nil {
198
+ return err
193
199
}
194
200
195
201
return nil
@@ -207,13 +213,3 @@ func (o CreateBasicAuthSecretOptions) Validate() error {
207
213
208
214
return nil
209
215
}
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
216
- }
217
-
218
- return o .Out
219
- }
0 commit comments