Skip to content

Commit dab5aad

Browse files
Merge pull request #20244 from juanvallejo/jvallejo/update-more-cmds
update importimage, projects, rsh, req_project
2 parents dd68e3f + beb0f33 commit dab5aad

File tree

9 files changed

+158
-139
lines changed

9 files changed

+158
-139
lines changed

pkg/oc/cli/cmd/importimage.go

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"io"
65
"strings"
76

87
"github.com/spf13/cobra"
@@ -41,39 +40,15 @@ var (
4140
`)
4241
)
4342

44-
// NewCmdImportImage implements the OpenShift cli import-image command.
45-
func NewCmdImportImage(fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
46-
opts := &ImportImageOptions{}
47-
cmd := &cobra.Command{
48-
Use: "import-image IMAGESTREAM[:TAG]",
49-
Short: "Imports images from a Docker registry",
50-
Long: importImageLong,
51-
Example: fmt.Sprintf(importImageExample, fullName),
52-
Run: func(cmd *cobra.Command, args []string) {
53-
kcmdutil.CheckErr(opts.Complete(f, cmd, args, fullName, streams.Out, streams.ErrOut))
54-
kcmdutil.CheckErr(opts.Validate(cmd))
55-
kcmdutil.CheckErr(opts.Run())
56-
},
57-
}
58-
cmd.Flags().StringVar(&opts.From, "from", "", "A Docker image repository to import images from")
59-
cmd.Flags().BoolVar(&opts.Confirm, "confirm", false, "If true, allow the image stream import location to be set or changed")
60-
cmd.Flags().BoolVar(&opts.All, "all", false, "If true, import all tags from the provided source on creation or if --from is specified")
61-
cmd.Flags().StringVar(&opts.ReferencePolicy, "reference-policy", sourceReferencePolicy, "Allow to request pullthrough for external image when set to 'local'. Defaults to 'source'.")
62-
cmd.Flags().BoolVar(&opts.DryRun, "dry-run", false, "Fetch information about images without creating or updating an image stream.")
63-
cmd.Flags().BoolVar(&opts.Scheduled, "scheduled", false, "Set each imported Docker image to be periodically imported from a remote repository. Defaults to false.")
64-
opts.Insecure = cmd.Flags().Bool("insecure", false, "If true, allow importing from registries that have invalid HTTPS certificates or are hosted via HTTP. This flag will take precedence over the insecure annotation.")
65-
66-
return cmd
67-
}
68-
6943
// ImageImportOptions contains all the necessary information to perform an import.
7044
type ImportImageOptions struct {
7145
// user set values
72-
From string
73-
Confirm bool
74-
All bool
75-
Scheduled bool
76-
Insecure *bool
46+
From string
47+
Confirm bool
48+
All bool
49+
Scheduled bool
50+
Insecure bool
51+
InsecureFlagProvided bool
7752

7853
DryRun bool
7954

@@ -84,27 +59,54 @@ type ImportImageOptions struct {
8459
Target string
8560
ReferencePolicy string
8661

87-
CommandName string
88-
8962
// helpers
90-
out io.Writer
91-
errout io.Writer
9263
imageClient imageclient.ImageInterface
9364
isClient imageclient.ImageStreamInterface
65+
66+
genericclioptions.IOStreams
67+
}
68+
69+
func NewImportImageOptions(name string, streams genericclioptions.IOStreams) *ImportImageOptions {
70+
return &ImportImageOptions{
71+
IOStreams: streams,
72+
ReferencePolicy: sourceReferencePolicy,
73+
}
74+
}
75+
76+
// NewCmdImportImage implements the OpenShift cli import-image command.
77+
func NewCmdImportImage(fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
78+
o := NewImportImageOptions(fullName, streams)
79+
80+
cmd := &cobra.Command{
81+
Use: "import-image IMAGESTREAM[:TAG]",
82+
Short: "Imports images from a Docker registry",
83+
Long: importImageLong,
84+
Example: fmt.Sprintf(importImageExample, fullName),
85+
Run: func(cmd *cobra.Command, args []string) {
86+
kcmdutil.CheckErr(o.Complete(f, cmd, args))
87+
kcmdutil.CheckErr(o.Validate(cmd))
88+
kcmdutil.CheckErr(o.Run())
89+
},
90+
}
91+
cmd.Flags().StringVar(&o.From, "from", o.From, "A Docker image repository to import images from")
92+
cmd.Flags().BoolVar(&o.Confirm, "confirm", o.Confirm, "If true, allow the image stream import location to be set or changed")
93+
cmd.Flags().BoolVar(&o.All, "all", o.All, "If true, import all tags from the provided source on creation or if --from is specified")
94+
cmd.Flags().StringVar(&o.ReferencePolicy, "reference-policy", o.ReferencePolicy, "Allow to request pullthrough for external image when set to 'local'. Defaults to 'source'.")
95+
cmd.Flags().BoolVar(&o.DryRun, "dry-run", o.DryRun, "Fetch information about images without creating or updating an image stream.")
96+
cmd.Flags().BoolVar(&o.Scheduled, "scheduled", o.Scheduled, "Set each imported Docker image to be periodically imported from a remote repository. Defaults to false.")
97+
cmd.Flags().BoolVar(&o.Insecure, "insecure", o.Insecure, "If true, allow importing from registries that have invalid HTTPS certificates or are hosted via HTTP. This flag will take precedence over the insecure annotation.")
98+
99+
return cmd
94100
}
95101

96102
// Complete turns a partially defined ImportImageOptions into a solvent structure
97103
// which can be validated and used for aa import.
98-
func (o *ImportImageOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []string, commandName string, out, errout io.Writer) error {
99-
o.CommandName = commandName
100-
104+
func (o *ImportImageOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []string) error {
101105
if len(args) > 0 {
102106
o.Target = args[0]
103107
}
104108

105-
if !cmd.Flags().Lookup("insecure").Changed {
106-
o.Insecure = nil
107-
}
109+
o.InsecureFlagProvided = cmd.Flags().Lookup("insecure").Changed
108110
if !cmd.Flags().Lookup("reference-policy").Changed {
109111
o.ReferencePolicy = ""
110112
}
@@ -127,9 +129,6 @@ func (o *ImportImageOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, ar
127129
o.imageClient = client.Image()
128130
o.isClient = client.Image().ImageStreams(namespace)
129131

130-
o.out = out
131-
o.errout = errout
132-
133132
return nil
134133
}
135134

@@ -173,15 +172,15 @@ func (o *ImportImageOptions) Run() error {
173172

174173
if o.DryRun {
175174
if wasError(result) {
176-
fmt.Fprintf(o.errout, "The dry-run import completed with errors.\n\n")
175+
fmt.Fprintf(o.ErrOut, "The dry-run import completed with errors.\n\n")
177176
} else {
178-
fmt.Fprint(o.out, "The dry-run import completed successfully.\n\n")
177+
fmt.Fprint(o.Out, "The dry-run import completed successfully.\n\n")
179178
}
180179
} else {
181180
if wasError(result) {
182-
fmt.Fprintf(o.errout, "The import completed with errors.\n\n")
181+
fmt.Fprintf(o.ErrOut, "The import completed with errors.\n\n")
183182
} else {
184-
fmt.Fprint(o.out, "The import completed successfully.\n\n")
183+
fmt.Fprint(o.Out, "The import completed successfully.\n\n")
185184
}
186185
}
187186

@@ -191,20 +190,20 @@ func (o *ImportImageOptions) Run() error {
191190
if err != nil {
192191
return err
193192
}
194-
fmt.Fprintln(o.out, info)
193+
fmt.Fprintln(o.Out, info)
195194
}
196195

197196
if repo := result.Status.Repository; repo != nil {
198197
for _, image := range repo.Images {
199198
if image.Image != nil {
200199
info, err := describe.DescribeImage(image.Image, imageapi.JoinImageStreamTag(stream.Name, image.Tag))
201200
if err != nil {
202-
fmt.Fprintf(o.errout, "error: tag %s failed: %v\n", image.Tag, err)
201+
fmt.Fprintf(o.ErrOut, "error: tag %s failed: %v\n", image.Tag, err)
203202
} else {
204-
fmt.Fprintln(o.out, info)
203+
fmt.Fprintln(o.Out, info)
205204
}
206205
} else {
207-
fmt.Fprintf(o.errout, "error: repository tag %s failed: %v\n", image.Tag, image.Status.Message)
206+
fmt.Fprintf(o.ErrOut, "error: repository tag %s failed: %v\n", image.Tag, image.Status.Message)
208207
}
209208
}
210209
}
@@ -213,17 +212,17 @@ func (o *ImportImageOptions) Run() error {
213212
if image.Image != nil {
214213
info, err := describe.DescribeImage(image.Image, imageapi.JoinImageStreamTag(stream.Name, image.Tag))
215214
if err != nil {
216-
fmt.Fprintf(o.errout, "error: tag %s failed: %v\n", image.Tag, err)
215+
fmt.Fprintf(o.ErrOut, "error: tag %s failed: %v\n", image.Tag, err)
217216
} else {
218-
fmt.Fprintln(o.out, info)
217+
fmt.Fprintln(o.Out, info)
219218
}
220219
} else {
221-
fmt.Fprintf(o.errout, "error: tag %s failed: %v\n", image.Tag, image.Status.Message)
220+
fmt.Fprintf(o.ErrOut, "error: tag %s failed: %v\n", image.Tag, image.Status.Message)
222221
}
223222
}
224223

225224
if r := result.Status.Repository; r != nil && len(r.AdditionalTags) > 0 {
226-
fmt.Fprintf(o.out, "\ninfo: The remote repository contained %d additional tags which were not imported: %s\n", len(r.AdditionalTags), strings.Join(r.AdditionalTags, ", "))
225+
fmt.Fprintf(o.Out, "\ninfo: The remote repository contained %d additional tags which were not imported: %s\n", len(r.AdditionalTags), strings.Join(r.AdditionalTags, ", "))
227226
}
228227
return nil
229228
}
@@ -457,8 +456,8 @@ func (o *ImportImageOptions) newImageStreamImport(stream *imageapi.ImageStream)
457456
insecureAnnotation := stream.Annotations[imageapi.InsecureRepositoryAnnotation]
458457
insecure := insecureAnnotation == "true"
459458
// --insecure flag (if provided) takes precedence over insecure annotation
460-
if o.Insecure != nil {
461-
insecure = *o.Insecure
459+
if o.InsecureFlagProvided {
460+
insecure = o.Insecure
462461
}
463462

464463
return isi, insecure

pkg/oc/cli/cmd/importimage_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,16 @@ func TestCreateImageImport(t *testing.T) {
580580
From: test.from,
581581
All: test.all,
582582
Scheduled: test.scheduled,
583-
Insecure: test.insecure,
584583
ReferencePolicy: test.referencePolicy,
585584
Confirm: test.confirm,
586585
isClient: fake.Image().ImageStreams("other"),
587586
}
587+
588+
if test.insecure != nil {
589+
o.Insecure = *test.insecure
590+
o.InsecureFlagProvided = true
591+
}
592+
588593
// we need to run Validate, because it sets appropriate Name and Tag
589594
if err := o.Validate(&cobra.Command{}); err != nil {
590595
t.Errorf("%s: unexpected error: %v", name, err)

pkg/oc/cli/cmd/project.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"io"
87
"net/url"
98

109
kapierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -32,7 +31,6 @@ type ProjectOptions struct {
3231
Config clientcmdapi.Config
3332
ClientConfig *restclient.Config
3433
ClientFn func() (projectclient.ProjectInterface, kclientset.Interface, error)
35-
Out io.Writer
3634
PathOptions *kclientcmd.PathOptions
3735

3836
ProjectName string
@@ -41,6 +39,8 @@ type ProjectOptions struct {
4139

4240
// SkipAccessValidation means that if a specific name is requested, don't bother checking for access to the project
4341
SkipAccessValidation bool
42+
43+
genericclioptions.IOStreams
4444
}
4545

4646
var (
@@ -65,32 +65,38 @@ var (
6565
%[1]s`)
6666
)
6767

