本文整理汇总了Golang中k8s/io/kubernetes/pkg/kubectl/cmd/util.GetFlagString函数的典型用法代码示例。如果您正苦于以下问题:Golang GetFlagString函数的具体用法?Golang GetFlagString怎么用?Golang GetFlagString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了GetFlagString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RunStop
func RunStop(f cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer, options *resource.FilenameOptions) error {
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
mapper, typer := f.Object()
r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
ResourceTypeOrNameArgs(false, args...).
FilenameParam(enforceNamespace, options).
SelectorParam(cmdutil.GetFlagString(cmd, "selector")).
SelectAllParam(cmdutil.GetFlagBool(cmd, "all")).
Flatten().
Do()
if r.Err() != nil {
return r.Err()
}
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
gracePeriod := cmdutil.GetFlagInt(cmd, "grace-period")
waitForDeletion := false
if gracePeriod == 0 {
// To preserve backwards compatibility, but prevent accidental data loss, we convert --grace-period=0
// into --grace-period=1 and wait until the object is successfully deleted.
gracePeriod = 1
waitForDeletion = true
}
return ReapResult(r, f, out, false, cmdutil.GetFlagBool(cmd, "ignore-not-found"), cmdutil.GetFlagDuration(cmd, "timeout"), gracePeriod, waitForDeletion, shortOutput, mapper, false)
}
示例2: CreateSecretTLS
// CreateSecretTLS is the implementation of the create secret tls command
func CreateSecretTLS(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error {
name, err := NameFromCommandArgs(cmd, args)
if err != nil {
return err
}
requiredFlags := []string{"cert", "key"}
for _, requiredFlag := range requiredFlags {
if value := cmdutil.GetFlagString(cmd, requiredFlag); len(value) == 0 {
return cmdutil.UsageError(cmd, "flag %s is required", requiredFlag)
}
}
var generator kubectl.StructuredGenerator
switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
case cmdutil.SecretForTLSV1GeneratorName:
generator = &kubectl.SecretForTLSGeneratorV1{
Name: name,
Key: cmdutil.GetFlagString(cmd, "key"),
Cert: cmdutil.GetFlagString(cmd, "cert"),
}
default:
return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))
}
return RunCreateSubcommand(f, cmd, cmdOut, &CreateSubcommandOptions{
Name: name,
StructuredGenerator: generator,
DryRun: cmdutil.GetFlagBool(cmd, "dry-run"),
OutputFormat: cmdutil.GetFlagString(cmd, "output"),
})
}
示例3: validateArguments
func validateArguments(cmd *cobra.Command, filenames, args []string) error {
deploymentKey := cmdutil.GetFlagString(cmd, "deployment-label-key")
image := cmdutil.GetFlagString(cmd, "image")
rollback := cmdutil.GetFlagBool(cmd, "rollback")
if len(deploymentKey) == 0 {
return cmdutil.UsageError(cmd, "--deployment-label-key can not be empty")
}
if len(filenames) > 1 {
return cmdutil.UsageError(cmd, "May only specify a single filename for new controller")
}
if !rollback {
if len(filenames) == 0 && len(image) == 0 {
return cmdutil.UsageError(cmd, "Must specify --filename or --image for new controller")
}
if len(filenames) != 0 && len(image) != 0 {
return cmdutil.UsageError(cmd, "--filename and --image can not both be specified")
}
} else {
if len(filenames) != 0 || len(image) != 0 {
return cmdutil.UsageError(cmd, "Don't specify --filename or --image on rollback")
}
}
if len(args) < 1 {
return cmdutil.UsageError(cmd, "Must specify the controller to update")
}
return nil
}
示例4: CreateRoleBinding
func CreateRoleBinding(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error {
name, err := NameFromCommandArgs(cmd, args)
if err != nil {
return err
}
var generator kubectl.StructuredGenerator
switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
case cmdutil.RoleBindingV1GeneratorName:
generator = &kubectl.RoleBindingGeneratorV1{
Name: name,
ClusterRole: cmdutil.GetFlagString(cmd, "clusterrole"),
Role: cmdutil.GetFlagString(cmd, "role"),
Users: cmdutil.GetFlagStringSlice(cmd, "user"),
Groups: cmdutil.GetFlagStringSlice(cmd, "group"),
ServiceAccounts: cmdutil.GetFlagStringSlice(cmd, "serviceaccount"),
}
default:
return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))
}
return RunCreateSubcommand(f, cmd, cmdOut, &CreateSubcommandOptions{
Name: name,
StructuredGenerator: generator,
DryRun: cmdutil.GetDryRunFlag(cmd),
OutputFormat: cmdutil.GetFlagString(cmd, "output"),
})
}
示例5: RunStop
func RunStop(f *cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer, options *StopOptions) error {
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
cmdTenant, enforceTenant, err := f.DefaultTenant()
if err != nil {
return err
}
mapper, typer := f.Object()
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
TenantParam(cmdTenant).DefaultTenant().
ResourceTypeOrNameArgs(false, args...).
FilenameParam(enforceTenant, enforceNamespace, options.Filenames...).
SelectorParam(cmdutil.GetFlagString(cmd, "selector")).
SelectAllParam(cmdutil.GetFlagBool(cmd, "all")).
Flatten().
Do()
if r.Err() != nil {
return r.Err()
}
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
return ReapResult(r, f, out, false, cmdutil.GetFlagBool(cmd, "ignore-not-found"), cmdutil.GetFlagDuration(cmd, "timeout"), cmdutil.GetFlagInt(cmd, "grace-period"), shortOutput, mapper)
}
示例6: validateArguments
func validateArguments(cmd *cobra.Command, args []string) (deploymentKey, filename, image, oldName string, err error) {
deploymentKey = cmdutil.GetFlagString(cmd, "deployment-label-key")
image = cmdutil.GetFlagString(cmd, "image")
filenames := cmdutil.GetFlagStringSlice(cmd, "filename")
filename = ""
if len(deploymentKey) == 0 {
return "", "", "", "", cmdutil.UsageError(cmd, "--deployment-label-key can not be empty")
}
if len(filenames) > 1 {
return "", "", "", "", cmdutil.UsageError(cmd, "May only specificy a single filename for new controller")
}
if len(filenames) > 0 {
filename = filenames[0]
}
if len(filenames) == 0 && len(image) == 0 {
return "", "", "", "", cmdutil.UsageError(cmd, "Must specify --filename or --image for new controller")
}
if len(filenames) != 0 && len(image) != 0 {
return "", "", "", "", cmdutil.UsageError(cmd, "--filename and --image can not both be specified")
}
if len(args) < 1 {
return "", "", "", "", cmdutil.UsageError(cmd, "Must specify the controller to update")
}
return deploymentKey, filename, image, args[0], nil
}
示例7: Complete
func (o *TriggersOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string) error {
cmdNamespace, explicit, err := f.DefaultNamespace()
if err != nil {
return err
}
if !cmd.Flags().Lookup("from-github").Changed {
o.FromGitHub = nil
}
if !cmd.Flags().Lookup("from-webhook").Changed {
o.FromWebHook = nil
}
if !cmd.Flags().Lookup("from-webhook-allow-env").Changed {
o.FromWebHookAllowEnv = nil
}
if len(o.FromImage) > 0 {
ref, err := imageapi.ParseDockerImageReference(o.FromImage)
if err != nil {
return fmt.Errorf("the value of --from-image does not appear to be a valid reference to an image: %v", err)
}
if len(ref.Registry) > 0 || len(ref.ID) > 0 {
return fmt.Errorf("the value of --from-image must point to an image stream tag on this server")
}
if len(ref.Tag) == 0 {
return fmt.Errorf("the value of --from-image must include the tag you wish to pull from")
}
o.FromImage = ref.NameString()
o.FromImageNamespace = defaultNamespace(ref.Namespace, cmdNamespace)
}
count := o.count()
o.Reset = count == 0 && (o.Auto || o.Manual)
switch {
case count == 0 && !o.Remove && !o.RemoveAll && !o.Auto && !o.Manual:
o.PrintTable = true
case !o.RemoveAll && !o.Auto && !o.Manual:
o.Auto = true
}
mapper, typer := f.Object(false)
o.Builder = resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(explicit, false, o.Filenames...).
SelectorParam(o.Selector).
ResourceTypeOrNameArgs(o.All, args...).
Flatten()
output := kcmdutil.GetFlagString(cmd, "output")
if len(output) != 0 {
o.PrintObject = func(obj runtime.Object) error { return f.PrintObject(cmd, mapper, obj, o.Out) }
}
o.Encoder = f.JSONEncoder()
o.ShortOutput = kcmdutil.GetFlagString(cmd, "output") == "name"
o.Mapper = mapper
return nil
}
示例8: CreateServiceNodePort
// CreateServiceNodePort is the implementation of the create secret docker-registry command
func CreateServiceNodePort(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error {
name, err := NameFromCommandArgs(cmd, args)
if err != nil {
return err
}
var generator kubectl.StructuredGenerator
switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
case cmdutil.ServiceNodePortGeneratorV1Name:
generator = &kubectl.ServiceCommonGeneratorV1{
Name: name,
TCP: cmdutil.GetFlagStringSlice(cmd, "tcp"),
Type: api.ServiceTypeNodePort,
ClusterIP: "",
NodePort: cmdutil.GetFlagInt(cmd, "node-port"),
}
default:
return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))
}
return RunCreateSubcommand(f, cmd, cmdOut, &CreateSubcommandOptions{
Name: name,
StructuredGenerator: generator,
DryRun: cmdutil.GetDryRunFlag(cmd),
OutputFormat: cmdutil.GetFlagString(cmd, "output"),
})
}
示例9: RunNewApplication
// RunNewApplication contains all the necessary functionality for the OpenShift cli new-app command
func RunNewApplication(fullName string, f *clientcmd.Factory, out io.Writer, c *cobra.Command, args []string, config *newcmd.AppConfig) error {
if err := setupAppConfig(f, c, args, config); err != nil {
return err
}
if config.Querying() {
result, err := config.RunQuery(out, c.Out())
if err != nil {
return handleRunError(c, err)
}
if len(cmdutil.GetFlagString(c, "output")) != 0 {
return f.Factory.PrintObject(c, result.List, out)
}
return printHumanReadableQueryResult(result, out, fullName)
}
if err := setAppConfigLabels(c, config); err != nil {
return err
}
result, err := config.RunAll(out, c.Out())
if err != nil {
return handleRunError(c, err)
}
if err := setLabels(config.Labels, result); err != nil {
return err
}
if len(cmdutil.GetFlagString(c, "output")) != 0 {
return f.Factory.PrintObject(c, result.List, out)
}
if err := createObjects(f, out, result); err != nil {
return err
}
hasMissingRepo := false
for _, item := range result.List.Items {
switch t := item.(type) {
case *kapi.Service:
portMappings := "."
if len(t.Spec.Ports) > 0 {
portMappings = fmt.Sprintf(" with port mappings %s.", describeServicePorts(t.Spec))
}
fmt.Fprintf(c.Out(), "Service %q created at %s%s\n", t.Name, t.Spec.ClusterIP, portMappings)
case *buildapi.BuildConfig:
fmt.Fprintf(c.Out(), "Build %q created and started - you can run `%s status` to check the progress.\n", t.Name, fullName)
case *imageapi.ImageStream:
if len(t.Status.DockerImageRepository) == 0 {
if hasMissingRepo {
continue
}
hasMissingRepo = true
fmt.Fprintf(c.Out(), "WARNING: We created an image stream %q, but it does not look like a Docker registry has been integrated with the server. Automatic builds and deployments depend on that integration to detect new images and will not function properly.\n", t.Name)
}
}
}
fmt.Fprintf(c.Out(), "Run '%s status' to view your app.\n", fullName)
return nil
}
示例10: RunReplace
func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *ReplaceOptions) error {
if len(os.Args) > 1 && os.Args[1] == "update" {
printDeprecationWarning("replace", "update")
}
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir"))
if err != nil {
return err
}
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
force := cmdutil.GetFlagBool(cmd, "force")
if len(options.Filenames) == 0 {
return cmdutil.UsageError(cmd, "Must specify --filename to replace")
}
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
if force {
return forceReplace(f, out, cmd, args, shortOutput, options)
}
mapper, typer := f.Object()
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
Schema(schema).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, options.Filenames...).
Flatten().
Do()
err = r.Err()
if err != nil {
return err
}
return r.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
// Serialize the configuration into an annotation.
if err := kubectl.UpdateApplyAnnotation(info); err != nil {
return err
}
// Serialize the object with the annotation applied.
obj, err := resource.NewHelper(info.Client, info.Mapping).Replace(info.Namespace, info.Name, true, info.Object)
if err != nil {
return cmdutil.AddSourceToErr("replacing", info.Source, err)
}
info.Refresh(obj, true)
printObjectSpecificMessage(obj, out)
cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, "replaced")
return nil
})
}
示例11: RunCreate
func RunCreate(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *CreateOptions) error {
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir"))
if err != nil {
return err
}
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
mapper, typer := f.Object()
r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
Schema(schema).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, options.Filenames...).
Flatten().
Do()
err = r.Err()
if err != nil {
return err
}
count := 0
err = r.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
if err := kubectl.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), info, f.JSONEncoder()); err != nil {
return cmdutil.AddSourceToErr("creating", info.Source, err)
}
if cmdutil.ShouldRecord(cmd, info) {
if err := cmdutil.RecordChangeCause(info.Object, f.Command()); err != nil {
return cmdutil.AddSourceToErr("creating", info.Source, err)
}
}
if err := createAndRefresh(info); err != nil {
return cmdutil.AddSourceToErr("creating", info.Source, err)
}
count++
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
if !shortOutput {
printObjectSpecificMessage(info.Object, out)
}
cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, "created")
return nil
})
if err != nil {
return err
}
if count == 0 {
return fmt.Errorf("no objects passed to create")
}
return nil
}
示例12: Complete
func (o *CreateClusterQuotaOptions) Complete(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
if len(args) != 1 {
return fmt.Errorf("NAME is required: %v", args)
}
var labelSelector *unversioned.LabelSelector
labelSelectorString := cmdutil.GetFlagString(cmd, "project-label-selector")
if len(labelSelectorString) > 0 {
var err error
labelSelector, err = unversioned.ParseToLabelSelector(labelSelectorString)
if err != nil {
return err
}
}
annotationSelector, err := parseAnnotationSelector(cmdutil.GetFlagString(cmd, "project-annotation-selector"))
if err != nil {
return err
}
o.ClusterQuota = "aapi.ClusterResourceQuota{
ObjectMeta: kapi.ObjectMeta{Name: args[0]},
Spec: quotaapi.ClusterResourceQuotaSpec{
Selector: quotaapi.ClusterResourceQuotaSelector{
LabelSelector: labelSelector,
AnnotationSelector: annotationSelector,
},
Quota: kapi.ResourceQuotaSpec{
Hard: kapi.ResourceList{},
},
},
}
for _, resourceCount := range cmdutil.GetFlagStringSlice(cmd, "hard") {
tokens := strings.Split(resourceCount, "=")
if len(tokens) != 2 {
return fmt.Errorf("%v must in the form of resource=quantity", resourceCount)
}
quantity, err := resource.ParseQuantity(tokens[1])
if err != nil {
return err
}
o.ClusterQuota.Spec.Quota.Hard[kapi.ResourceName(tokens[0])] = quantity
}
o.Client, _, err = f.Clients()
if err != nil {
return err
}
o.Mapper, _ = f.Object(false)
o.OutputFormat = cmdutil.GetFlagString(cmd, "output")
o.Printer = func(obj runtime.Object, out io.Writer) error {
return f.PrintObject(cmd, o.Mapper, obj, out)
}
return nil
}
示例13: Complete
func (o *LogsOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
containerName := cmdutil.GetFlagString(cmd, "container")
switch len(args) {
case 0:
return cmdutil.UsageError(cmd, "POD is required for logs")
case 1:
o.ResourceArg = args[0]
case 2:
if cmd.Flag("container").Changed {
return cmdutil.UsageError(cmd, "only one of -c, [CONTAINER] arg is allowed")
}
o.ResourceArg = args[0]
containerName = args[1]
default:
return cmdutil.UsageError(cmd, "logs POD [-c CONTAINER]")
}
var err error
o.Namespace, _, err = f.DefaultNamespace()
if err != nil {
return err
}
logOptions := &api.PodLogOptions{
Container: containerName,
Follow: cmdutil.GetFlagBool(cmd, "follow"),
Previous: cmdutil.GetFlagBool(cmd, "previous"),
Timestamps: cmdutil.GetFlagBool(cmd, "timestamps"),
}
if sinceTime := cmdutil.GetFlagString(cmd, "since-time"); len(sinceTime) > 0 {
t, err := api.ParseRFC3339(sinceTime, unversioned.Now)
if err != nil {
return err
}
logOptions.SinceTime = &t
}
if limit := cmdutil.GetFlagInt64(cmd, "limit-bytes"); limit != 0 {
logOptions.LimitBytes = &limit
}
if tail := cmdutil.GetFlagInt64(cmd, "tail"); tail != -1 {
logOptions.TailLines = &tail
}
if sinceSeconds := cmdutil.GetFlagDuration(cmd, "since"); sinceSeconds != 0 {
// round up to the nearest second
sec := int64(math.Ceil(float64(sinceSeconds) / float64(time.Second)))
logOptions.SinceSeconds = &sec
}
o.Options = logOptions
o.Mapper, o.Typer = f.Object()
o.Decoder = f.Decoder(true)
o.ClientMapper = resource.ClientMapperFunc(f.ClientForMapping)
o.LogsForObject = f.LogsForObject
o.Out = out
return nil
}
示例14: CreatePassthroughRoute
// CreatePassthroughRoute implements the behavior to run the create passthrough route command.
func CreatePassthroughRoute(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
oc, kc, err := f.Clients()
if err != nil {
return err
}
ns, _, err := f.DefaultNamespace()
if err != nil {
return err
}
serviceName, err := resolveServiceName(f, kcmdutil.GetFlagString(cmd, "service"))
if err != nil {
return err
}
routeName, err := resolveRouteName(args)
if err != nil {
return err
}
route, err := cmdutil.UnsecuredRoute(kc, ns, routeName, serviceName, kcmdutil.GetFlagString(cmd, "port"))
if err != nil {
return err
}
route.Spec.Host = kcmdutil.GetFlagString(cmd, "hostname")
route.Spec.TLS = new(api.TLSConfig)
route.Spec.TLS.Termination = api.TLSTerminationPassthrough
dryRun := kcmdutil.GetFlagBool(cmd, "dry-run")
actualRoute := route
if !dryRun {
actualRoute, err = oc.Routes(ns).Create(route)
if err != nil {
return err
}
}
mapper, typer := f.Object(false)
resourceMapper := &resource.Mapper{
ObjectTyper: typer,
RESTMapper: mapper,
ClientMapper: resource.ClientMapperFunc(f.ClientForMapping),
}
info, err := resourceMapper.InfoForObject(actualRoute, nil)
if err != nil {
return err
}
created := "created"
if dryRun {
created = "created (DRY RUN)"
}
shortOutput := kcmdutil.GetFlagString(cmd, "output") == "name"
kcmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, created)
return nil
}
示例15: initExample
func initExample(cmdNamespace string, resource string, f *cmdutil.Factory, cmd *cobra.Command) (runtime.Object, error) {
mapper, _ := f.Object()
version, kind, err := mapper.VersionAndKindForResource(resource)
if err != nil {
return nil, err
}
exBuilder := f.ExampleBuilder()
obj, _, err := exBuilder.NewExample(version, kind)
if err != nil {
return nil, err
}
mapping, err := mapper.RESTMapping(kind, version)
if err != nil {
return nil, err
}
err = mapping.SetAPIVersion(obj, version)
if err != nil {
return nil, err
}
err = mapping.SetKind(obj, kind)
if err != nil {
return nil, err
}
err = mapping.SetNamespace(obj, cmdNamespace)
if err != nil {
return nil, err
}
nameFlag := cmdutil.GetFlagString(cmd, "name")
if nameFlag != "" {
err = mapping.SetName(obj, nameFlag)
} else {
err = mapping.SetName(obj, " ")
}
if err != nil {
return nil, err
}
labelsFlag := cmdutil.GetFlagString(cmd, "labels")
if labelsFlag != "" {
parsedLabels, err := kubectl.ParseLabels(labelsFlag)
if err != nil {
return nil, err
}
err = mapping.SetLabels(obj, parsedLabels)
if err != nil {
return nil, err
}
}
return obj, nil
}