Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a generic --run-on flag #6187

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions pkg/machineoutput/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"sync"

"github.com/redhat-developer/odo/pkg/log"

"github.com/spf13/cobra"
)

// ListKind is the kind used for all lists in the machine readable output
Expand All @@ -21,13 +19,6 @@ const APIVersion = "odo.dev/v1alpha1"
// multiple threads.
var unindentedMutex = &sync.Mutex{}

func UsedByCommand(cmd *cobra.Command) {
if cmd.Annotations == nil {
cmd.Annotations = map[string]string{}
}
cmd.Annotations["machineoutput"] = "json"
}

// OutputSuccessUnindented outputs a "successful" machine-readable output format in unindented json
func OutputSuccessUnindented(machineOutput interface{}) {
printableOutput, err := json.Marshal(machineOutput)
Expand Down
4 changes: 2 additions & 2 deletions pkg/odo/cli/add/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (o *AddBindingOptions) SetClientset(clientset *clientset.Clientset) {
o.clientset = clientset
}

func (o *AddBindingOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (o *AddBindingOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {
o.Context, err = genericclioptions.New(genericclioptions.NewCreateParameters(cmdline).NeedDevfile(""))
// The command must work without Devfile
if err != nil && !genericclioptions.IsNoDevfileError(err) {
Expand All @@ -79,7 +79,7 @@ func (o *AddBindingOptions) Complete(cmdline cmdline.Cmdline, args []string) (er
return nil
}

func (o *AddBindingOptions) Validate() (err error) {
func (o *AddBindingOptions) Validate(ctx context.Context) (err error) {
withDevfile := o.EnvSpecificInfo.GetDevfileObj().Data != nil
return o.clientset.BindingClient.ValidateAddBinding(o.flags, withDevfile)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/odo/cli/alizer/alizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/machineoutput"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/commonflags"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
Expand All @@ -34,11 +34,11 @@ func (o *AlizerOptions) SetClientset(clientset *clientset.Clientset) {
o.clientset = clientset
}

func (o *AlizerOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (o *AlizerOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {
return nil
}

func (o *AlizerOptions) Validate() error {
func (o *AlizerOptions) Validate(ctx context.Context) error {
return nil
}

Expand Down Expand Up @@ -73,7 +73,7 @@ func NewCmdAlizer(name, fullName string) *cobra.Command {
},
}
clientset.Add(alizerCmd, clientset.ALIZER)
machineoutput.UsedByCommand(alizerCmd)
commonflags.UseOutputFlag(alizerCmd)
alizerCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
alizerCmd.Annotations["command"] = "utility"
return alizerCmd
Expand Down
4 changes: 2 additions & 2 deletions pkg/odo/cli/build_images/build_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (o *BuildImagesOptions) SetClientset(clientset *clientset.Clientset) {
}

// Complete completes LoginOptions after they've been created
func (o *BuildImagesOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (o *BuildImagesOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {
o.Context, err = genericclioptions.New(genericclioptions.NewCreateParameters(cmdline).NeedDevfile(o.contextFlag).IsOffline())
if err != nil {
return err
Expand All @@ -61,7 +61,7 @@ func (o *BuildImagesOptions) Complete(cmdline cmdline.Cmdline, args []string) (e
}

// Validate validates the LoginOptions based on completed values
func (o *BuildImagesOptions) Validate() (err error) {
func (o *BuildImagesOptions) Validate(ctx context.Context) (err error) {
return
}

Expand Down
10 changes: 3 additions & 7 deletions pkg/odo/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"unicode"

"github.com/redhat-developer/odo/pkg/odo/cli/logs"
"github.com/redhat-developer/odo/pkg/odo/commonflags"

"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cli/add"
Expand Down Expand Up @@ -133,13 +134,8 @@ func odoRootCmd(name, fullName string) *cobra.Command {
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.odo.yaml)")

// Add the machine readable output flag to all commands
// We use "flag" in order to make this accessible throughtout ALL of odo, rather than the
// above traditional "persistentflags" usage that does not make it a pointer within the 'pflag'
// package

flag.CommandLine.String("o", "", "Specify output format, supported format: json")
_ = pflag.CommandLine.MarkHidden("o")
commonflags.AddOutputFlag()
commonflags.AddRunOnFlag()

// Here we add the necessary "logging" flags.. However, we choose to hide some of these from the user
// as they are not necessarily needed and more for advanced debugging
Expand Down
4 changes: 2 additions & 2 deletions pkg/odo/cli/create/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (o *NamespaceCreateOptions) SetClientset(clientset *clientset.Clientset) {
}

// Complete completes NamespaceCreateOptions after they've been created
func (nco *NamespaceCreateOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (nco *NamespaceCreateOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {
nco.namespaceName = args[0]
nco.Context, err = genericclioptions.New(genericclioptions.NewCreateParameters(cmdline))
if err != nil {
Expand All @@ -79,7 +79,7 @@ func (nco *NamespaceCreateOptions) Complete(cmdline cmdline.Cmdline, args []stri
}

// Validate validates the parameters of the NamespaceCreateOptions
func (nco *NamespaceCreateOptions) Validate() error {
func (nco *NamespaceCreateOptions) Validate(ctx context.Context) error {
return dfutil.ValidateK8sResourceName("namespace name", nco.namespaceName)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/odo/cli/delete/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (o *ComponentOptions) SetClientset(clientset *clientset.Clientset) {
o.clientset = clientset
}

func (o *ComponentOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (o *ComponentOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {
// 1. Name is not passed, and odo has access to devfile.yaml; Name is not passed so we assume that odo has access to the devfile.yaml
if o.name == "" {
o.Context, err = genericclioptions.New(genericclioptions.NewCreateParameters(cmdline).NeedDevfile(""))
Expand All @@ -83,7 +83,7 @@ func (o *ComponentOptions) Complete(cmdline cmdline.Cmdline, args []string) (err
return nil
}

func (o *ComponentOptions) Validate() (err error) {
func (o *ComponentOptions) Validate(ctx context.Context) (err error) {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/odo/cli/delete/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (do *DeleteOptions) SetClientset(clientset *clientset.Clientset) {
}

// Complete completes DeleteOptions after they've been created
func (do *DeleteOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (do *DeleteOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {
do.namespaceName = args[0]
do.Context, err = genericclioptions.New(genericclioptions.NewCreateParameters(cmdline))
if err != nil {
Expand All @@ -78,7 +78,7 @@ func (do *DeleteOptions) Complete(cmdline cmdline.Cmdline, args []string) (err e
}

// Validate validates the DeleteOptions based on completed values
func (do *DeleteOptions) Validate() (err error) {
func (do *DeleteOptions) Validate(ctx context.Context) (err error) {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/odo/cli/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (o *DeployOptions) SetClientset(clientset *clientset.Clientset) {
}

// Complete DeployOptions after they've been created
func (o *DeployOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (o *DeployOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {
o.contextDir, err = os.Getwd()
if err != nil {
return err
Expand Down Expand Up @@ -109,7 +109,7 @@ func (o *DeployOptions) Complete(cmdline cmdline.Cmdline, args []string) (err er
}

// Validate validates the DeployOptions based on completed values
func (o *DeployOptions) Validate() error {
func (o *DeployOptions) Validate(ctx context.Context) error {
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/odo/cli/describe/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/machineoutput"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/commonflags"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
)
Expand Down Expand Up @@ -55,7 +55,7 @@ func (o *BindingOptions) SetClientset(clientset *clientset.Clientset) {
o.clientset = clientset
}

func (o *BindingOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (o *BindingOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {
if o.nameFlag == "" {
o.contextDir, err = os.Getwd()
if err != nil {
Expand All @@ -73,7 +73,7 @@ func (o *BindingOptions) Complete(cmdline cmdline.Cmdline, args []string) (err e
return nil
}

func (o *BindingOptions) Validate() (err error) {
func (o *BindingOptions) Validate(ctx context.Context) (err error) {
return nil
}

Expand Down Expand Up @@ -127,7 +127,7 @@ func NewCmdBinding(name, fullName string) *cobra.Command {
}
bindingCmd.Flags().StringVar(&o.nameFlag, "name", "", "Name of the binding to describe, optional. By default, the bindings in the local devfile are described")
clientset.Add(bindingCmd, clientset.KUBERNETES, clientset.BINDING)
machineoutput.UsedByCommand(bindingCmd)
commonflags.UseOutputFlag(bindingCmd)

return bindingCmd
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/odo/cli/describe/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/machineoutput"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/commonflags"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
)
Expand Down Expand Up @@ -59,7 +59,7 @@ func (o *ComponentOptions) SetClientset(clientset *clientset.Clientset) {
o.clientset = clientset
}

func (o *ComponentOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (o *ComponentOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {
// 1. Name is not passed, and odo has access to devfile.yaml; Name is not passed so we assume that odo has access to the devfile.yaml
if o.nameFlag == "" {

Expand All @@ -84,7 +84,7 @@ func (o *ComponentOptions) Complete(cmdline cmdline.Cmdline, args []string) (err
return nil
}

func (o *ComponentOptions) Validate() (err error) {
func (o *ComponentOptions) Validate(ctx context.Context) (err error) {
return nil
}

Expand Down Expand Up @@ -246,7 +246,7 @@ func NewCmdComponent(name, fullName string) *cobra.Command {
componentCmd.Flags().StringVar(&o.nameFlag, "name", "", "Name of the component to describe, optional. By default, the component in the local devfile is described")
componentCmd.Flags().StringVar(&o.namespaceFlag, "namespace", "", "Namespace in which to find the component to describe, optional. By default, the current namespace defined in kubeconfig is used")
clientset.Add(componentCmd, clientset.KUBERNETES, clientset.STATE)
machineoutput.UsedByCommand(componentCmd)
commonflags.UseOutputFlag(componentCmd)

return componentCmd
}
4 changes: 2 additions & 2 deletions pkg/odo/cli/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (o *DevOptions) SetClientset(clientset *clientset.Clientset) {
o.clientset = clientset
}

func (o *DevOptions) Complete(cmdline cmdline.Cmdline, args []string) error {
func (o *DevOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) error {
var err error

// Define this first so that if user hits Ctrl+c very soon after running odo dev, odo doesn't panic
Expand Down Expand Up @@ -143,7 +143,7 @@ func (o *DevOptions) Complete(cmdline cmdline.Cmdline, args []string) error {
return nil
}

func (o *DevOptions) Validate() error {
func (o *DevOptions) Validate(ctx context.Context) error {
if !o.debugFlag && !libdevfile.HasRunCommand(o.initialDevfileObj.Data) {
return clierrors.NewNoCommandInDevfileError("run")
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/odo/cli/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
"github.com/redhat-developer/odo/pkg/init/backend"
"github.com/redhat-developer/odo/pkg/libdevfile"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/machineoutput"
"github.com/redhat-developer/odo/pkg/odo/cli/messages"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/commonflags"
fcontext "github.com/redhat-developer/odo/pkg/odo/commonflags/context"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (o *InitOptions) SetClientset(clientset *clientset.Clientset) {
// Complete will build the parameters for init, using different backends based on the flags set,
// either by using flags or interactively if no flag is passed
// Complete will return an error immediately if the current working directory is not empty
func (o *InitOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (o *InitOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {

o.ctx = cmdline.Context()

Expand All @@ -98,7 +99,7 @@ func (o *InitOptions) Complete(cmdline cmdline.Cmdline, args []string) (err erro
}

// Validate validates the InitOptions based on completed values
func (o *InitOptions) Validate() error {
func (o *InitOptions) Validate(ctx context.Context) error {

devfilePresent, err := location.DirectoryContainsDevfile(o.clientset.FS, o.contextDir)
if err != nil {
Expand All @@ -113,7 +114,7 @@ func (o *InitOptions) Validate() error {
return err
}

if len(o.flags) == 0 && log.IsJSON() {
if len(o.flags) == 0 && fcontext.IsJsonOutput(ctx) {
return errors.New("parameters are expected to select a devfile")
}
return nil
Expand Down Expand Up @@ -257,7 +258,7 @@ func NewCmdInit(name, fullName string) *cobra.Command {
initCmd.Flags().String(backend.FLAG_STARTER, "", "name of the starter project")
initCmd.Flags().String(backend.FLAG_DEVFILE_PATH, "", "path to a devfile. This is an alternative to using devfile from Devfile registry. It can be local filesystem path or http(s) URL")

machineoutput.UsedByCommand(initCmd)
commonflags.UseOutputFlag(initCmd)
// Add a defined annotation in order to appear in the help menu
initCmd.Annotations["command"] = "main"
initCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestInitOptions_Complete(t *testing.T) {
if tt.initExpects != nil {
tt.initExpects(initClient)
}
if err := o.Complete(cmdline, []string{}); (err != nil) != tt.wantErr {
if err := o.Complete(context.TODO(), cmdline, []string{}); (err != nil) != tt.wantErr {
t.Errorf("InitOptions.Complete() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
8 changes: 4 additions & 4 deletions pkg/odo/cli/list/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/machineoutput"
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/commonflags"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
)
Expand Down Expand Up @@ -63,7 +63,7 @@ func (o *BindingListOptions) SetClientset(clientset *clientset.Clientset) {
}

// Complete completes BindingListOptions after they've been created
func (o *BindingListOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
func (o *BindingListOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) (err error) {
o.contextDir, err = os.Getwd()
if err != nil {
return err
Expand All @@ -86,7 +86,7 @@ func (o *BindingListOptions) Complete(cmdline cmdline.Cmdline, args []string) (e
}

// Validate validates the BindingListOptions based on completed values
func (o *BindingListOptions) Validate() (err error) {
func (o *BindingListOptions) Validate(ctx context.Context) (err error) {
return nil
}

Expand Down Expand Up @@ -138,7 +138,7 @@ func NewCmdBindingList(name, fullName string) *cobra.Command {
}
clientset.Add(bindingListCmd, clientset.KUBERNETES, clientset.BINDING)
bindingListCmd.Flags().StringVar(&o.namespaceFlag, "namespace", "", "Namespace for odo to scan for bindings")
machineoutput.UsedByCommand(bindingListCmd)
commonflags.UseOutputFlag(bindingListCmd)
return bindingListCmd
}

Expand Down
Loading