From 62800bc186dda2160aee8062dc2f0cef734a1ba8 Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Mon, 28 Feb 2022 15:34:50 +0100 Subject: [PATCH] warning users when using both --base-import-paths --bare flags (#618) * warning users when using both --base-import-paths --bare flags Signed-off-by: Carlos Panato * update header Signed-off-by: cpanato --- pkg/commands/apply.go | 4 ++++ pkg/commands/build.go | 4 ++++ pkg/commands/create.go | 4 ++++ pkg/commands/options/validate.go | 36 ++++++++++++++++++++++++++++++++ pkg/commands/resolve.go | 4 ++++ pkg/commands/run.go | 4 ++++ 6 files changed, 56 insertions(+) create mode 100644 pkg/commands/options/validate.go diff --git a/pkg/commands/apply.go b/pkg/commands/apply.go index 8480a0c3bc..d27770be8f 100644 --- a/pkg/commands/apply.go +++ b/pkg/commands/apply.go @@ -82,6 +82,10 @@ func addApply(topLevel *cobra.Command) { ko apply -f config -- --namespace=foo --kubeconfig=cfg.yaml `, RunE: func(cmd *cobra.Command, args []string) error { + if err := options.Validate(po, bo); err != nil { + return fmt.Errorf("validating options: %w", err) + } + if !isKubectlAvailable() { return errors.New("error: kubectl is not available. kubectl must be installed to use ko apply") } diff --git a/pkg/commands/build.go b/pkg/commands/build.go index 64d387632a..dd6b0b33f7 100644 --- a/pkg/commands/build.go +++ b/pkg/commands/build.go @@ -58,6 +58,10 @@ func addBuild(topLevel *cobra.Command) { ko build --local github.com/foo/bar/cmd/baz github.com/foo/bar/cmd/blah`, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + if err := options.Validate(po, bo); err != nil { + return fmt.Errorf("validating options: %w", err) + } + ctx := cmd.Context() bo.InsecureRegistry = po.InsecureRegistry diff --git a/pkg/commands/create.go b/pkg/commands/create.go index f69cc14902..4fe449ae5f 100644 --- a/pkg/commands/create.go +++ b/pkg/commands/create.go @@ -67,6 +67,10 @@ func addCreate(topLevel *cobra.Command) { ko apply -f config -- --namespace=foo --kubeconfig=cfg.yaml `, RunE: func(cmd *cobra.Command, args []string) error { + if err := options.Validate(po, bo); err != nil { + return fmt.Errorf("validating options: %w", err) + } + if !isKubectlAvailable() { return errors.New("error: kubectl is not available. kubectl must be installed to use ko create") } diff --git a/pkg/commands/options/validate.go b/pkg/commands/options/validate.go new file mode 100644 index 0000000000..8ed92d5ecf --- /dev/null +++ b/pkg/commands/options/validate.go @@ -0,0 +1,36 @@ +// Copyright 2022 Google LLC All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package options + +import "log" + +const bareBaseFlagsWarning = `WARNING! +----------------------------------------------------------------- +Both --base-import-paths and --bare were set. + +--base-import-paths will take precedence and ignore --bare flag. + +In a future release this will be an error. +----------------------------------------------------------------- +` + +func Validate(po *PublishOptions, bo *BuildOptions) error { + if po.Bare && po.BaseImportPaths { + log.Print(bareBaseFlagsWarning) + // TODO: return error when we decided to make this an error, for now it is a warning + } + + return nil +} diff --git a/pkg/commands/resolve.go b/pkg/commands/resolve.go index 9444a47689..f9825b89c5 100644 --- a/pkg/commands/resolve.go +++ b/pkg/commands/resolve.go @@ -55,6 +55,10 @@ func addResolve(topLevel *cobra.Command) { ko resolve --local -f config/`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { + if err := options.Validate(po, bo); err != nil { + return fmt.Errorf("validating options: %w", err) + } + ctx := cmd.Context() bo.InsecureRegistry = po.InsecureRegistry diff --git a/pkg/commands/run.go b/pkg/commands/run.go index bb7efdaf08..5143268082 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -49,6 +49,10 @@ func addRun(topLevel *cobra.Command) { # You can also supply args and flags to the command. ko run ./cmd/baz -- -v arg1 arg2 --yes`, RunE: func(cmd *cobra.Command, args []string) error { + if err := options.Validate(po, bo); err != nil { + return fmt.Errorf("validating options: %w", err) + } + ctx := cmd.Context() // Args after -- are for kubectl, so only consider importPaths before it.