github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/varlinkapi/intermediate.go (about)

     1  package varlinkapi
     2  
     3  import (
     4  	"github.com/sirupsen/logrus"
     5  )
     6  
     7  /*
     8  attention
     9  
    10  in this file you will see a lot of struct duplication.  this was done because people wanted a strongly typed
    11  varlink mechanism.  this resulted in us creating this intermediate layer that allows us to take the input
    12  from the cli and make an intermediate layer which can be transferred as strongly typed structures over a varlink
    13  interface.
    14  
    15  we intentionally avoided heavy use of reflection here because we were concerned about performance impacts to the
    16  non-varlink intermediate layer generation.
    17  */
    18  
    19  // GenericCLIResult describes the overall interface for dealing with
    20  // the create command cli in both local and remote uses
    21  type GenericCLIResult interface {
    22  	IsSet() bool
    23  	Name() string
    24  	Value() interface{}
    25  }
    26  
    27  // CRStringSlice describes a string slice cli struct
    28  type CRStringSlice struct {
    29  	Val []string
    30  	createResult
    31  }
    32  
    33  // CRString describes a string cli struct
    34  type CRString struct {
    35  	Val string
    36  	createResult
    37  }
    38  
    39  // CRUint64 describes a uint64 cli struct
    40  type CRUint64 struct {
    41  	Val uint64
    42  	createResult
    43  }
    44  
    45  // CRFloat64 describes a float64 cli struct
    46  type CRFloat64 struct {
    47  	Val float64
    48  	createResult
    49  }
    50  
    51  //CRBool describes a bool cli struct
    52  type CRBool struct {
    53  	Val bool
    54  	createResult
    55  }
    56  
    57  // CRInt64 describes an int64 cli struct
    58  type CRInt64 struct {
    59  	Val int64
    60  	createResult
    61  }
    62  
    63  // CRUint describes a uint cli struct
    64  type CRUint struct {
    65  	Val uint
    66  	createResult
    67  }
    68  
    69  // CRInt describes an int cli struct
    70  type CRInt struct {
    71  	Val int
    72  	createResult
    73  }
    74  
    75  // CRStringArray describes a stringarray cli struct
    76  type CRStringArray struct {
    77  	Val []string
    78  	createResult
    79  }
    80  
    81  type createResult struct {
    82  	Flag    string
    83  	Changed bool
    84  }
    85  
    86  // GenericCLIResults in the intermediate object between the cobra cli
    87  // and createconfig
    88  type GenericCLIResults struct {
    89  	results   map[string]GenericCLIResult
    90  	InputArgs []string
    91  }
    92  
    93  // IsSet returns a bool if the flag was changed
    94  func (f GenericCLIResults) IsSet(flag string) bool {
    95  	r := f.findResult(flag)
    96  	if r == nil {
    97  		return false
    98  	}
    99  	return r.IsSet()
   100  }
   101  
   102  // Value returns the value of the cli flag
   103  func (f GenericCLIResults) Value(flag string) interface{} {
   104  	r := f.findResult(flag)
   105  	if r == nil {
   106  		return ""
   107  	}
   108  	return r.Value()
   109  }
   110  
   111  func (f GenericCLIResults) findResult(flag string) GenericCLIResult {
   112  	val, ok := f.results[flag]
   113  	if ok {
   114  		return val
   115  	}
   116  	logrus.Debugf("unable to find flag %s", flag)
   117  	return nil
   118  }
   119  
   120  // Bool is a wrapper to get a bool value from GenericCLIResults
   121  func (f GenericCLIResults) Bool(flag string) bool {
   122  	r := f.findResult(flag)
   123  	if r == nil {
   124  		return false
   125  	}
   126  	return r.Value().(bool)
   127  }
   128  
   129  // String is a wrapper to get a string value from GenericCLIResults
   130  func (f GenericCLIResults) String(flag string) string {
   131  	r := f.findResult(flag)
   132  	if r == nil {
   133  		return ""
   134  	}
   135  	return r.Value().(string)
   136  }
   137  
   138  // Uint is a wrapper to get an uint value from GenericCLIResults
   139  func (f GenericCLIResults) Uint(flag string) uint {
   140  	r := f.findResult(flag)
   141  	if r == nil {
   142  		return 0
   143  	}
   144  	return r.Value().(uint)
   145  }
   146  
   147  // StringSlice is a wrapper to get a stringslice value from GenericCLIResults
   148  func (f GenericCLIResults) StringSlice(flag string) []string {
   149  	r := f.findResult(flag)
   150  	if r == nil {
   151  		return []string{}
   152  	}
   153  	return r.Value().([]string)
   154  }
   155  
   156  // StringArray is a wrapper to get a stringslice value from GenericCLIResults
   157  func (f GenericCLIResults) StringArray(flag string) []string {
   158  	r := f.findResult(flag)
   159  	if r == nil {
   160  		return []string{}
   161  	}
   162  	return r.Value().([]string)
   163  }
   164  
   165  // Uint64 is a wrapper to get an uint64 value from GenericCLIResults
   166  func (f GenericCLIResults) Uint64(flag string) uint64 {
   167  	r := f.findResult(flag)
   168  	if r == nil {
   169  		return 0
   170  	}
   171  	return r.Value().(uint64)
   172  }
   173  
   174  // Int64 is a wrapper to get an int64 value from GenericCLIResults
   175  func (f GenericCLIResults) Int64(flag string) int64 {
   176  	r := f.findResult(flag)
   177  	if r == nil {
   178  		return 0
   179  	}
   180  	return r.Value().(int64)
   181  }
   182  
   183  // Int is a wrapper to get an int value from GenericCLIResults
   184  func (f GenericCLIResults) Int(flag string) int {
   185  	r := f.findResult(flag)
   186  	if r == nil {
   187  		return 0
   188  	}
   189  	return r.Value().(int)
   190  }
   191  
   192  // Float64 is a wrapper to get an float64 value from GenericCLIResults
   193  func (f GenericCLIResults) Float64(flag string) float64 {
   194  	r := f.findResult(flag)
   195  	if r == nil {
   196  		return 0
   197  	}
   198  	return r.Value().(float64)
   199  }
   200  
   201  // Float64 is a wrapper to get an float64 value from GenericCLIResults
   202  func (f GenericCLIResults) Changed(flag string) bool {
   203  	r := f.findResult(flag)
   204  	if r == nil {
   205  		return false
   206  	}
   207  	return r.IsSet()
   208  }
   209  
   210  // IsSet ...
   211  func (c CRStringSlice) IsSet() bool { return c.Changed }
   212  
   213  // Name ...
   214  func (c CRStringSlice) Name() string { return c.Flag }
   215  
   216  // Value ...
   217  func (c CRStringSlice) Value() interface{} { return c.Val }
   218  
   219  // IsSet ...
   220  func (c CRString) IsSet() bool { return c.Changed }
   221  
   222  // Name ...
   223  func (c CRString) Name() string { return c.Flag }
   224  
   225  // Value ...
   226  func (c CRString) Value() interface{} { return c.Val }
   227  
   228  // IsSet ...
   229  func (c CRUint64) IsSet() bool { return c.Changed }
   230  
   231  // Name ...
   232  func (c CRUint64) Name() string { return c.Flag }
   233  
   234  // Value ...
   235  func (c CRUint64) Value() interface{} { return c.Val }
   236  
   237  // IsSet ...
   238  func (c CRFloat64) IsSet() bool { return c.Changed }
   239  
   240  // Name ...
   241  func (c CRFloat64) Name() string { return c.Flag }
   242  
   243  // Value ...
   244  func (c CRFloat64) Value() interface{} { return c.Val }
   245  
   246  // IsSet ...
   247  func (c CRBool) IsSet() bool { return c.Changed }
   248  
   249  // Name ...
   250  func (c CRBool) Name() string { return c.Flag }
   251  
   252  // Value ...
   253  func (c CRBool) Value() interface{} { return c.Val }
   254  
   255  // IsSet ...
   256  func (c CRInt64) IsSet() bool { return c.Changed }
   257  
   258  // Name ...
   259  func (c CRInt64) Name() string { return c.Flag }
   260  
   261  // Value ...
   262  func (c CRInt64) Value() interface{} { return c.Val }
   263  
   264  // IsSet ...
   265  func (c CRUint) IsSet() bool { return c.Changed }
   266  
   267  // Name ...
   268  func (c CRUint) Name() string { return c.Flag }
   269  
   270  // Value ...
   271  func (c CRUint) Value() interface{} { return c.Val }
   272  
   273  // IsSet ...
   274  func (c CRInt) IsSet() bool { return c.Changed }
   275  
   276  // Name ...
   277  func (c CRInt) Name() string { return c.Flag }
   278  
   279  // Value ...
   280  func (c CRInt) Value() interface{} { return c.Val }
   281  
   282  // IsSet ...
   283  func (c CRStringArray) IsSet() bool { return c.Changed }
   284  
   285  // Name ...
   286  func (c CRStringArray) Name() string { return c.Flag }
   287  
   288  // Value ...
   289  func (c CRStringArray) Value() interface{} { return c.Val }