本文整理匯總了Golang中github.com/spf13/pflag.FlagSet.GetString方法的典型用法代碼示例。如果您正苦於以下問題:Golang FlagSet.GetString方法的具體用法?Golang FlagSet.GetString怎麽用?Golang FlagSet.GetString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/spf13/pflag.FlagSet
的用法示例。
在下文中一共展示了FlagSet.GetString方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ParseLogDriverFlags
// ParseLogDriverFlags parses a silly string format for log driver and options.
// Fully baked log driver config should be returned.
//
// If no log driver is available, nil, nil will be returned.
func ParseLogDriverFlags(flags *pflag.FlagSet) (*api.Driver, error) {
if !flags.Changed("log-driver") {
return nil, nil
}
name, err := flags.GetString("log-driver")
if err != nil {
return nil, err
}
var opts map[string]string
if flags.Changed("log-opt") {
rawOpts, err := flags.GetStringSlice("log-opt")
if err != nil {
return nil, err
}
opts = make(map[string]string, len(rawOpts))
for _, rawOpt := range rawOpts {
parts := strings.SplitN(rawOpt, "=", 2)
if len(parts) == 1 {
opts[parts[0]] = ""
continue
}
opts[parts[0]] = parts[1]
}
}
return &api.Driver{
Name: name,
Options: opts,
}, nil
}
示例2: parseContainer
func parseContainer(flags *pflag.FlagSet, spec *api.ServiceSpec) error {
if flags.Changed("image") {
image, err := flags.GetString("image")
if err != nil {
return err
}
spec.Task.GetContainer().Image = image
}
if flags.Changed("command") {
command, err := flags.GetStringSlice("command")
if err != nil {
return err
}
spec.Task.GetContainer().Command = command
}
if flags.Changed("args") {
args, err := flags.GetStringSlice("args")
if err != nil {
return err
}
spec.Task.GetContainer().Args = args
}
if flags.Changed("env") {
env, err := flags.GetStringSlice("env")
if err != nil {
return err
}
spec.Task.GetContainer().Env = env
}
return nil
}
示例3: mergeSwarm
func mergeSwarm(swarm *swarm.Swarm, flags *pflag.FlagSet) error {
spec := &swarm.Spec
if flags.Changed("auto-accept") {
value := flags.Lookup("auto-accept").Value.(*AutoAcceptOption)
if len(spec.AcceptancePolicy.Policies) > 0 {
spec.AcceptancePolicy.Policies = value.Policies(spec.AcceptancePolicy.Policies[0].Secret)
} else {
spec.AcceptancePolicy.Policies = value.Policies("")
}
}
if flags.Changed("secret") {
secret, _ := flags.GetString("secret")
for _, policy := range spec.AcceptancePolicy.Policies {
policy.Secret = secret
}
}
if flags.Changed("task-history-limit") {
spec.Orchestration.TaskHistoryRetentionLimit, _ = flags.GetInt64("task-history-limit")
}
if flags.Changed("dispatcher-heartbeat") {
if v, err := flags.GetDuration("dispatcher-heartbeat"); err == nil {
spec.Dispatcher.HeartbeatPeriod = uint64(v.Nanoseconds())
}
}
return nil
}
示例4: parseUpdate
func parseUpdate(flags *pflag.FlagSet, spec *api.ServiceSpec) error {
if flags.Changed("update-parallelism") {
parallelism, err := flags.GetUint64("update-parallelism")
if err != nil {
return err
}
if spec.Update == nil {
spec.Update = &api.UpdateConfig{}
}
spec.Update.Parallelism = parallelism
}
if flags.Changed("update-delay") {
delay, err := flags.GetString("update-delay")
if err != nil {
return err
}
delayDuration, err := time.ParseDuration(delay)
if err != nil {
return err
}
if spec.Update == nil {
spec.Update = &api.UpdateConfig{}
}
spec.Update.Delay = *ptypes.DurationProto(delayDuration)
}
return nil
}
示例5: parseResourceMemory
func parseResourceMemory(flags *pflag.FlagSet, resources *api.Resources, name string) error {
memory, err := flags.GetString(name)
if err != nil {
return err
}
bytes, err := units.RAMInBytes(memory)
if err != nil {
return err
}
resources.MemoryBytes = bytes
return nil
}
示例6: parseUpdate
func parseUpdate(flags *pflag.FlagSet, spec *api.ServiceSpec) error {
if flags.Changed("update-parallelism") {
parallelism, err := flags.GetUint64("update-parallelism")
if err != nil {
return err
}
if spec.Update == nil {
spec.Update = &api.UpdateConfig{}
}
spec.Update.Parallelism = parallelism
}
if flags.Changed("update-delay") {
delay, err := flags.GetString("update-delay")
if err != nil {
return err
}
delayDuration, err := time.ParseDuration(delay)
if err != nil {
return err
}
if spec.Update == nil {
spec.Update = &api.UpdateConfig{}
}
spec.Update.Delay = delayDuration
}
if flags.Changed("update-on-failure") {
if spec.Update == nil {
spec.Update = &api.UpdateConfig{}
}
action, err := flags.GetString("update-on-failure")
if err != nil {
return err
}
switch action {
case "pause":
spec.Update.FailureAction = api.UpdateConfig_PAUSE
case "continue":
spec.Update.FailureAction = api.UpdateConfig_CONTINUE
default:
return errors.New("--update-on-failure value must be pause or continue")
}
}
return nil
}
示例7: parseResourceCPU
func parseResourceCPU(flags *pflag.FlagSet, resources *api.Resources, name string) error {
cpu, err := flags.GetString(name)
if err != nil {
return err
}
nanoCPUs, ok := new(big.Rat).SetString(cpu)
if !ok {
return fmt.Errorf("invalid cpu: %s", cpu)
}
cpuRat := new(big.Rat).Mul(nanoCPUs, big.NewRat(1e9, 1))
if !cpuRat.IsInt() {
return fmt.Errorf("CPU value cannot have more than 9 decimal places: %s", cpu)
}
resources.NanoCPUs = cpuRat.Num().Int64()
return nil
}
示例8: updateHealthcheck
func updateHealthcheck(flags *pflag.FlagSet, containerSpec *swarm.ContainerSpec) error {
if !anyChanged(flags, flagNoHealthcheck, flagHealthCmd, flagHealthInterval, flagHealthRetries, flagHealthTimeout) {
return nil
}
if containerSpec.Healthcheck == nil {
containerSpec.Healthcheck = &container.HealthConfig{}
}
noHealthcheck, err := flags.GetBool(flagNoHealthcheck)
if err != nil {
return err
}
if noHealthcheck {
if !anyChanged(flags, flagHealthCmd, flagHealthInterval, flagHealthRetries, flagHealthTimeout) {
containerSpec.Healthcheck = &container.HealthConfig{
Test: []string{"NONE"},
}
return nil
}
return fmt.Errorf("--%s conflicts with --health-* options", flagNoHealthcheck)
}
if len(containerSpec.Healthcheck.Test) > 0 && containerSpec.Healthcheck.Test[0] == "NONE" {
containerSpec.Healthcheck.Test = nil
}
if flags.Changed(flagHealthInterval) {
val := *flags.Lookup(flagHealthInterval).Value.(*PositiveDurationOpt).Value()
containerSpec.Healthcheck.Interval = val
}
if flags.Changed(flagHealthTimeout) {
val := *flags.Lookup(flagHealthTimeout).Value.(*PositiveDurationOpt).Value()
containerSpec.Healthcheck.Timeout = val
}
if flags.Changed(flagHealthRetries) {
containerSpec.Healthcheck.Retries, _ = flags.GetInt(flagHealthRetries)
}
if flags.Changed(flagHealthCmd) {
cmd, _ := flags.GetString(flagHealthCmd)
if cmd != "" {
containerSpec.Healthcheck.Test = []string{"CMD-SHELL", cmd}
} else {
containerSpec.Healthcheck.Test = nil
}
}
return nil
}
示例9: mergeNodeUpdate
func mergeNodeUpdate(flags *pflag.FlagSet) func(*swarm.Node) error {
return func(node *swarm.Node) error {
spec := &node.Spec
if flags.Changed(flagRole) {
str, err := flags.GetString(flagRole)
if err != nil {
return err
}
spec.Role = swarm.NodeRole(str)
}
if flags.Changed(flagAvailability) {
str, err := flags.GetString(flagAvailability)
if err != nil {
return err
}
spec.Availability = swarm.NodeAvailability(str)
}
if spec.Annotations.Labels == nil {
spec.Annotations.Labels = make(map[string]string)
}
if flags.Changed(flagLabelAdd) {
labels := flags.Lookup(flagLabelAdd).Value.(*opts.ListOpts).GetAll()
for k, v := range runconfigopts.ConvertKVStringsToMap(labels) {
spec.Annotations.Labels[k] = v
}
}
if flags.Changed(flagLabelRemove) {
keys := flags.Lookup(flagLabelRemove).Value.(*opts.ListOpts).GetAll()
for _, k := range keys {
// if a key doesn't exist, fail the command explicitly
if _, exists := spec.Annotations.Labels[k]; !exists {
return fmt.Errorf("key %s doesn't exist in node's labels", k)
}
delete(spec.Annotations.Labels, k)
}
}
return nil
}
}
示例10: mergeMode
func mergeMode(flags *pflag.FlagSet, serviceMode *swarm.ServiceMode) error {
if !flags.Changed("mode") && !flags.Changed("scale") {
return nil
}
var mode string
if flags.Changed("mode") {
mode, _ = flags.GetString("mode")
}
if !(mode == "replicated" || serviceMode.Replicated != nil) && flags.Changed("replicas") {
return fmt.Errorf("replicas can only be used with replicated mode")
}
if mode == "global" {
serviceMode.Replicated = nil
serviceMode.Global = &swarm.GlobalService{}
return nil
}
if flags.Changed("replicas") {
replicas := flags.Lookup("replicas").Value.(*Uint64Opt).Value()
serviceMode.Replicated = &swarm.ReplicatedService{Replicas: replicas}
serviceMode.Global = nil
return nil
}
if mode == "replicated" {
if serviceMode.Replicated != nil {
return nil
}
serviceMode.Replicated = &swarm.ReplicatedService{Replicas: &DefaultReplicas}
serviceMode.Global = nil
}
return nil
}
示例11: mergeNodeUpdate
func mergeNodeUpdate(flags *pflag.FlagSet) func(*swarm.Node) {
return func(node *swarm.Node) {
mergeString := func(flag string, field *string) {
if flags.Changed(flag) {
*field, _ = flags.GetString(flag)
}
}
mergeRole := func(flag string, field *swarm.NodeRole) {
if flags.Changed(flag) {
str, _ := flags.GetString(flag)
*field = swarm.NodeRole(str)
}
}
mergeMembership := func(flag string, field *swarm.NodeMembership) {
if flags.Changed(flag) {
str, _ := flags.GetString(flag)
*field = swarm.NodeMembership(str)
}
}
mergeAvailability := func(flag string, field *swarm.NodeAvailability) {
if flags.Changed(flag) {
str, _ := flags.GetString(flag)
*field = swarm.NodeAvailability(str)
}
}
mergeLabels := func(flag string, field *map[string]string) {
if flags.Changed(flag) {
values, _ := flags.GetStringSlice(flag)
for key, value := range runconfigopts.ConvertKVStringsToMap(values) {
(*field)[key] = value
}
}
}
spec := &node.Spec
mergeString("name", &spec.Name)
// TODO: setting labels is not working
mergeLabels("label", &spec.Labels)
mergeRole("role", &spec.Role)
mergeMembership("membership", &spec.Membership)
mergeAvailability("availability", &spec.Availability)
}
}
示例12: parseMode
func parseMode(flags *pflag.FlagSet, spec *api.ServiceSpec) error {
if flags.Changed("mode") {
mode, err := flags.GetString("mode")
if err != nil {
return err
}
switch mode {
case "global":
if spec.GetGlobal() == nil {
spec.Mode = &api.ServiceSpec_Global{
Global: &api.GlobalService{},
}
}
case "replicated":
if spec.GetReplicated() == nil {
spec.Mode = &api.ServiceSpec_Replicated{
Replicated: &api.ReplicatedService{},
}
}
}
}
if flags.Changed("replicas") {
if spec.GetReplicated() == nil {
return fmt.Errorf("--replicas can only be specified in --mode replicated")
}
replicas, err := flags.GetUint64("replicas")
if err != nil {
return err
}
spec.GetReplicated().Replicas = replicas
}
return nil
}
示例13: mergeSwarm
func mergeSwarm(swarm *swarm.Swarm, flags *pflag.FlagSet) error {
spec := &swarm.Spec
if flags.Changed("auto-accept") {
value := flags.Lookup("auto-accept").Value.(*AutoAcceptOption)
spec.AcceptancePolicy.Policies = value.Policies(nil)
}
var psecret *string
if flags.Changed("secret") {
secret, _ := flags.GetString("secret")
psecret = &secret
}
for i := range spec.AcceptancePolicy.Policies {
spec.AcceptancePolicy.Policies[i].Secret = psecret
}
if flags.Changed("task-history-limit") {
spec.Orchestration.TaskHistoryRetentionLimit, _ = flags.GetInt64("task-history-limit")
}
if flags.Changed("dispatcher-heartbeat") {
if v, err := flags.GetDuration("dispatcher-heartbeat"); err == nil {
spec.Dispatcher.HeartbeatPeriod = uint64(v.Nanoseconds())
}
}
if flags.Changed("cert-expiry") {
if v, err := flags.GetDuration("cert-expiry"); err == nil {
spec.CAConfig.NodeCertExpiry = v
}
}
return nil
}
示例14: mergeNodeUpdate
func mergeNodeUpdate(flags *pflag.FlagSet) func(*swarm.Node) {
return func(node *swarm.Node) {
spec := &node.Spec
if flags.Changed(flagRole) {
str, _ := flags.GetString(flagRole)
spec.Role = swarm.NodeRole(str)
}
if flags.Changed(flagMembership) {
str, _ := flags.GetString(flagMembership)
spec.Membership = swarm.NodeMembership(str)
}
if flags.Changed(flagAvailability) {
str, _ := flags.GetString(flagAvailability)
spec.Availability = swarm.NodeAvailability(str)
}
}
}
示例15: mergeService
func mergeService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
mergeString := func(flag string, field *string) {
if flags.Changed(flag) {
*field, _ = flags.GetString(flag)
}
}
mergeListOpts := func(flag string, field *[]string) {
if flags.Changed(flag) {
value := flags.Lookup(flag).Value.(*opts.ListOpts)
*field = value.GetAll()
}
}
mergeSlice := func(flag string, field *[]string) {
if flags.Changed(flag) {
*field, _ = flags.GetStringSlice(flag)
}
}
mergeInt64Value := func(flag string, field *int64) {
if flags.Changed(flag) {
*field = flags.Lookup(flag).Value.(int64Value).Value()
}
}
mergeDuration := func(flag string, field *time.Duration) {
if flags.Changed(flag) {
*field, _ = flags.GetDuration(flag)
}
}
mergeDurationOpt := func(flag string, field *time.Duration) {
if flags.Changed(flag) {
*field = *flags.Lookup(flag).Value.(*DurationOpt).Value()
}
}
mergeUint64 := func(flag string, field *uint64) {
if flags.Changed(flag) {
*field, _ = flags.GetUint64(flag)
}
}
mergeUint64Opt := func(flag string, field *uint64) {
if flags.Changed(flag) {
*field = *flags.Lookup(flag).Value.(*Uint64Opt).Value()
}
}
cspec := &spec.TaskTemplate.ContainerSpec
task := &spec.TaskTemplate
mergeString("name", &spec.Name)
mergeLabels(flags, &spec.Labels)
mergeString("image", &cspec.Image)
mergeSlice("command", &cspec.Command)
mergeSlice("arg", &cspec.Command)
mergeListOpts("env", &cspec.Env)
mergeString("workdir", &cspec.Dir)
mergeString("user", &cspec.User)
mergeMounts(flags, &cspec.Mounts)
mergeInt64Value("limit-cpu", &task.Resources.Limits.NanoCPUs)
mergeInt64Value("limit-memory", &task.Resources.Limits.MemoryBytes)
mergeInt64Value("reserve-cpu", &task.Resources.Reservations.NanoCPUs)
mergeInt64Value("reserve-memory", &task.Resources.Reservations.MemoryBytes)
mergeDurationOpt("stop-grace-period", cspec.StopGracePeriod)
if flags.Changed("restart-policy-condition") {
value, _ := flags.GetString("restart-policy-condition")
task.RestartPolicy.Condition = swarm.RestartPolicyCondition(value)
}
mergeDurationOpt("restart-policy-delay", task.RestartPolicy.Delay)
mergeUint64Opt("restart-policy-max-attempts", task.RestartPolicy.MaxAttempts)
mergeDurationOpt("restart-policy-window", task.RestartPolicy.Window)
mergeSlice("constraint", &task.Placement.Constraints)
if err := mergeMode(flags, &spec.Mode); err != nil {
return err
}
mergeUint64("updateconfig-parallelism", &spec.UpdateConfig.Parallelism)
mergeDuration("updateconfig-delay", &spec.UpdateConfig.Delay)
mergeNetworks(flags, &spec.Networks)
if flags.Changed("endpoint-mode") {
value, _ := flags.GetString("endpoint-mode")
spec.EndpointSpec.Mode = swarm.ResolutionMode(value)
}
mergePorts(flags, &spec.EndpointSpec.Ports)
return nil
}