github.com/motyar/up@v0.2.10/config/stages.go (about)

     1  package config
     2  
     3  import (
     4  	"github.com/apex/up/internal/validate"
     5  	"github.com/pkg/errors"
     6  )
     7  
     8  // Stage config.
     9  type Stage struct {
    10  	Domain       string `json:"domain"`
    11  	Path         string `json:"path"`
    12  	Cert         string `json:"cert"`
    13  	Name         string `json:"-"`
    14  	HostedZoneID string `json:"-"`
    15  }
    16  
    17  // Stages config.
    18  type Stages struct {
    19  	Development *Stage `json:"development"`
    20  	Staging     *Stage `json:"staging"`
    21  	Production  *Stage `json:"production"`
    22  }
    23  
    24  // Default implementation.
    25  func (s *Stages) Default() error {
    26  	if s := s.Development; s != nil {
    27  		s.Name = "development"
    28  	}
    29  
    30  	if s := s.Staging; s != nil {
    31  		s.Name = "staging"
    32  	}
    33  
    34  	if s := s.Production; s != nil {
    35  		s.Name = "production"
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  // Validate implementation.
    42  func (s *Stages) Validate() error {
    43  	if s := s.Development; s != nil {
    44  		if err := validate.RequiredString(s.Domain); err != nil {
    45  			return errors.Wrap(err, ".development: .domain")
    46  		}
    47  	}
    48  
    49  	if s := s.Staging; s != nil {
    50  		if err := validate.RequiredString(s.Domain); err != nil {
    51  			return errors.Wrap(err, ".staging: .domain")
    52  		}
    53  	}
    54  
    55  	if s := s.Production; s != nil {
    56  		if err := validate.RequiredString(s.Domain); err != nil {
    57  			return errors.Wrap(err, ".production: .domain")
    58  		}
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  // List returns configured stages.
    65  func (s *Stages) List() (v []*Stage) {
    66  	if s := s.Development; s != nil {
    67  		v = append(v, s)
    68  	}
    69  
    70  	if s := s.Staging; s != nil {
    71  		v = append(v, s)
    72  	}
    73  
    74  	if s := s.Production; s != nil {
    75  		v = append(v, s)
    76  	}
    77  
    78  	return
    79  }
    80  
    81  // GetByDomain returns the stage by domain or nil.
    82  func (s *Stages) GetByDomain(domain string) *Stage {
    83  	for _, s := range s.List() {
    84  		if s.Domain == domain {
    85  			return s
    86  		}
    87  	}
    88  	return nil
    89  }