github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/yaml/run.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 (
    20  	Run struct {
    21  		Background       bool
    22  		Command          string
    23  		Environment      map[string]string
    24  		Name             string
    25  		NoOutputTimeout  string
    26  		Shell            string
    27  		When             string
    28  		WorkingDirectory string
    29  	}
    30  
    31  	run struct {
    32  		Background       bool              `yaml:"background,omitempty"`
    33  		Command          string            `yaml:"command"`
    34  		Environment      map[string]string `yaml:"environment,omitempty"`
    35  		Name             string            `yaml:"name,omitempty"`
    36  		NoOutputTimeout  string            `yaml:"no_output_timeout,omitempty"`
    37  		Shell            string            `yaml:"shell,omitempty"`
    38  		When             string            `yaml:"when,omitempty"`
    39  		WorkingDirectory string            `yaml:"working_directory,omitempty"`
    40  	}
    41  )
    42  
    43  // UnmarshalYAML implements the unmarshal interface.
    44  func (v *Run) UnmarshalYAML(unmarshal func(interface{}) error) error {
    45  	var out1 string
    46  	var out2 *run
    47  
    48  	if err := unmarshal(&out1); err == nil {
    49  		v.Command = out1
    50  		return nil
    51  	}
    52  
    53  	if err := unmarshal(&out2); err == nil {
    54  		v.Background = out2.Background
    55  		v.Command = out2.Command
    56  		v.Environment = out2.Environment
    57  		v.Name = out2.Name
    58  		v.NoOutputTimeout = out2.NoOutputTimeout
    59  		v.Shell = out2.Shell
    60  		v.When = out2.When
    61  		v.WorkingDirectory = out2.WorkingDirectory
    62  		return nil
    63  	}
    64  
    65  	return errors.New("failed to unmarshal machine")
    66  }
    67  
    68  // MarshalYAML implements the marshal interface.
    69  func (v *Run) MarshalYAML() (interface{}, error) {
    70  	if v.IsEmpty() {
    71  		return v.Command, nil
    72  	}
    73  	return &run{
    74  		Background:       v.Background,
    75  		Command:          v.Command,
    76  		Environment:      v.Environment,
    77  		Name:             v.Name,
    78  		NoOutputTimeout:  v.NoOutputTimeout,
    79  		Shell:            v.Shell,
    80  		When:             v.When,
    81  		WorkingDirectory: v.WorkingDirectory,
    82  	}, nil
    83  }
    84  
    85  // IsEmpty returns true if the struct is empty.
    86  func (v *Run) IsEmpty() bool {
    87  	return !v.Background &&
    88  		len(v.Environment) == 0 &&
    89  		len(v.Name) == 0 &&
    90  		len(v.NoOutputTimeout) == 0 &&
    91  		len(v.Shell) == 0 &&
    92  		len(v.When) == 0 &&
    93  		len(v.WorkingDirectory) == 0
    94  }