68+
func NewProjectOptions(streams genericclioptions.IOStreams) *ProjectOptions {
69+
return &ProjectOptions{
70+
IOStreams: streams,
71+
}
72+
}
73+
6874
// NewCmdProject implements the OpenShift cli rollback command
6975
func NewCmdProject(fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
70-
options := &ProjectOptions{}
76+
o := NewProjectOptions(streams)
7177

7278
cmd := &cobra.Command{
7379
Use: "project [NAME]",
7480
Short: "Switch to another project",
7581
Long: projectLong,
7682
Example: fmt.Sprintf(projectExample, fullName),
7783
Run: func(cmd *cobra.Command, args []string) {
78-
options.PathOptions = cliconfig.NewPathOptions(cmd)
84+
o.PathOptions = cliconfig.NewPathOptions(cmd)
7985

80-
if err := options.Complete(f, args, streams.Out); err != nil {
86+
if err := o.Complete(f, args); err != nil {
8187
kcmdutil.CheckErr(kcmdutil.UsageErrorf(cmd, err.Error()))
8288
}
8389

84-
if err := options.RunProject(); err != nil {
90+
if err := o.RunProject(); err != nil {
8591
kcmdutil.CheckErr(err)
8692
}
8793
},
8894
}
89-
cmd.Flags().BoolVarP(&options.DisplayShort, "short", "q", false, "If true, display only the project name")
95+
cmd.Flags().BoolVarP(&o.DisplayShort, "short", "q", false, "If true, display only the project name")
9096
return cmd
9197
}
9298

93-
func (o *ProjectOptions) Complete(f genericclioptions.RESTClientGetter, args []string, out io.Writer) error {
99+
func (o *ProjectOptions) Complete(f genericclioptions.RESTClientGetter, args []string) error {
94100
var err error
95101

96102
argsLength := len(args)
@@ -146,8 +152,6 @@ func (o *ProjectOptions) Complete(f genericclioptions.RESTClientGetter, args []s
146152
return projectClient.Project(), kc, nil
147153
}
148154

149-
o.Out = out
150-
151155
return nil
152156
}
153157
func (o ProjectOptions) Validate() error {
@@ -158,7 +162,6 @@ func (o ProjectOptions) Validate() error {
158162
func (o ProjectOptions) RunProject() error {
159163
config := o.Config
160164
clientCfg := o.ClientConfig
161-
out := o.Out
162165

163166
var currentProject string
164167
currentContext := config.Contexts[config.CurrentContext]
@@ -170,7 +173,7 @@ func (o ProjectOptions) RunProject() error {
170173
if len(o.ProjectName) == 0 {
171174
if len(currentProject) > 0 {
172175
if o.DisplayShort {
173-
fmt.Fprintln(out, currentProject)
176+
fmt.Fprintln(o.Out, currentProject)
174177
return nil
175178
}
176179

@@ -193,17 +196,17 @@ func (o ProjectOptions) RunProject() error {
193196
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
194197
// a context name they didn't choose
195198
if config.CurrentContext == defaultContextName {
196-
fmt.Fprintf(out, "Using project %q on server %q.\n", currentProject, clientCfg.Host)
199+
fmt.Fprintf(o.Out, "Using project %q on server %q.\n", currentProject, clientCfg.Host)
197200

198201
} else {
199-
fmt.Fprintf(out, "Using project %q from context named %q on server %q.\n", currentProject, config.CurrentContext, clientCfg.Host)
202+
fmt.Fprintf(o.Out, "Using project %q from context named %q on server %q.\n", currentProject, config.CurrentContext, clientCfg.Host)
200203
}
201204

202205
} else {
203206
if o.DisplayShort {
204207
return fmt.Errorf("no project has been set")
205208
}
206-
fmt.Fprintf(out, "No project has been set. Pass a project name to make that the default.\n")
209+
fmt.Fprintf(o.Out, "No project has been set. Pass a project name to make that the default.\n")
207210
}
208211
return nil
209212
}
@@ -282,7 +285,7 @@ func (o ProjectOptions) RunProject() error {
282285
}
283286

284287
if o.DisplayShort {
285-
fmt.Fprintln(out, namespaceInUse)
288+
fmt.Fprintln(o.Out, namespaceInUse)
286289
return nil
287290
}
288291

@@ -293,20 +296,20 @@ func (o ProjectOptions) RunProject() error {
293296
switch {
294297
// if there is no namespace, then the only information we can provide is the context and server
295298
case (len(namespaceInUse) == 0):
296-
fmt.Fprintf(out, "Now using context named %q on server %q.\n", contextInUse, clientCfg.Host)
299+
fmt.Fprintf(o.Out, "Now using context named %q on server %q.\n", contextInUse, clientCfg.Host)
297300

298301
// inform them that they are already in the project they are trying to switch to
299302
case currentProject == namespaceInUse:
300-
fmt.Fprintf(out, "Already on project %q on server %q.\n", currentProject, clientCfg.Host)
303+
fmt.Fprintf(o.Out, "Already on project %q on server %q.\n", currentProject, clientCfg.Host)
301304

302305
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
303306
// a context name they didn't choose
304307
case (argument == namespaceInUse) && (contextInUse == defaultContextName):
305-
fmt.Fprintf(out, "Now using project %q on server %q.\n", namespaceInUse, clientCfg.Host)
308+
fmt.Fprintf(o.Out, "Now using project %q on server %q.\n", namespaceInUse, clientCfg.Host)
306309

307310
// in all other cases, display all information
308311
default:
309-
fmt.Fprintf(out, "Now using project %q from context named %q on server %q.\n", namespaceInUse, contextInUse, clientCfg.Host)
312+
fmt.Fprintf(o.Out, "Now using project %q from context named %q on server %q.\n", namespaceInUse, contextInUse, clientCfg.Host)
310313

311314
}
312315

0 commit comments

Comments
 (0)