github.com/dnephin/dobi@v0.15.0/config/meta.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dnephin/configtf"
     7  )
     8  
     9  // MetaConfig Configure **dobi** and include other config files.
    10  // name: meta
    11  // example: Set the the project name to ``mywebapp`` and run the ``all`` task by
    12  // default.
    13  //
    14  // .. code-block:: yaml
    15  //
    16  //     meta:
    17  //         project: mywebapp
    18  //         default: all
    19  //
    20  type MetaConfig struct {
    21  	// Default The name of a task from the ``dobi.yml`` to run when no
    22  	// task name is specified on the command line.
    23  	Default string
    24  
    25  	// Project The name of the project. Used to create unique identifiers for
    26  	// image tags and container names.
    27  	// default: *basename of ``dobi.yml``*
    28  	Project string
    29  
    30  	// Include A list of dobi configuration files to include. Paths are
    31  	// relative to the current working directory. Includs can be partial
    32  	// configs that depend on resources in any of the other included files.
    33  	// type: list of file paths or glob patterns
    34  	Include PathGlobs
    35  
    36  	// ExecID A template value used as part of unique identifiers for image tags
    37  	// and container names. This field supports :doc:`variables`. This value can
    38  	// be overridden with the ``$DOBI_EXEC_ID`` environment variable.
    39  	// default: ``{user.name}``
    40  	ExecID string `config:"exec-id"`
    41  }
    42  
    43  // Validate the MetaConfig
    44  func (m *MetaConfig) Validate(config *Config) error {
    45  	if _, ok := config.Resources[m.Default]; m.Default != "" && !ok {
    46  		return fmt.Errorf("undefined default resource: %s", m.Default)
    47  	}
    48  	if err := m.Include.Validate(); err != nil {
    49  		return fmt.Errorf("invalid include: %s", err)
    50  	}
    51  	return nil
    52  }
    53  
    54  // IsZero returns true if the struct contains only zero values, except for
    55  // Includes which is ignored
    56  func (m *MetaConfig) IsZero() bool {
    57  	return m.Default == "" && m.Project == "" && m.ExecID == ""
    58  }
    59  
    60  // NewMetaConfig returns a new MetaConfig from config values
    61  func NewMetaConfig(name string, values map[string]interface{}) (*MetaConfig, error) {
    62  	meta := &MetaConfig{}
    63  	return meta, configtf.Transform(name, values, meta)
    64  }