Skip to content

Commit ab39188

Browse files
committed
add warning on invalid --output-version
1 parent 5c1e463 commit ab39188

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

pkg/cmd/cli/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func NewCommandCLI(name, fullName string, in io.Reader, out, errout io.Writer) *
160160
cmd.NewCmdApply(fullName, f, out),
161161
cmd.NewCmdPatch(fullName, f, out),
162162
cmd.NewCmdProcess(fullName, f, out),
163-
cmd.NewCmdExport(fullName, f, in, out),
163+
cmd.NewCmdExport(fullName, f, in, out, errout),
164164
cmd.NewCmdExtract(fullName, f, in, out, errout),
165165
observe.NewCmdObserve(fullName, f, out, errout),
166166
policy.NewCmdPolicy(policy.PolicyRecommendedName, fullName+" "+policy.PolicyRecommendedName, f, out),

pkg/cmd/cli/cmd/export.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ to generate the API structure for a template to which you can add parameters and
4747
%[1]s export service -o json`
4848
)
4949

50-
func NewCmdExport(fullName string, f *clientcmd.Factory, in io.Reader, out io.Writer) *cobra.Command {
50+
func NewCmdExport(fullName string, f *clientcmd.Factory, in io.Reader, out, errOut io.Writer) *cobra.Command {
5151
exporter := &DefaultExporter{}
5252
var filenames []string
5353
cmd := &cobra.Command{
@@ -56,7 +56,7 @@ func NewCmdExport(fullName string, f *clientcmd.Factory, in io.Reader, out io.Wr
5656
Long: exportLong,
5757
Example: fmt.Sprintf(exportExample, fullName),
5858
Run: func(cmd *cobra.Command, args []string) {
59-
err := RunExport(f, exporter, in, out, cmd, args, filenames)
59+
err := RunExport(f, exporter, in, out, errOut, cmd, args, filenames)
6060
if err == cmdutil.ErrExit {
6161
os.Exit(1)
6262
}
@@ -76,7 +76,7 @@ func NewCmdExport(fullName string, f *clientcmd.Factory, in io.Reader, out io.Wr
7676
return cmd
7777
}
7878

79-
func RunExport(f *clientcmd.Factory, exporter Exporter, in io.Reader, out io.Writer, cmd *cobra.Command, args []string, filenames []string) error {
79+
func RunExport(f *clientcmd.Factory, exporter Exporter, in io.Reader, out, errOut io.Writer, cmd *cobra.Command, args []string, filenames []string) error {
8080
selector := kcmdutil.GetFlagString(cmd, "selector")
8181
allNamespaces := kcmdutil.GetFlagBool(cmd, "all-namespaces")
8282
exact := kcmdutil.GetFlagBool(cmd, "exact")
@@ -171,5 +171,18 @@ func RunExport(f *clientcmd.Factory, exporter Exporter, in io.Reader, out io.Wri
171171
if err != nil {
172172
return err
173173
}
174-
return p.PrintObj(result, out)
174+
err = p.PrintObj(result, out)
175+
if err != nil {
176+
return err
177+
}
178+
179+
// validSpecifiedVersion resolves to true if the version passed to this function matches the
180+
// version assigned to the converted object
181+
validSpecifiedVersion := (result.GetObjectKind().GroupVersionKind().Version == outputVersion.Version)
182+
outputVersionString := kcmdutil.GetFlagString(cmd, "output-version")
183+
if !validSpecifiedVersion && len(outputVersionString) > 0 {
184+
err = fmt.Errorf("the output version specified (%v) is invalid, defaulting to %v\n", outputVersion, clientConfig.GroupVersion)
185+
}
186+
187+
return err
175188
}

0 commit comments

Comments
 (0)