github.com/Venafi/vcert/v5@v5.10.2/pkg/certificate/chainOption.go (about)

     1  /*
     2   * Copyright 2018 Venafi, Inc.
     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 certificate
    18  
    19  import (
    20  	"strings"
    21  
    22  	"gopkg.in/yaml.v3"
    23  )
    24  
    25  // ChainOption represents the options to be used with the certificate chain
    26  type ChainOption int
    27  
    28  const (
    29  	//ChainOptionRootLast specifies the root certificate should be in the last position of the chain
    30  	ChainOptionRootLast ChainOption = iota
    31  	//ChainOptionRootFirst specifies the root certificate should be in the first position of the chain
    32  	ChainOptionRootFirst
    33  	//ChainOptionIgnore specifies the chain should be ignored
    34  	ChainOptionIgnore
    35  
    36  	// String representations of the ChainOption types
    37  	strChainOptionIgnore    = "ignore"
    38  	strChainOptionRootFirst = "root-first"
    39  	strChainOptionRootLast  = "root-last"
    40  	strChainOptionUnknown   = "unknown"
    41  )
    42  
    43  // String returns a string representation of this object
    44  func (co *ChainOption) String() string {
    45  	switch *co {
    46  	case ChainOptionIgnore:
    47  		return strChainOptionIgnore
    48  	case ChainOptionRootFirst:
    49  		return strChainOptionRootFirst
    50  	case ChainOptionRootLast:
    51  		return strChainOptionRootLast
    52  	default:
    53  		return strChainOptionUnknown
    54  	}
    55  }
    56  
    57  // ChainOptionFromString converts the string to the corresponding ChainOption
    58  func ChainOptionFromString(order string) ChainOption {
    59  	switch strings.ToLower(order) {
    60  	case strChainOptionRootFirst:
    61  		return ChainOptionRootFirst
    62  	case strChainOptionIgnore:
    63  		return ChainOptionIgnore
    64  	default:
    65  		return ChainOptionRootLast
    66  	}
    67  }
    68  
    69  // MarshalYAML customizes the behavior of ChainOption when being marshaled into a YAML document.
    70  // The returned value is marshaled in place of the original value implementing Marshaller
    71  func (co ChainOption) MarshalYAML() (interface{}, error) {
    72  	return co.String(), nil
    73  }
    74  
    75  // UnmarshalYAML customizes the behavior when being unmarshalled from a YAML document
    76  func (co *ChainOption) UnmarshalYAML(value *yaml.Node) error {
    77  	var strValue string
    78  	err := value.Decode(&strValue)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	*co = ChainOptionFromString(strValue)
    83  	return nil
    84  }