github.com/facebookincubator/ttpforge@v1.0.13-0.20240405153150-5ae801628835/pkg/platforms/spec.go (about)

     1  /*
     2  Copyright © 2023-present, Meta Platforms, Inc. and affiliates
     3  Permission is hereby granted, free of charge, to any person obtaining a copy
     4  of this software and associated documentation files (the "Software"), to deal
     5  in the Software without restriction, including without limitation the rights
     6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  copies of the Software, and to permit persons to whom the Software is
     8  furnished to do so, subject to the following conditions:
     9  The above copyright notice and this permission notice shall be included in
    10  all copies or substantial portions of the Software.
    11  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    17  THE SOFTWARE.
    18  */
    19  
    20  package platforms
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  
    26  	"github.com/facebookincubator/ttpforge/pkg/logging"
    27  )
    28  
    29  // Spec defines a platform as an
    30  // os/arch pair.
    31  type Spec struct {
    32  	OS   string
    33  	Arch string
    34  }
    35  
    36  // IsCompatibleWith returns true if the current spec is compatible with the
    37  // spec specified as its argument.
    38  // TTPs will often not care about the architecture and will
    39  // therefore specify requirements like:
    40  //
    41  //	platforms:
    42  //	  - os: windows
    43  //
    44  // In such cases, we can assume that the TTP is compatible with all
    45  // architectures.
    46  func (s *Spec) IsCompatibleWith(otherSpec Spec) bool {
    47  	// would be a bit weird to have a TTP
    48  	// that cares about architecture but not OS,
    49  	// but no harm in it
    50  	if s.OS != "" && s.OS != otherSpec.OS {
    51  		return false
    52  	}
    53  	if s.Arch != "" {
    54  		return s.Arch == otherSpec.Arch
    55  	}
    56  	return true
    57  }
    58  
    59  // String returns a human readable representation of the spec;
    60  // it is mainly used for error messages.
    61  func (s *Spec) String() string {
    62  	anyOS := "[any OS]"
    63  	anyArch := "[any architecture]"
    64  	fmtStr := "%v/%v"
    65  	if s.OS != "" {
    66  		if s.Arch != "" {
    67  			return fmt.Sprintf(fmtStr, s.OS, s.Arch)
    68  		}
    69  		return fmt.Sprintf(fmtStr, s.OS, anyArch)
    70  	} else if s.Arch != "" {
    71  		return fmt.Sprintf(fmtStr, anyOS, s.Arch)
    72  	}
    73  	if s.OS != "" && s.Arch != "" {
    74  		return fmt.Sprintf(fmtStr, anyOS, s.Arch)
    75  	}
    76  	return fmt.Sprintf(fmtStr, anyOS, anyArch)
    77  }
    78  
    79  // Validate checks whether the platform spec is valid.
    80  // To be valid, the spec must be enforceable, so
    81  // at least one of the fields must be non-empty.
    82  func (s *Spec) Validate() error {
    83  	if s.OS == "" && s.Arch == "" {
    84  		return fmt.Errorf("os and arch cannot both be empty")
    85  	}
    86  
    87  	// this really ought to to be a list I can
    88  	// import from some package, but doesn't look
    89  	// like that is possible right now
    90  	// https://stackoverflow.com/a/20728862
    91  	validOS := map[string]bool{
    92  		"android":   true,
    93  		"darwin":    true,
    94  		"dragonfly": true,
    95  		"freebsd":   true,
    96  		"linux":     true,
    97  		"netbsd":    true,
    98  		"openbsd":   true,
    99  		// if you run this on plan9 I will buy you a beer
   100  		"plan9":   true,
   101  		"solaris": true,
   102  		"windows": true,
   103  	}
   104  	if s.OS != "" && !validOS[s.OS] {
   105  		errorMsg := fmt.Sprintf("invalid `os` value %q specified", s.OS)
   106  		logging.L().Errorf(errorMsg)
   107  		logging.L().Errorf("valid values are:")
   108  		for k := range validOS {
   109  			logging.L().Errorf("\t%s", k)
   110  		}
   111  		return errors.New(errorMsg)
   112  	}
   113  
   114  	// https://stackoverflow.com/a/20728862
   115  	validArch := map[string]bool{
   116  		"arm":      true,
   117  		"386":      true,
   118  		"amd64":    true,
   119  		"arm64":    true,
   120  		"ppc64":    true,
   121  		"ppc64le":  true,
   122  		"mips":     true,
   123  		"mipsle":   true,
   124  		"mips64":   true,
   125  		"mips64le": true,
   126  	}
   127  	if s.Arch != "" && !validArch[s.Arch] {
   128  		errorMsg := fmt.Sprintf("invalid `arch` value %q specified", s.Arch)
   129  		logging.L().Errorf(errorMsg)
   130  		logging.L().Errorf("valid values are:")
   131  		for k := range validArch {
   132  			logging.L().Errorf("\t%s", k)
   133  		}
   134  		return errors.New(errorMsg)
   135  	}
   136  	return nil
   137  }