本文整理汇总了Golang中github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util.PrinterForCommand函数的典型用法代码示例。如果您正苦于以下问题:Golang PrinterForCommand函数的具体用法?Golang PrinterForCommand怎么用?Golang PrinterForCommand使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PrinterForCommand函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: PrinterForMapping
// PrinterForMapping returns a printer suitable for displaying the provided resource type.
// Requires that printer flags have been added to cmd (see AddPrinterFlags).
func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.ResourcePrinter, error) {
printer, ok, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
return nil, err
}
if ok {
clientConfig, err := f.ClientConfig()
if err != nil {
return nil, err
}
defaultVersion := clientConfig.Version
version := cmdutil.OutputVersion(cmd, defaultVersion)
if len(version) == 0 {
version = mapping.APIVersion
}
if len(version) == 0 {
return nil, fmt.Errorf("you must specify an output-version when using this output format")
}
printer = kubectl.NewVersionedPrinter(printer, mapping.ObjectConvertor, version)
} else {
printer, err = f.Printer(mapping, cmdutil.GetFlagBool(cmd, "no-headers"))
if err != nil {
return nil, err
}
}
return printer, nil
}
示例2: Complete
func (n *NodeOptions) Complete(f *clientcmd.Factory, c *cobra.Command, args []string, out io.Writer) error {
defaultNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
_, kc, err := f.Clients()
if err != nil {
return err
}
cmdPrinter, output, err := kcmdutil.PrinterForCommand(c)
if err != nil {
return err
}
mapper, typer := f.Object()
n.DefaultNamespace = defaultNamespace
n.Kclient = kc
n.Writer = out
n.Mapper = mapper
n.Typer = typer
n.RESTClientFactory = f.Factory.RESTClient
n.Printer = f.Printer
n.NodeNames = []string{}
n.CmdPrinter = cmdPrinter
n.CmdPrinterOutput = false
if output {
n.CmdPrinterOutput = true
}
if len(args) != 0 {
n.NodeNames = append(n.NodeNames, args...)
}
return nil
}
示例3: NewCmdConfigView
func NewCmdConfigView(out io.Writer, pathOptions *pathOptions) *cobra.Command {
options := &viewOptions{pathOptions: pathOptions}
cmd := &cobra.Command{
Use: "view",
Short: "displays merged .kubeconfig settings or a specified .kubeconfig file.",
Long: view_long,
Example: view_example,
Run: func(cmd *cobra.Command, args []string) {
options.complete()
printer, _, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
glog.FatalDepth(1, err)
}
config, err := options.loadConfig()
if err != nil {
glog.FatalDepth(1, err)
}
err = printer.PrintObj(config, out)
if err != nil {
glog.FatalDepth(1, err)
}
},
}
cmdutil.AddPrinterFlags(cmd)
// Default to yaml
cmd.Flags().Set("output", "yaml")
options.merge.Default(true)
cmd.Flags().Var(&options.merge, "merge", "merge together the full hierarchy of .kubeconfig files")
return cmd
}
示例4: previewConfiguration
// Preview the configuration if required - returns true|false and errors.
func previewConfiguration(c *ipfailover.Configurator, cmd *cobra.Command, out io.Writer) (bool, error) {
p, output, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
return true, fmt.Errorf("Error configuring printer: %v", err)
}
// Check if we are outputting info.
if !output {
return false, nil
}
configList, err := c.Generate()
if err != nil {
return true, fmt.Errorf("Error generating config: %v", err)
}
if err := p.PrintObj(configList, out); err != nil {
return true, fmt.Errorf("Unable to print object: %v", err)
}
return true, nil
}
示例5: NewCmdConfigView
func NewCmdConfigView(out io.Writer, ConfigAccess ConfigAccess) *cobra.Command {
options := &ViewOptions{ConfigAccess: ConfigAccess}
cmd := &cobra.Command{
Use: "view",
Short: "displays Merged kubeconfig settings or a specified kubeconfig file.",
Long: view_long,
Example: view_example,
Run: func(cmd *cobra.Command, args []string) {
options.Complete()
printer, _, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
glog.FatalDepth(1, err)
}
version := cmdutil.OutputVersion(cmd, latest.Version)
printer = kubectl.NewVersionedPrinter(printer, clientcmdapi.Scheme, version)
if err := options.Run(out, printer); err != nil {
glog.FatalDepth(1, err)
}
},
}
cmdutil.AddPrinterFlags(cmd)
// Default to yaml
cmd.Flags().Set("output", "yaml")
options.Merge.Default(true)
cmd.Flags().Var(&options.Merge, "merge", "merge together the full hierarchy of kubeconfig files")
cmd.Flags().BoolVar(&options.RawByteData, "raw", false, "display raw byte data")
cmd.Flags().BoolVar(&options.Flatten, "flatten", false, "flatten the resulting kubeconfig file into self contained output (useful for creating portable kubeconfig files)")
cmd.Flags().BoolVar(&options.Minify, "minify", false, "remove all information not used by current-context from the output")
return cmd
}
示例6: RunCmdRegistry
// RunCmdRegistry contains all the necessary functionality for the OpenShift cli registry command
func RunCmdRegistry(f *clientcmd.Factory, cmd *cobra.Command, out io.Writer, cfg *RegistryConfig, args []string) error {
var name string
switch len(args) {
case 0:
name = "docker-registry"
default:
return cmdutil.UsageError(cmd, "No arguments are allowed to this command")
}
ports, err := app.ContainerPortsFromString(cfg.Ports)
if err != nil {
return err
}
label := map[string]string{
"docker-registry": "default",
}
if cfg.Labels != defaultLabel {
valid, remove, err := app.LabelsFromSpec(strings.Split(cfg.Labels, ","))
if err != nil {
return err
}
if len(remove) > 0 {
return cmdutil.UsageError(cmd, "You may not pass negative labels in %q", cfg.Labels)
}
label = valid
}
nodeSelector := map[string]string{}
if len(cfg.Selector) > 0 {
valid, remove, err := app.LabelsFromSpec(strings.Split(cfg.Selector, ","))
if err != nil {
return err
}
if len(remove) > 0 {
return cmdutil.UsageError(cmd, "You may not pass negative labels in selector %q", cfg.Selector)
}
nodeSelector = valid
}
image := cfg.ImageTemplate.ExpandOrDie(cfg.Type)
namespace, _, err := f.OpenShiftClientConfig.Namespace()
if err != nil {
return fmt.Errorf("error getting client: %v", err)
}
_, kClient, err := f.Clients()
if err != nil {
return fmt.Errorf("error getting client: %v", err)
}
p, output, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
return fmt.Errorf("unable to configure printer: %v", err)
}
generate := output
if !generate {
_, err = kClient.Services(namespace).Get(name)
if err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("can't check for existing docker-registry %q: %v", name, err)
}
generate = true
}
}
if generate {
if cfg.DryRun && !output {
return fmt.Errorf("docker-registry %q does not exist (no service).", name)
}
// create new registry
if len(cfg.Credentials) == 0 {
return fmt.Errorf("registry does not exist; you must specify a .kubeconfig file path containing credentials for connecting the registry to the master with --credentials")
}
clientConfigLoadingRules := &kclientcmd.ClientConfigLoadingRules{ExplicitPath: cfg.Credentials}
credentials, err := clientConfigLoadingRules.Load()
if err != nil {
return fmt.Errorf("registry does not exist; the provided credentials %q could not be loaded: %v", cfg.Credentials, err)
}
config, err := kclientcmd.NewDefaultClientConfig(*credentials, &kclientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return fmt.Errorf("registry does not exist; the provided credentials %q could not be used: %v", cfg.Credentials, err)
}
if err := kclient.LoadTLSFiles(config); err != nil {
return fmt.Errorf("registry does not exist; the provided credentials %q could not load certificate info: %v", cfg.Credentials, err)
}
insecure := "false"
if config.Insecure {
insecure = "true"
} else {
if len(config.KeyData) == 0 || len(config.CertData) == 0 {
return fmt.Errorf("registry does not exist; the provided credentials %q are missing the client certificate and/or key", cfg.Credentials)
}
}
env := app.Environment{
"OPENSHIFT_MASTER": config.Host,
//.........这里部分代码省略.........
示例7: RunCmdRouter
// RunCmdRouter contains all the necessary functionality for the OpenShift cli router command
func RunCmdRouter(f *clientcmd.Factory, cmd *cobra.Command, out io.Writer, cfg *RouterConfig, args []string) error {
var name string
switch len(args) {
case 0:
name = "router"
case 1:
name = args[0]
default:
return cmdutil.UsageError(cmd, "You may pass zero or one arguments to provide a name for the router")
}
if len(cfg.StatsUsername) > 0 {
if strings.Contains(cfg.StatsUsername, ":") {
return cmdutil.UsageError(cmd, "username %s must not contain ':'", cfg.StatsUsername)
}
}
ports, err := app.ContainerPortsFromString(cfg.Ports)
if err != nil {
glog.Fatal(err)
}
if cfg.StatsPort > 0 {
ports = append(ports, kapi.ContainerPort{
Name: "stats",
HostPort: cfg.StatsPort,
ContainerPort: cfg.StatsPort,
Protocol: kapi.ProtocolTCP,
})
}
label := map[string]string{"router": name}
if cfg.Labels != defaultLabel {
valid, remove, err := app.LabelsFromSpec(strings.Split(cfg.Labels, ","))
if err != nil {
glog.Fatal(err)
}
if len(remove) > 0 {
return cmdutil.UsageError(cmd, "You may not pass negative labels in %q", cfg.Labels)
}
label = valid
}
nodeSelector := map[string]string{}
if len(cfg.Selector) > 0 {
valid, remove, err := app.LabelsFromSpec(strings.Split(cfg.Selector, ","))
if err != nil {
glog.Fatal(err)
}
if len(remove) > 0 {
return cmdutil.UsageError(cmd, "You may not pass negative labels in selector %q", cfg.Selector)
}
nodeSelector = valid
}
image := cfg.ImageTemplate.ExpandOrDie(cfg.Type)
namespace, err := f.OpenShiftClientConfig.Namespace()
if err != nil {
return fmt.Errorf("error getting client: %v", err)
}
_, kClient, err := f.Clients()
if err != nil {
return fmt.Errorf("error getting client: %v", err)
}
p, output, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
return fmt.Errorf("unable to configure printer: %v", err)
}
generate := output
if !generate {
_, err = kClient.Services(namespace).Get(name)
if err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("can't check for existing router %q: %v", name, err)
}
generate = true
}
}
if generate {
if cfg.DryRun && !output {
return fmt.Errorf("router %q does not exist (no service)", name)
}
// create new router
if len(cfg.Credentials) == 0 {
return fmt.Errorf("router could not be created; you must specify a .kubeconfig file path containing credentials for connecting the router to the master with --credentials")
}
clientConfigLoadingRules := &kclientcmd.ClientConfigLoadingRules{ExplicitPath: cfg.Credentials, Precedence: []string{}}
credentials, err := clientConfigLoadingRules.Load()
if err != nil {
return fmt.Errorf("router could not be created; the provided credentials %q could not be loaded: %v", cfg.Credentials, err)
}
config, err := kclientcmd.NewDefaultClientConfig(*credentials, &kclientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
//.........这里部分代码省略.........
示例8: RunGet
// RunGet implements the generic Get command
// TODO: convert all direct flag accessors to a struct and pass that instead of cmd
func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
selector := cmdutil.GetFlagString(cmd, "selector")
allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces")
mapper, typer := f.Object()
cmdNamespace, _, err := f.DefaultNamespace()
if err != nil {
return err
}
if len(args) == 0 {
fmt.Fprint(out, `
You must specify the type of resource to get. Valid resource types include:
* pods (aka 'po')
* replicationcontrollers (aka 'rc')
* services
* nodes (aka 'no')
* events (aka 'ev')
* secrets
* limits
* persistentVolumes (aka 'pv')
* persistentVolumeClaims (aka 'pvc')
* quota
`)
return errors.New("Required resource not specified.")
}
// handle watch separately since we cannot watch multiple resource types
isWatch, isWatchOnly := cmdutil.GetFlagBool(cmd, "watch"), cmdutil.GetFlagBool(cmd, "watch-only")
if isWatch || isWatchOnly {
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
SelectorParam(selector).
ResourceTypeOrNameArgs(true, args...).
SingleResourceType().
Do()
if err != nil {
return err
}
mapping, err := r.ResourceMapping()
if err != nil {
return err
}
printer, err := f.PrinterForMapping(cmd, mapping, allNamespaces)
if err != nil {
return err
}
obj, err := r.Object()
if err != nil {
return err
}
rv, err := mapping.MetadataAccessor.ResourceVersion(obj)
if err != nil {
return err
}
// print the current object
if !isWatchOnly {
if err := printer.PrintObj(obj, out); err != nil {
return fmt.Errorf("unable to output the provided object: %v", err)
}
}
// print watched changes
w, err := r.Watch(rv)
if err != nil {
return err
}
kubectl.WatchLoop(w, func(e watch.Event) error {
return printer.PrintObj(e.Object, out)
})
return nil
}
b := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
SelectorParam(selector).
ResourceTypeOrNameArgs(true, args...).
ContinueOnError().
Latest()
printer, generic, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
return err
}
if generic {
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
defaultVersion := clientConfig.Version
singular := false
//.........这里部分代码省略.........
示例9: RunGet
// RunGet implements the generic Get command
// TODO: convert all direct flag accessors to a struct and pass that instead of cmd
func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
selector := cmdutil.GetFlagString(cmd, "selector")
mapper, typer := f.Object()
cmdNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
// handle watch separately since we cannot watch multiple resource types
isWatch, isWatchOnly := cmdutil.GetFlagBool(cmd, "watch"), cmdutil.GetFlagBool(cmd, "watch-only")
if isWatch || isWatchOnly {
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
NamespaceParam(cmdNamespace).DefaultNamespace().
SelectorParam(selector).
ResourceTypeOrNameArgs(true, args...).
SingleResourceType().
Do()
if err != nil {
return err
}
mapping, err := r.ResourceMapping()
if err != nil {
return err
}
printer, err := f.PrinterForMapping(cmd, mapping)
if err != nil {
return err
}
obj, err := r.Object()
if err != nil {
return err
}
rv, err := mapping.MetadataAccessor.ResourceVersion(obj)
if err != nil {
return err
}
// print the current object
if !isWatchOnly {
if err := printer.PrintObj(obj, out); err != nil {
return fmt.Errorf("unable to output the provided object: %v", err)
}
}
// print watched changes
w, err := r.Watch(rv)
if err != nil {
return err
}
kubectl.WatchLoop(w, func(e watch.Event) error {
return printer.PrintObj(e.Object, out)
})
return nil
}
b := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
NamespaceParam(cmdNamespace).DefaultNamespace().
SelectorParam(selector).
ResourceTypeOrNameArgs(true, args...).
ContinueOnError().
Latest()
printer, generic, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
return err
}
if generic {
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
defaultVersion := clientConfig.Version
singular := false
r := b.Flatten().Do()
infos, err := r.IntoSingular(&singular).Infos()
if err != nil {
return err
}
// the outermost object will be converted to the output-version, but inner
// objects can use their mappings
version := cmdutil.OutputVersion(cmd, defaultVersion)
obj, err := resource.AsVersionedObject(infos, !singular, version)
if err != nil {
return err
}
return printer.PrintObj(obj, out)
}
// use the default printer for each object
//.........这里部分代码省略.........