github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/environ.go (about)

     1  // Copyright 2022 Harness, Inc.
     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 yaml
    16  
    17  import "errors"
    18  
    19  type Environment struct {
    20  	Name           string      `yaml:"name,omitempty"`
    21  	Url            string      `yaml:"url,omitempty"`
    22  	OnStop         string      `yaml:"on_stop,omitempty"`
    23  	Action         string      `yaml:"action,omitempty"` // start, prepare, stop, verify, access
    24  	AutoStopIn     string      `yaml:"auto_stop_in,omitempty"`
    25  	DeploymentTier string      `yaml:"deployment_tier,omitempty"` // production, staging, testing, development, other
    26  	Kubernetes     *Kubernetes `yaml:"kubernetes,omitempty"`
    27  }
    28  
    29  type Kubernetes struct {
    30  	Namespace string `yaml:"namespace,omitempty"`
    31  }
    32  
    33  // UnmarshalYAML implements the unmarshal interface.
    34  func (v *Environment) UnmarshalYAML(unmarshal func(interface{}) error) error {
    35  	var out1 string
    36  	var out2 = struct {
    37  		Name           string      `yaml:"name,omitempty"`
    38  		Url            string      `yaml:"url,omitempty"`
    39  		OnStop         string      `yaml:"on_stop,omitempty"`
    40  		Action         string      `yaml:"action,omitempty"`
    41  		AutoStopIn     string      `yaml:"auto_stop_in,omitempty"`
    42  		DeploymentTier string      `yaml:"deployment_tier,omitempty"`
    43  		Kubernetes     *Kubernetes `yaml:"kubernetes,omitempty"`
    44  	}{}
    45  
    46  	if err := unmarshal(&out1); err == nil {
    47  		v.Name = out1
    48  		return nil
    49  	}
    50  
    51  	if err := unmarshal(&out2); err == nil {
    52  		v.Name = out2.Name
    53  		v.Url = out2.Url
    54  		v.OnStop = out2.OnStop
    55  		v.Action = out2.Action
    56  		v.AutoStopIn = out2.AutoStopIn
    57  		v.DeploymentTier = out2.DeploymentTier
    58  		v.Kubernetes = out2.Kubernetes
    59  		return nil
    60  	}
    61  
    62  	return errors.New("failed to unmarshal environment")
    63  }