func (templater *templater) UsageFunc(exposedFlags ...string) func(*cobra.Command) error { return func(c *cobra.Command) error { t := template.New("usage") t.Funcs(templater.templateFuncs(exposedFlags...)) template.Must(t.Parse(templater.UsageTemplate)) out := term.NewResponsiveWriter(c.OutOrStderr()) return t.Execute(out, c) } }
// CommandFor returns the appropriate command for this base name, // or the global OpenShift command func CommandFor(basename string) *cobra.Command { var cmd *cobra.Command in, out, errout := os.Stdin, term.NewResponsiveWriter(os.Stdout), os.Stderr // Make case-insensitive and strip executable suffix if present if runtime.GOOS == "windows" { basename = strings.ToLower(basename) basename = strings.TrimSuffix(basename, ".exe") } switch basename { case "openshift-router": cmd = irouter.NewCommandTemplateRouter(basename) case "openshift-f5-router": cmd = irouter.NewCommandF5Router(basename) case "openshift-deploy": cmd = deployer.NewCommandDeployer(basename) case "openshift-recycle": cmd = recycle.NewCommandRecycle(basename, out) case "openshift-sti-build": cmd = builder.NewCommandS2IBuilder(basename) case "openshift-docker-build": cmd = builder.NewCommandDockerBuilder(basename) case "oc", "osc": cmd = cli.NewCommandCLI(basename, basename, in, out, errout) case "oadm", "osadm": cmd = admin.NewCommandAdmin(basename, basename, in, out, errout) case "kubectl": cmd = cli.NewCmdKubectl(basename, out) case "kube-apiserver": cmd = kubernetes.NewAPIServerCommand(basename, basename, out) case "kube-controller-manager": cmd = kubernetes.NewControllersCommand(basename, basename, out) case "kubelet": cmd = kubernetes.NewKubeletCommand(basename, basename, out) case "kube-proxy": cmd = kubernetes.NewProxyCommand(basename, basename, out) case "kube-scheduler": cmd = kubernetes.NewSchedulerCommand(basename, basename, out) case "kubernetes": cmd = kubernetes.NewCommand(basename, basename, out) case "origin", "atomic-enterprise": cmd = NewCommandOpenShift(basename) default: cmd = NewCommandOpenShift("openshift") } if cmd.UsageFunc() == nil { templates.ActsAsRootCommand(cmd, []string{"options"}) } flagtypes.GLog(cmd.PersistentFlags()) return cmd }
func (templater *templater) HelpFunc() func(*cobra.Command, []string) { return func(c *cobra.Command, s []string) { t := template.New("help") t.Funcs(templater.templateFuncs()) template.Must(t.Parse(templater.HelpTemplate)) out := term.NewResponsiveWriter(c.OutOrStdout()) err := t.Execute(out, c) if err != nil { c.Println(err) } } }
// NewCommandOpenShift creates the standard OpenShift command func NewCommandOpenShift(name string) *cobra.Command { in, out, errout := os.Stdin, term.NewResponsiveWriter(os.Stdout), os.Stderr root := &cobra.Command{ Use: name, Short: "Build, deploy, and manage your cloud applications", Long: fmt.Sprintf(openshiftLong, name, cmdutil.GetPlatformName(name), cmdutil.GetDistributionName(name)), Run: kcmdutil.DefaultSubCommandRun(out), } f := clientcmd.New(pflag.NewFlagSet("", pflag.ContinueOnError)) startAllInOne, _ := start.NewCommandStartAllInOne(name, out, errout) root.AddCommand(startAllInOne) root.AddCommand(admin.NewCommandAdmin("admin", name+" admin", in, out, errout)) root.AddCommand(cli.NewCommandCLI("cli", name+" cli", in, out, errout)) root.AddCommand(cli.NewCmdKubectl("kube", out)) root.AddCommand(newExperimentalCommand("ex", name+" ex")) root.AddCommand(newCompletionCommand("completion", name+" completion")) root.AddCommand(cmd.NewCmdVersion(name, f, out, cmd.VersionOptions{PrintEtcdVersion: true, IsServer: true})) // infra commands are those that are bundled with the binary but not displayed to end users // directly infra := &cobra.Command{ Use: "infra", // Because this command exposes no description, it will not be shown in help } infra.AddCommand( irouter.NewCommandTemplateRouter("router"), irouter.NewCommandF5Router("f5-router"), deployer.NewCommandDeployer("deploy"), recycle.NewCommandRecycle("recycle", out), builder.NewCommandS2IBuilder("sti-build"), builder.NewCommandDockerBuilder("docker-build"), diagnostics.NewCommandPodDiagnostics("diagnostic-pod", out), diagnostics.NewCommandNetworkPodDiagnostics("network-diagnostic-pod", out), ) root.AddCommand(infra) root.AddCommand(cmd.NewCmdOptions(out)) // TODO: add groups templates.ActsAsRootCommand(root, []string{"options"}) return root }
func NewCommandCLI(name, fullName string, in io.Reader, out, errout io.Writer) *cobra.Command { // Main command cmds := &cobra.Command{ Use: name, Short: "Command line tools for managing applications", Long: cliLong, Run: func(c *cobra.Command, args []string) { explainOut := term.NewResponsiveWriter(out) c.SetOutput(explainOut) kcmdutil.RequireNoArguments(c, args) fmt.Fprintf(explainOut, "%s\n\n%s\n", cliLong, fmt.Sprintf(cliExplain, fullName)) }, BashCompletionFunction: bashCompletionFunc, } f := clientcmd.New(cmds.PersistentFlags()) loginCmd := login.NewCmdLogin(fullName, f, in, out) secretcmds := secrets.NewCmdSecrets(secrets.SecretsRecommendedName, fullName+" "+secrets.SecretsRecommendedName, f, in, out, errout, fullName+" edit") groups := templates.CommandGroups{ { Message: "Basic Commands:", Commands: []*cobra.Command{ cmd.NewCmdTypes(fullName, f, out), loginCmd, cmd.NewCmdRequestProject(cmd.RequestProjectRecommendedCommandName, fullName, f, out, errout), cmd.NewCmdNewApplication(cmd.NewAppRecommendedCommandName, fullName, f, out, errout), cmd.NewCmdStatus(cmd.StatusRecommendedName, fullName, fullName+" "+cmd.StatusRecommendedName, f, out), cmd.NewCmdProject(fullName+" project", f, out), cmd.NewCmdProjects(fullName, f, out), cmd.NewCmdExplain(fullName, f, out, errout), cluster.NewCmdCluster(cluster.ClusterRecommendedName, fullName+" "+cluster.ClusterRecommendedName, f, out, errout), cmd.NewCmdIdle(fullName, f, out, errout), }, }, { Message: "Build and Deploy Commands:", Commands: []*cobra.Command{ rollout.NewCmdRollout(fullName, f, out, errout), cmd.NewCmdDeploy(fullName, f, out), cmd.NewCmdRollback(fullName, f, out), cmd.NewCmdNewBuild(cmd.NewBuildRecommendedCommandName, fullName, f, in, out, errout), cmd.NewCmdStartBuild(fullName, f, in, out, errout), cmd.NewCmdCancelBuild(cmd.CancelBuildRecommendedCommandName, fullName, f, in, out, errout), cmd.NewCmdImportImage(fullName, f, out, errout), cmd.NewCmdTag(fullName, f, out), }, }, { Message: "Application Management Commands:", Commands: []*cobra.Command{ cmd.NewCmdGet(fullName, f, out, errout), cmd.NewCmdDescribe(fullName, f, out, errout), cmd.NewCmdEdit(fullName, f, out, errout), set.NewCmdSet(fullName, f, in, out, errout), cmd.NewCmdLabel(fullName, f, out), cmd.NewCmdAnnotate(fullName, f, out), cmd.NewCmdExpose(fullName, f, out), cmd.NewCmdDelete(fullName, f, out), cmd.NewCmdScale(fullName, f, out), cmd.NewCmdAutoscale(fullName, f, out), secretcmds, sa.NewCmdServiceAccounts(sa.ServiceAccountsRecommendedName, fullName+" "+sa.ServiceAccountsRecommendedName, f, out, errout), }, }, { Message: "Troubleshooting and Debugging Commands:", Commands: []*cobra.Command{ cmd.NewCmdLogs(cmd.LogsRecommendedCommandName, fullName, f, out), cmd.NewCmdRsh(cmd.RshRecommendedName, fullName, f, in, out, errout), rsync.NewCmdRsync(rsync.RsyncRecommendedName, fullName, f, out, errout), cmd.NewCmdPortForward(fullName, f, out, errout), cmd.NewCmdDebug(fullName, f, in, out, errout), cmd.NewCmdExec(fullName, f, in, out, errout), cmd.NewCmdProxy(fullName, f, out), cmd.NewCmdAttach(fullName, f, in, out, errout), cmd.NewCmdRun(fullName, f, in, out, errout), }, }, { Message: "Advanced Commands:", Commands: []*cobra.Command{ admin.NewCommandAdmin("adm", fullName+" "+"adm", in, out, errout), cmd.NewCmdCreate(fullName, f, out, errout), cmd.NewCmdReplace(fullName, f, out), cmd.NewCmdApply(fullName, f, out), cmd.NewCmdPatch(fullName, f, out), cmd.NewCmdProcess(fullName, f, out, errout), cmd.NewCmdExport(fullName, f, in, out), cmd.NewCmdExtract(fullName, f, in, out, errout), observe.NewCmdObserve(fullName, f, out, errout), policy.NewCmdPolicy(policy.PolicyRecommendedName, fullName+" "+policy.PolicyRecommendedName, f, out, errout), cmd.NewCmdConvert(fullName, f, out), importer.NewCmdImport(fullName, f, in, out, errout), }, }, { Message: "Settings Commands:", Commands: []*cobra.Command{ login.NewCmdLogout("logout", fullName+" logout", fullName+" login", f, in, out), cmd.NewCmdConfig(fullName, "config", out, errout), cmd.NewCmdWhoAmI(cmd.WhoAmIRecommendedCommandName, fullName+" "+cmd.WhoAmIRecommendedCommandName, f, out), cmd.NewCmdCompletion(fullName, f, out), }, }, } groups.Add(cmds) filters := []string{ "options", // These commands are deprecated and should not appear in help moved(fullName, "set env", cmds, set.NewCmdEnv(fullName, f, in, out, errout)), moved(fullName, "set volume", cmds, set.NewCmdVolume(fullName, f, out, errout)), moved(fullName, "logs", cmds, cmd.NewCmdBuildLogs(fullName, f, out)), moved(fullName, "secrets link", secretcmds, secrets.NewCmdLinkSecret("add", fullName, f.Factory, out)), } changeSharedFlagDefaults(cmds) templates.ActsAsRootCommand(cmds, filters, groups...). ExposeFlags(loginCmd, "certificate-authority", "insecure-skip-tls-verify", "token") // experimental commands are those that are bundled with the binary but not displayed to end users // directly experimental := &cobra.Command{ Use: "ex", // Because this command exposes no description, it will not be shown in help } experimental.AddCommand( dockerbuild.NewCmdDockerbuild(fullName, f, out, errout), ) cmds.AddCommand(experimental) if name == fullName { cmds.AddCommand(cmd.NewCmdVersion(fullName, f, out, cmd.VersionOptions{PrintClientFeatures: true})) } cmds.AddCommand(cmd.NewCmdOptions(out)) return cmds }