github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/project/configuration.go (about)

     1  package project
     2  
     3  import (
     4  	"github.com/mutagen-io/mutagen/pkg/api/models/forwarding"
     5  	"github.com/mutagen-io/mutagen/pkg/api/models/synchronization"
     6  	"github.com/mutagen-io/mutagen/pkg/encoding"
     7  )
     8  
     9  // ForwardingConfiguration encodes a forwarding session specification.
    10  type ForwardingConfiguration struct {
    11  	// Source is the source URL for the session.
    12  	Source string `yaml:"source"`
    13  	// Destination is the destination URL for the session.
    14  	Destination string `yaml:"destination"`
    15  	// Configuration is the configuration for the session.
    16  	Configuration forwarding.Configuration `yaml:",inline"`
    17  	// ConfigurationSource is the source-specific configuration for the session.
    18  	ConfigurationSource forwarding.Configuration `yaml:"configurationSource"`
    19  	// ConfigurationDestination is the destination-specific configuration for
    20  	// the session.
    21  	ConfigurationDestination forwarding.Configuration `yaml:"configurationDestination"`
    22  }
    23  
    24  // FlushOnCreateBehavior is a custom YAML type that can encode various
    25  // flush-on-create specifications, including a lack of specification.
    26  type FlushOnCreateBehavior uint8
    27  
    28  const (
    29  	// FlushOnCreateBehaviorDefault indicates that flush-on-create behavior is
    30  	// unspecified.
    31  	FlushOnCreateBehaviorDefault FlushOnCreateBehavior = iota
    32  	// FlushOnCreateBehaviorNoFlush indicates that flush-on-create behavior has
    33  	// been disabled.
    34  	FlushOnCreateBehaviorNoFlush
    35  	// FlushOnCreateBehaviorFlush indicates that flush-on-create behavior has
    36  	// been enabled.
    37  	FlushOnCreateBehaviorFlush
    38  )
    39  
    40  // IsDefault indicates whether or not the flush-on-create behavior is
    41  // FlushOnCreateBehaviorDefault.
    42  func (b FlushOnCreateBehavior) IsDefault() bool {
    43  	return b == FlushOnCreateBehaviorDefault
    44  }
    45  
    46  // FlushOnCreate converts the behavior specification to an actual boolean
    47  // indicating behavior.
    48  func (b FlushOnCreateBehavior) FlushOnCreate() bool {
    49  	switch b {
    50  	case FlushOnCreateBehaviorDefault:
    51  		return false
    52  	case FlushOnCreateBehaviorNoFlush:
    53  		return false
    54  	case FlushOnCreateBehaviorFlush:
    55  		return true
    56  	default:
    57  		panic("unhandled flush-on-create behavior")
    58  	}
    59  }
    60  
    61  // UnmarshalYAML implements Unmarshaler.UnmarshalYAML.
    62  func (b *FlushOnCreateBehavior) UnmarshalYAML(unmarshal func(any) error) error {
    63  	// Call the underlying unmarshaling function.
    64  	var flush bool
    65  	if err := unmarshal(&flush); err != nil {
    66  		return err
    67  	}
    68  
    69  	// Set behavior.
    70  	if flush {
    71  		*b = FlushOnCreateBehaviorFlush
    72  	} else {
    73  		*b = FlushOnCreateBehaviorNoFlush
    74  	}
    75  
    76  	// Success.
    77  	return nil
    78  }
    79  
    80  // SynchronizationConfiguration encodes a synchronization session specification.
    81  type SynchronizationConfiguration struct {
    82  	// Alpha is the alpha URL for the session.
    83  	Alpha string `yaml:"alpha"`
    84  	// Beta is the beta URL for the session.
    85  	Beta string `yaml:"beta"`
    86  	// FlushOnCreate indicates the flush-on-create behavior for the session.
    87  	FlushOnCreate FlushOnCreateBehavior `yaml:"flushOnCreate"`
    88  	// Configuration is the configuration for the session.
    89  	Configuration synchronization.Configuration `yaml:",inline"`
    90  	// ConfigurationAlpha is the alpha-specific configuration for the session.
    91  	ConfigurationAlpha synchronization.Configuration `yaml:"configurationAlpha"`
    92  	// ConfigurationBeta is the beta-specific configuration for the session.
    93  	ConfigurationBeta synchronization.Configuration `yaml:"configurationBeta"`
    94  }
    95  
    96  // Configuration is the orchestration configuration object type.
    97  type Configuration struct {
    98  	// BeforeCreate are setup commands to be run before session creation.
    99  	BeforeCreate []string `yaml:"beforeCreate"`
   100  	// AfterCreate are setup commands to be run after session creation.
   101  	AfterCreate []string `yaml:"afterCreate"`
   102  	// BeforePause are setup commands to be run before session pausing.
   103  	BeforePause []string `yaml:"beforePause"`
   104  	// AfterPause are setup commands to be run after session pausing.
   105  	AfterPause []string `yaml:"afterPause"`
   106  	// BeforeResume are setup commands to be run before session resumption.
   107  	BeforeResume []string `yaml:"beforeResume"`
   108  	// AfterResume are setup commands to be run after session resumption.
   109  	AfterResume []string `yaml:"afterResume"`
   110  	// BeforeTerminate are teardown commands to be run before session
   111  	// termination.
   112  	BeforeTerminate []string `yaml:"beforeTerminate"`
   113  	// AfterTerminate are teardown commands to be run after session termination.
   114  	AfterTerminate []string `yaml:"afterTerminate"`
   115  	// Commands are commands that can be invoked while a project is running.
   116  	Commands map[string]string `yaml:"commands"`
   117  	// Forwarding represents the forwarding sessions to be created. If a
   118  	// "defaults" key is present, it is treated as a template upon which other
   119  	// configurations are layered, thus keeping syntactic compatibility with the
   120  	// global Mutagen configuration file.
   121  	Forwarding map[string]ForwardingConfiguration `yaml:"forward"`
   122  	// Synchronization represents the forwarding sessions to be created. If a
   123  	// "defaults" key is present, it is treated as a template upon which other
   124  	// configurations are layered, thus keeping syntactic compatibility with the
   125  	// global Mutagen configuration file.
   126  	Synchronization map[string]SynchronizationConfiguration `yaml:"sync"`
   127  }
   128  
   129  // LoadConfiguration attempts to load a YAML-based Mutagen orchestration
   130  // configuration file from the specified path.
   131  func LoadConfiguration(path string) (*Configuration, error) {
   132  	// Create the target configuration object.
   133  	result := &Configuration{}
   134  
   135  	// Attempt to load. We pass-through os.IsNotExist errors.
   136  	if err := encoding.LoadAndUnmarshalYAML(path, result); err != nil {
   137  		return nil, err
   138  	}
   139  
   140  	// Success.
   141  	return result, nil
   142  }