Skip to content

Commit

Permalink
Add changes to how we detect json
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrage committed Jul 26, 2019
1 parent 2de03a4 commit ee5b504
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/log/status.go
Expand Up @@ -272,7 +272,7 @@ func SpinnerNoSpin(status string) *Status {
func IsJSON() bool {

flag := pflag.Lookup("o")
if flag != nil {
if flag != nil && flag.Changed {
return strings.Contains(pflag.Lookup("o").Value.String(), "json")
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/cli.go
Expand Up @@ -93,7 +93,7 @@ func NewCmdOdo(name, fullName string) *cobra.Command {
// 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")
flag.CommandLine.String("o", "json", "Specify output format, supported format: json")

// 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
28 changes: 23 additions & 5 deletions pkg/odo/genericclioptions/runnable.go
Expand Up @@ -2,10 +2,12 @@ package genericclioptions

import (
"flag"
"os"

"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/odo/util"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type Runnable interface {
Expand All @@ -16,16 +18,32 @@ type Runnable interface {

func GenericRun(o Runnable, cmd *cobra.Command, args []string) {

// CheckMachineReadableOutput
// fixes / checks all related machine readable output functions
CheckMachineReadableOutputCommand()

// Run completion, validation and run.
util.LogErrorAndExit(o.Complete(cmd.Name(), cmd, args), "")
util.LogErrorAndExit(o.Validate(), "")
util.LogErrorAndExit(o.Run(), "")
}

// CheckMachineReadableOutputCommand performs machine-readable output functions required to
// have it work correctly
func CheckMachineReadableOutputCommand() {

// Check that the -o flag has been correctly set as json.
outputFlag := pflag.Lookup("o")
if outputFlag != nil && outputFlag.Changed && outputFlag.Value.String() != "json" {
log.Error("Please input a valid output format for -o, available format: json")
os.Exit(1)
}

// Before running anything, we will make sure that no verbose output is made
// This is a HACK to manually override `-v 4` to `-v 0` (in which we have no glog.V(0) in our code...
// in order to have NO verbose output when combining both `-o json` and `-v 4` so json output
// is not malformed / mixed in with normal logging
if log.IsJSON() {
flag.Set("v", "0")
}

// Run completion, validation and run.
util.LogErrorAndExit(o.Complete(cmd.Name(), cmd, args), "")
util.LogErrorAndExit(o.Validate(), "")
util.LogErrorAndExit(o.Run(), "")
}

0 comments on commit ee5b504

Please sign in to comment.