istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/util/formatting/msg_threshold.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package formatting
    16  
    17  import (
    18  	"errors"
    19  	"strings"
    20  
    21  	"istio.io/istio/pkg/config/analysis/diag"
    22  )
    23  
    24  // MessageThreshold is a wrapper around Level to be used as a cobra command line argument.
    25  // It should satisfy the pflag.Value interface.
    26  type MessageThreshold struct {
    27  	diag.Level
    28  }
    29  
    30  // String is a function declared in the pflag.Value interface
    31  func (m *MessageThreshold) String() string {
    32  	return m.Level.String()
    33  }
    34  
    35  // Type is a function declared in the pflag.Value interface
    36  func (m *MessageThreshold) Type() string {
    37  	return "Level"
    38  }
    39  
    40  // Set is a function declared in the pflag.Value interface
    41  func (m *MessageThreshold) Set(s string) error {
    42  	levelMap := diag.GetUppercaseStringToLevelMap()
    43  	level, ok := levelMap[strings.ToUpper(s)]
    44  	if !ok {
    45  		return errors.New("invalid level option")
    46  	}
    47  	m.Level = level
    48  	return nil
    49  }