k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/flags/types.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package flags
    18  
    19  import (
    20  	"strconv"
    21  	"strings"
    22  	"time"
    23  
    24  	"github.com/spf13/pflag"
    25  )
    26  
    27  var _ pflag.Value = (*stringFlagFunc)(nil)
    28  var _ flagFunc = (*stringFlagFunc)(nil)
    29  var _ pflag.Value = (*intFlagFunc)(nil)
    30  var _ flagFunc = (*intFlagFunc)(nil)
    31  
    32  type flagFunc interface {
    33  	initialize() error
    34  }
    35  
    36  type stringFlagFunc struct {
    37  	valPtr         *string
    38  	initializeFunc func() error
    39  }
    40  
    41  // initialize runs additional parsing function.
    42  func (s *stringFlagFunc) initialize() error {
    43  	return s.initializeFunc()
    44  }
    45  
    46  // String returns default string.
    47  func (s *stringFlagFunc) String() string {
    48  	return ""
    49  }
    50  
    51  // Set handles flag value setting.
    52  func (s *stringFlagFunc) Set(val string) error {
    53  	*s.valPtr = val
    54  	return nil
    55  }
    56  
    57  // Type returns flag type.
    58  func (s *stringFlagFunc) Type() string {
    59  	return "string"
    60  }
    61  
    62  type stringSliceFlagFunc struct {
    63  	valPtr         *[]string
    64  	initializeFunc func() error
    65  }
    66  
    67  // initialize runs additional parsing function.
    68  func (s *stringSliceFlagFunc) initialize() error {
    69  	return s.initializeFunc()
    70  }
    71  
    72  // String returns default string.
    73  func (*stringSliceFlagFunc) String() string {
    74  	return "false"
    75  }
    76  
    77  // Set handles flag value setting.
    78  func (s *stringSliceFlagFunc) Set(val string) error {
    79  	if val != "" {
    80  		*s.valPtr = strings.Split(val, ",")
    81  	} else {
    82  		*s.valPtr = nil
    83  	}
    84  	return nil
    85  }
    86  
    87  // Type returns flag type.
    88  func (*stringSliceFlagFunc) Type() string {
    89  	return "stringSlice"
    90  }
    91  
    92  type intFlagFunc struct {
    93  	valPtr         *int
    94  	initializeFunc func() error
    95  }
    96  
    97  // initialize runs additional parsing function.
    98  func (i *intFlagFunc) initialize() error {
    99  	return i.initializeFunc()
   100  }
   101  
   102  // String returns default string.
   103  func (i *intFlagFunc) String() string {
   104  	return "0"
   105  }
   106  
   107  // Set handles flag value setting.
   108  func (i *intFlagFunc) Set(val string) error {
   109  	iVal, err := strconv.Atoi(val)
   110  	if err != nil {
   111  		return err
   112  	}
   113  	*i.valPtr = iVal
   114  	return nil
   115  }
   116  
   117  // Type returns flag type.
   118  func (i *intFlagFunc) Type() string {
   119  	return "int"
   120  }
   121  
   122  type boolFlagFunc struct {
   123  	valPtr         *bool
   124  	initializeFunc func() error
   125  }
   126  
   127  // initialize runs additional parsing function.
   128  func (b *boolFlagFunc) initialize() error {
   129  	return b.initializeFunc()
   130  }
   131  
   132  // String returns default string.
   133  func (*boolFlagFunc) String() string {
   134  	return "false"
   135  }
   136  
   137  // Set handles flag value setting.
   138  func (b *boolFlagFunc) Set(val string) error {
   139  	bVal, err := strconv.ParseBool(val)
   140  	if err != nil {
   141  		return err
   142  	}
   143  	*b.valPtr = bVal
   144  	return nil
   145  }
   146  
   147  // Type returns flag type.
   148  func (*boolFlagFunc) Type() string {
   149  	return "bool"
   150  }
   151  
   152  type durationFlagFunc struct {
   153  	valPtr         *time.Duration
   154  	initializeFunc func() error
   155  }
   156  
   157  // initialize runs additional parsing function.
   158  func (d *durationFlagFunc) initialize() error {
   159  	return d.initializeFunc()
   160  }
   161  
   162  // String returns default string.
   163  func (*durationFlagFunc) String() string {
   164  	return "0s"
   165  }
   166  
   167  // Set handles flag value setting.
   168  func (d *durationFlagFunc) Set(val string) error {
   169  	dVal, err := time.ParseDuration(val)
   170  	if err != nil {
   171  		return err
   172  	}
   173  	*d.valPtr = dVal
   174  	return nil
   175  }
   176  
   177  // Type returns flag type.
   178  func (*durationFlagFunc) Type() string {
   179  	return "time.Duration"
   180  }