Replace RootCmd() by NewCmdOdo function and use it.

This commit is contained in:
Chris Laprun
2018-11-22 21:53:58 +01:00
parent 1406285b0f
commit 73c74946eb
3 changed files with 54 additions and 67 deletions

View File

@@ -148,9 +148,9 @@ func main() {
} else {
switch args[0] {
case "reference":
fmt.Print(referencePrinter(cli.RootCmd(), 0))
fmt.Print(referencePrinter(cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName), 0))
case "structure":
fmt.Print(commandPrinter(cli.RootCmd(), 0))
fmt.Print(commandPrinter(cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName), 0))
default:
fmt.Print(command.Usage())
}

View File

@@ -2,9 +2,14 @@ package main
import (
"flag"
"fmt"
"github.com/golang/glog"
"github.com/posener/complete"
"github.com/redhat-developer/odo/pkg/config"
"github.com/redhat-developer/odo/pkg/odo/cli"
"github.com/redhat-developer/odo/pkg/odo/cli/version"
"github.com/redhat-developer/odo/pkg/odo/util"
"github.com/redhat-developer/odo/pkg/odo/util/completion"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@@ -12,7 +17,7 @@ import (
func main() {
// create the complete command
root := cli.RootCmd()
root := cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName)
rootCmp := createCompletion(root)
cmp := complete.New("odo", rootCmp)
@@ -24,7 +29,9 @@ func main() {
// add the completion flags to the root command, though they won't appear in completions
root.Flags().AddGoFlagSet(flag.CommandLine)
// override usage so that flag.Parse uses root command's usage instead of default one when invoked with -h
flag.Usage = usage
flag.Usage = func() {
_ = root.Usage
}
// parse the flags - both the program's flags and the completion flags
flag.Parse()
@@ -38,11 +45,26 @@ func main() {
}
// Call commands
cli.Execute()
}
// checking the value of updatenotification in config
// before proceeding with fetching the latest version
cfg, err := config.New()
if err != nil {
util.CheckError(err, "")
}
if cfg.GetUpdateNotification() == true {
updateInfo := make(chan string)
go version.GetLatestReleaseInfo(updateInfo)
func usage() {
_ = cli.RootCmd().Usage()
util.CheckError(root.Execute(), "")
select {
case message := <-updateInfo:
fmt.Println(message)
default:
glog.V(4).Info("Could not get the latest release information in time. Never mind, exiting gracefully :)")
}
} else {
util.CheckError(root.Execute(), "")
}
}
func createCompletion(root *cobra.Command) complete.Command {

View File

@@ -3,9 +3,6 @@ package cli
import (
"flag"
"fmt"
"github.com/golang/glog"
"github.com/redhat-developer/odo/pkg/config"
"github.com/redhat-developer/odo/pkg/odo/cli/application"
"github.com/redhat-developer/odo/pkg/odo/cli/catalog"
"github.com/redhat-developer/odo/pkg/odo/cli/component"
@@ -18,13 +15,24 @@ import (
"github.com/redhat-developer/odo/pkg/odo/cli/utils"
"github.com/redhat-developer/odo/pkg/odo/cli/version"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/util"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
)
// Templates
var rootUsageTemplate = `Usage:{{if .Runnable}}
const OdoRecommendedName = "odo"
var (
odoLong = ktemplates.LongDesc(`
Odo (OpenShift Do) is a CLI tool for running OpenShift applications in a fast and automated matter. Odo reduces the complexity of deployment by adding iterative development without the worry of deploying your source code.
Find more information at https://github.com/redhat-developer/odo`)
odoExample = ktemplates.Examples(` # Creating and deploying a Node.js project
git clone https://github.com/openshift/nodejs-ex && cd nodejs-ex
%[1]s create nodejs
%[1]s push
# Accessing your Node.js component
%[1]s url create`)
rootUsageTemplate = `Usage:{{if .Runnable}}
{{if .HasAvailableFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
{{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
@@ -54,58 +62,16 @@ Additional help topics:{{range .Commands}}{{if .IsHelpCommand}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: rootCommandName,
Short: "Odo (Openshift Do)",
Long: `Odo (OpenShift Do) is a CLI tool for running OpenShift applications in a fast and automated matter. Odo reduces the complexity of deployment by adding iterative development without the worry of deploying your source code.
Find more information at https://github.com/redhat-developer/odo`,
Example: ` # Creating and deploying a Node.js project
git clone https://github.com/openshift/nodejs-ex && cd nodejs-ex
odo create nodejs
odo push
# Accessing your Node.js component
odo url create`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
},
}
// RootCmd exposes the root command to main package to allow inspection by completion code
func RootCmd() *cobra.Command {
return rootCmd
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
// checking the value of updatenotification in config
// before proceeding with fetching the latest version
cfg, err := config.New()
if err != nil {
util.CheckError(err, "")
func NewCmdOdo(name, fullName string) *cobra.Command {
// rootCmd represents the base command when called without any subcommands
rootCmd := &cobra.Command{
Use: name,
Short: "Odo (OpenShift Do)",
Long: odoLong,
Example: fmt.Sprintf(odoExample, fullName),
}
if cfg.GetUpdateNotification() == true {
updateInfo := make(chan string)
go version.GetLatestReleaseInfo(updateInfo)
util.CheckError(rootCmd.Execute(), "")
select {
case message := <-updateInfo:
fmt.Println(message)
default:
glog.V(4).Info("Could not get the latest release information in time. Never mind, exiting gracefully :)")
}
} else {
util.CheckError(rootCmd.Execute(), "")
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
@@ -144,7 +110,6 @@ func init() {
utils.NewCmdUtils(),
version.NewCmdVersion(),
)
}
// rootCommandName is the name of the root command
const rootCommandName = "odo"
return rootCmd
}